Skip to content

Commit d1bc9b6

Browse files
anbahns
authored andcommitted
Java does not support unsigned types, so special care needs to be taken when mimicking unsigned types with their signed counterparts. Also remove Cloneable interface from DiyFp and instead call the constructor directly, no need to follow the C++ implementation and its copy-constructor so closely
1 parent 9ca79e1 commit d1bc9b6

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/org/mozilla/javascript/v8dtoa/DiyFp.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
// have the most significant bit of the significand set.
3636
// Multiplication and Subtraction do not normalize their results.
3737
// DiyFp are not designed to contain special doubles (NaN and Infinity).
38-
class DiyFp implements Cloneable {
38+
class DiyFp {
3939

4040
private long f;
4141
private int e;
@@ -54,21 +54,26 @@ class DiyFp implements Cloneable {
5454
this.e = e;
5555
}
5656

57+
private static boolean uint64_gte(long a, long b) {
58+
// greater-or-equal for unsigned int64 in java-style...
59+
return (a == b) || ((a > b) ^ (a < 0) ^ (b < 0));
60+
}
61+
5762
// this = this - other.
5863
// The exponents of both numbers must be the same and the significand of this
5964
// must be bigger than the significand of other.
6065
// The result will not be normalized.
6166
void subtract(DiyFp other) {
6267
assert (e == other.e);
63-
assert (f >= other.f);
68+
assert uint64_gte(f, other.f);
6469
f -= other.f;
6570
}
6671

6772
// Returns a - b.
6873
// The exponents of both numbers must be the same and this must be bigger
6974
// than other. The result will not be normalized.
7075
static DiyFp minus(DiyFp a, DiyFp b) {
71-
DiyFp result = a.clone();
76+
DiyFp result = new DiyFp(a.f, a.e);
7277
result.subtract(b);
7378
return result;
7479
}
@@ -100,7 +105,7 @@ void multiply(DiyFp other) {
100105

101106
// returns a * b;
102107
static DiyFp times(DiyFp a, DiyFp b) {
103-
DiyFp result = a.clone();
108+
DiyFp result = new DiyFp(a.f, a.e);
104109
result.multiply(b);
105110
return result;
106111
}
@@ -126,7 +131,7 @@ void normalize() {
126131
}
127132

128133
static DiyFp normalize(DiyFp a) {
129-
DiyFp result = a.clone();
134+
DiyFp result = new DiyFp(a.f, a.e);
130135
result.normalize();
131136
return result;
132137
}
@@ -137,14 +142,6 @@ static DiyFp normalize(DiyFp a) {
137142
void setF(long new_value) { f = new_value; }
138143
void setE(int new_value) { e = new_value; }
139144

140-
public DiyFp clone() {
141-
try {
142-
return (DiyFp) super.clone();
143-
} catch (CloneNotSupportedException e) {
144-
throw new RuntimeException(); // won't happen
145-
}
146-
}
147-
148145
@Override
149146
public String toString() {
150147
return "[DiyFp f:" + f + ", e:" + e + "]";

src/org/mozilla/javascript/v8dtoa/FastDtoa.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ static long biggestPowerTen(int number,
276276
return ((long)power << 32) | (0xffffffffL & exponent);
277277
}
278278

279+
private static boolean uint64_lte(long a, long b) {
280+
// less-or-equal for unsigned int64 in java-style...
281+
return (a == b) || ((a < b) ^ (a < 0) ^ (b < 0));
282+
}
279283

280284
// Generates the digits of input number w.
281285
// w is a floating-point number (DiyFp), consisting of a significand and an
@@ -325,7 +329,7 @@ static boolean digitGen(DiyFp low,
325329
FastDtoaBuilder buffer,
326330
int mk) {
327331
assert(low.e() == w.e() && w.e() == high.e());
328-
assert(low.f() + 1 <= high.f() - 1);
332+
assert uint64_lte(low.f() + 1, high.f() - 1);
329333
assert(minimal_target_exponent <= w.e() && w.e() <= maximal_target_exponent);
330334
// low, w and high are imprecise, but by less than one ulp (unit in the last
331335
// place).

0 commit comments

Comments
 (0)