Debian Codegraph @

rhino/src/org/mozilla/javascript/v8dtoa/DoubleHelper.java

raw symbols SCIP
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 // Helper functions for doubles.
34 public class DoubleHelper {
35
36 static final long kSignMask = 0x8000000000000000L;
37 static final long kExponentMask = 0x7FF0000000000000L;
38 static final long kSignificandMask = 0x000FFFFFFFFFFFFFL;
39 static final long kHiddenBit = 0x0010000000000000L;
40
41 static DiyFp asDiyFp(long d64) {
42 assert(!isSpecial(d64));
43 return new DiyFp(significand(d64), exponent(d64));
44 }
45
46 // this->Significand() must not be 0.
47 static DiyFp asNormalizedDiyFp(long d64) {
48 long f = significand(d64);
49 int e = exponent(d64);
50
51 assert(f != 0);
52
53 // The current double could be a denormal.
54 while ((f & kHiddenBit) == 0) {
55 f <<= 1;
56 e--;
57 }
58 // Do the final shifts in one go. Don't forget the hidden bit (the '-1').
59 f <<= DiyFp.kSignificandSize - kSignificandSize - 1;
60 e -= DiyFp.kSignificandSize - kSignificandSize - 1;
61 return new DiyFp(f, e);
62 }
63
64 static int exponent(long d64) {
65 if (isDenormal(d64)) return kDenormalExponent;
66
67 int biased_e = (int)(((d64 & kExponentMask) >>> kSignificandSize) & 0xffffffffL);
68 return biased_e - kExponentBias;
69 }
70
71 static long significand(long d64) {
72 long significand = d64 & kSignificandMask;
73 if (!isDenormal(d64)) {
74 return significand + kHiddenBit;
75 } else {
76 return significand;
77 }
78 }
79
80 // Returns true if the double is a denormal.
81 static boolean isDenormal(long d64) {
82 return (d64 & kExponentMask) == 0L;
83 }
84
85 // We consider denormals not to be special.
86 // Hence only Infinity and NaN are special.
87 static boolean isSpecial(long d64) {
88 return (d64 & kExponentMask) == kExponentMask;
89 }
90
91 static boolean isNan(long d64) {
92 return ((d64 & kExponentMask) == kExponentMask) &&
93 ((d64 & kSignificandMask) != 0L);
94 }
95
96
97 static boolean isInfinite(long d64) {
98 return ((d64 & kExponentMask) == kExponentMask) &&
99 ((d64 & kSignificandMask) == 0L);
100 }
101
102
103 static int sign(long d64) {
104 return (d64 & kSignMask) == 0L? 1: -1;
105 }
106
107
108 // Returns the two boundaries of first argument.
109 // The bigger boundary (m_plus) is normalized. The lower boundary has the same
110 // exponent as m_plus.
111 static void normalizedBoundaries(long d64, DiyFp m_minus, DiyFp m_plus) {
112 DiyFp v = asDiyFp(d64);
113 boolean significand_is_zero = (v.f() == kHiddenBit);
114 m_plus.setF((v.f() << 1) + 1);
115 m_plus.setE(v.e() - 1);
116 m_plus.normalize();
117 if (significand_is_zero && v.e() != kDenormalExponent) {
118 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
119 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
120 // at a distance of 1e8.
121 // The only exception is for the smallest normal: the largest denormal is
122 // at the same distance as its successor.
123 // Note: denormals have the same exponent as the smallest normals.
124 m_minus.setF((v.f() << 2) - 1);
125 m_minus.setE(v.e() - 2);
126 } else {
127 m_minus.setF((v.f() << 1) - 1);
128 m_minus.setE(v.e() - 1);
129 }
130 m_minus.setF(m_minus.f() << (m_minus.e() - m_plus.e()));
131 m_minus.setE(m_plus.e());
132 }
133
134 private static final int kSignificandSize = 52; // Excludes the hidden bit.
135 private static final int kExponentBias = 0x3FF + kSignificandSize;
136 private static final int kDenormalExponent = -kExponentBias + 1;
137
138 }