|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | ///|
|
16 |
| -fn copy_sign(x : Double, y : Double) -> Double { |
17 |
| - if x < 0 { |
18 |
| - -y.abs() |
19 |
| - } else { |
20 |
| - y.abs() |
| 16 | +pub fn op_mod(self : Double, other : Double) -> Double { |
| 17 | + let x = self |
| 18 | + let y = other |
| 19 | + let mut uint64_x = x.reinterpret_as_uint64() |
| 20 | + let mut uint64_y = y.reinterpret_as_uint64() |
| 21 | + let mut ex = ((uint64_x >> 52) & 0x7FF).to_int() |
| 22 | + let mut ey = ((uint64_y >> 52) & 0x7FF).to_int() |
| 23 | + let sign_x = uint64_x >> 63 |
| 24 | + let mut i : UInt64 = 0 |
| 25 | + if (uint64_y << 1) == 0 || y.is_nan() || ex == 0x7ff { |
| 26 | + return x * y / (x * y) |
21 | 27 | }
|
22 |
| -} |
23 |
| - |
24 |
| -///| |
25 |
| -fn ldexp(frac : Double, exp_ : Double) -> Double { |
26 |
| - if frac == 0 { |
27 |
| - 0 |
28 |
| - } else if frac.is_inf() || frac.is_nan() { |
29 |
| - frac |
30 |
| - } else { |
31 |
| - let (frac, e) = normalize(frac) |
32 |
| - let mut exp = exp_ |
33 |
| - exp += e.to_double() |
34 |
| - let mut x = frac.reinterpret_as_uint64() |
35 |
| - exp += (((x >> 52).to_int() & 0x7FF) - 1023).to_double() |
36 |
| - if exp < -1075 { |
37 |
| - copy_sign(0, frac) |
38 |
| - } else if exp > 1023 { |
39 |
| - if frac < 0 { |
40 |
| - neg_infinity |
41 |
| - } else { |
42 |
| - infinity |
43 |
| - } |
44 |
| - } else { |
45 |
| - let mut m : Double = 1 |
46 |
| - if exp < -1022 { |
47 |
| - exp += 53 |
48 |
| - m = 1.0 / (1 << 53).to_double() |
49 |
| - } |
50 |
| - x = x & (0x7FFUL << 52).lnot() |
51 |
| - x = x | ((exp + 1023.0).to_int64().reinterpret_as_uint64() << 52) |
52 |
| - m * x.reinterpret_as_double() |
| 28 | + if (uint64_x << 1) <= (uint64_y << 1) { |
| 29 | + if (uint64_x << 1) == (uint64_y << 1) { |
| 30 | + return 0.0 * x |
53 | 31 | }
|
| 32 | + return x |
54 | 33 | }
|
55 |
| -} |
56 | 34 |
|
57 |
| -///| |
58 |
| -pub fn op_mod(self : Double, other : Double) -> Double { |
59 |
| - if other == 0 || self.is_inf() || self.is_nan() || other.is_nan() { |
60 |
| - not_a_number |
| 35 | + // normalize x and y |
| 36 | + if ex == 0 { |
| 37 | + i = uint64_x << 12 |
| 38 | + while (i >> 63) == 0 { |
| 39 | + ex -= 1 |
| 40 | + i = i << 1 |
| 41 | + } |
| 42 | + uint64_x = uint64_x << (-ex + 1) |
61 | 43 | } else {
|
62 |
| - let y = other.abs() |
63 |
| - let (yfr, yexp) = frexp(y) |
64 |
| - let mut r = self |
65 |
| - if self < 0 { |
66 |
| - r = -self |
| 44 | + uint64_x = uint64_x & (18446744073709551615UL >> 12) |
| 45 | + uint64_x = uint64_x | (1UL << 52) |
| 46 | + } |
| 47 | + if ey == 0 { |
| 48 | + i = uint64_y << 12 |
| 49 | + while (i >> 63) == 0 { |
| 50 | + ey -= 1 |
| 51 | + i = i << 1 |
67 | 52 | }
|
68 |
| - while r >= y { |
69 |
| - let (rfr, rexp_) = frexp(r) |
70 |
| - let mut rexp = rexp_ |
71 |
| - if rfr < yfr { |
72 |
| - rexp -= 1 |
| 53 | + uint64_y = uint64_y << (-ey + 1) |
| 54 | + } else { |
| 55 | + uint64_y = uint64_y & (18446744073709551615UL >> 12) |
| 56 | + uint64_y = uint64_y | (1UL << 52) |
| 57 | + } |
| 58 | + |
| 59 | + // x mod y |
| 60 | + while ex > ey { |
| 61 | + i = uint64_x - uint64_y |
| 62 | + if (i >> 63) == 0 { |
| 63 | + if i == 0 { |
| 64 | + return 0.0 * x |
73 | 65 | }
|
74 |
| - r = r - ldexp(y, (rexp - yexp).to_double()) |
| 66 | + uint64_x = i |
75 | 67 | }
|
76 |
| - if self < 0 { |
77 |
| - r = -r |
| 68 | + uint64_x = uint64_x << 1 |
| 69 | + ex -= 1 |
| 70 | + } |
| 71 | + i = uint64_x - uint64_y |
| 72 | + if (i >> 63) == 0 { |
| 73 | + if i == 0 { |
| 74 | + return 0.0 * x |
78 | 75 | }
|
79 |
| - r |
| 76 | + uint64_x = i |
| 77 | + } |
| 78 | + while (uint64_x >> 52) == 0 { |
| 79 | + uint64_x = uint64_x << 1 |
| 80 | + ex -= 1 |
| 81 | + } |
| 82 | + |
| 83 | + // scale result |
| 84 | + if ex > 0 { |
| 85 | + uint64_x = uint64_x - (1UL << 52) |
| 86 | + uint64_x = uint64_x | (ex.to_uint64() << 52) |
| 87 | + } else { |
| 88 | + uint64_x = uint64_x >> (-ex + 1) |
80 | 89 | }
|
| 90 | + uint64_x = uint64_x | (sign_x << 63) |
| 91 | + uint64_x.reinterpret_as_double() |
81 | 92 | }
|
0 commit comments