| 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // Ported to Java from Mozilla's version of V8-dtoa by Hannes Wallnoefer. |
| 29 | // The original revision was 67d1049b0bf9 from the mozilla-central tree. |
| 30 | |
| 31 | package org.mozilla.javascript.v8dtoa; |
| 32 | |
| 33 | // This "Do It Yourself Floating Point" class implements a floating-point number |
| 34 | // with a uint64 significand and an int exponent. Normalized DiyFp numbers will |
| 35 | // have the most significant bit of the significand set. |
| 36 | // Multiplication and Subtraction do not normalize their results. |
| 37 | // DiyFp are not designed to contain special doubles (NaN and Infinity). |
| 38 | class DiyFp { |
| 39 | |
| 40 | private long f; |
| 41 | private int e; |
| 42 | |
| 43 | static final int kSignificandSize = 64; |
| 44 | static final long kUint64MSB = 0x8000000000000000L; |
| 45 | |
| 46 | |
| 47 | DiyFp() { |
| 48 | this.f = 0; |
| 49 | this.e = 0; |
| 50 | } |
| 51 | |
| 52 | DiyFp(long f, int e) { |
| 53 | this.f = f; |
| 54 | this.e = e; |
| 55 | } |
| 56 | |
| 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 | |
| 62 | // this = this - other. |
| 63 | // The exponents of both numbers must be the same and the significand of this |
| 64 | // must be bigger than the significand of other. |
| 65 | // The result will not be normalized. |
| 66 | void subtract(DiyFp other) { |
| 67 | assert (e == other.e); |
| 68 | assert uint64_gte(f, other.f); |
| 69 | f -= other.f; |
| 70 | } |
| 71 | |
| 72 | // Returns a - b. |
| 73 | // The exponents of both numbers must be the same and this must be bigger |
| 74 | // than other. The result will not be normalized. |
| 75 | static DiyFp minus(DiyFp a, DiyFp b) { |
| 76 | DiyFp result = new DiyFp(a.f, a.e); |
| 77 | result.subtract(b); |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | // this = this * other. |
| 83 | void multiply(DiyFp other) { |
| 84 | // Simply "emulates" a 128 bit multiplication. |
| 85 | // However: the resulting number only contains 64 bits. The least |
| 86 | // significant 64 bits are only used for rounding the most significant 64 |
| 87 | // bits. |
| 88 | final long kM32 = 0xFFFFFFFFL; |
| 89 | long a = f >>> 32; |
| 90 | long b = f & kM32; |
| 91 | long c = other.f >>> 32; |
| 92 | long d = other.f & kM32; |
| 93 | long ac = a * c; |
| 94 | long bc = b * c; |
| 95 | long ad = a * d; |
| 96 | long bd = b * d; |
| 97 | long tmp = (bd >>> 32) + (ad & kM32) + (bc & kM32); |
| 98 | // By adding 1U << 31 to tmp we round the final result. |
| 99 | // Halfway cases will be round up. |
| 100 | tmp += 1L << 31; |
| 101 | long result_f = ac + (ad >>> 32) + (bc >>> 32) + (tmp >>> 32); |
| 102 | e += other.e + 64; |
| 103 | f = result_f; |
| 104 | } |
| 105 | |
| 106 | // returns a * b; |
| 107 | static DiyFp times(DiyFp a, DiyFp b) { |
| 108 | DiyFp result = new DiyFp(a.f, a.e); |
| 109 | result.multiply(b); |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | void normalize() { |
| 114 | assert(f != 0); |
| 115 | long f = this.f; |
| 116 | int e = this.e; |
| 117 | |
| 118 | // This method is mainly called for normalizing boundaries. In general |
| 119 | // boundaries need to be shifted by 10 bits. We thus optimize for this case. |
| 120 | final long k10MSBits = 0xFFC00000L << 32; |
| 121 | while ((f & k10MSBits) == 0) { |
| 122 | f <<= 10; |
| 123 | e -= 10; |
| 124 | } |
| 125 | while ((f & kUint64MSB) == 0) { |
| 126 | f <<= 1; |
| 127 | e--; |
| 128 | } |
| 129 | this.f = f; |
| 130 | this.e = e; |
| 131 | } |
| 132 | |
| 133 | static DiyFp normalize(DiyFp a) { |
| 134 | DiyFp result = new DiyFp(a.f, a.e); |
| 135 | result.normalize(); |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | long f() { return f; } |
| 140 | int e() { return e; } |
| 141 | |
| 142 | void setF(long new_value) { f = new_value; } |
| 143 | void setE(int new_value) { e = new_value; } |
| 144 | |
| 145 | @Override |
| 146 | public String toString() { |
| 147 | return "[DiyFp f:" + f + ", e:" + e + "]"; |
| 148 | } |
| 149 | |
| 150 | } |