Debian Codegraph @

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

raw symbols SCIP
1 // Copyright 2011 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 V8's conversions-inl.h and double.h files.
29 // The original revision was r12273 from the bleeding_edge branch.
30
31 package org.mozilla.javascript.v8dtoa;
32
33 public final class DoubleConversion {
34 private static final long kSignMask = 0x8000000000000000L;
35 private static final long kExponentMask = 0x7FF0000000000000L;
36 private static final long kSignificandMask = 0x000FFFFFFFFFFFFFL;
37 private static final long kHiddenBit = 0x0010000000000000L;
38 private static final int kPhysicalSignificandSize = 52; // Excludes the hidden bit.
39 private static final int kSignificandSize = 53;
40 private static final int kExponentBias = 0x3FF + kPhysicalSignificandSize;
41 private static final int kDenormalExponent = -kExponentBias + 1;
42
43 private DoubleConversion() {
44 }
45
46 private static int exponent(long d64) {
47 if (isDenormal(d64))
48 return kDenormalExponent;
49
50 int biased_e = (int) ((d64 & kExponentMask) >> kPhysicalSignificandSize);
51 return biased_e - kExponentBias;
52 }
53
54 private static long significand(long d64) {
55 long significand = d64 & kSignificandMask;
56 if (!isDenormal(d64)) {
57 return significand + kHiddenBit;
58 } else {
59 return significand;
60 }
61 }
62
63 // Returns true if the double is a denormal.
64 private static boolean isDenormal(long d64) {
65 return (d64 & kExponentMask) == 0;
66 }
67
68 private static int sign(long d64) {
69 return (d64 & kSignMask) == 0 ? 1 : -1;
70 }
71
72 public static int doubleToInt32(double x) {
73 int i = (int) x;
74 if ((double) i == x) {
75 return i;
76 }
77 long d64 = Double.doubleToLongBits(x);
78 int exponent = exponent(d64);
79 if (exponent <= -kSignificandSize || exponent > 31) {
80 return 0;
81 }
82 long s = significand(d64);
83 return sign(d64) * (int) (exponent < 0 ? s >> -exponent : s << exponent);
84 }
85 }