OLD | NEW |
(Empty) | |
| 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 #ifndef V8_CACHED_POWERS_H_ |
| 29 #define V8_CACHED_POWERS_H_ |
| 30 |
| 31 #include "diy_fp.h" |
| 32 |
| 33 namespace v8 { |
| 34 namespace internal { |
| 35 |
| 36 struct CachedPower { |
| 37 uint64_t significand; |
| 38 int16_t binary_exponent; |
| 39 int16_t decimal_exponent; |
| 40 }; |
| 41 |
| 42 // The following defines implement the interface between this file and the |
| 43 // generated 'powers_ten.h'. |
| 44 // GRISU_CACHE_NAME(1) contains all possible cached powers. |
| 45 // GRISU_CACHE_NAME(i) contains GRISU_CACHE_NAME(1) where only every 'i'th |
| 46 // element is kept. More formally GRISU_CACHE_NAME(i) contains the elements j*i |
| 47 // with 0 <= j < k with k such that j*k < the size of GRISU_CACHE_NAME(1). |
| 48 // The higher 'i' is the fewer elements we use. |
| 49 // Given that there are less elements, the exponent-distance between two |
| 50 // elements in the cache grows. The variable GRISU_CACHE_MAX_DISTANCE(i) stores |
| 51 // the maximum distance between two elements. |
| 52 #define GRISU_CACHE_STRUCT CachedPower |
| 53 #define GRISU_CACHE_NAME(i) kCachedPowers##i |
| 54 #define GRISU_CACHE_MAX_DISTANCE(i) kCachedPowersMaxDistance##i |
| 55 #define GRISU_CACHE_OFFSET kCachedPowerOffset |
| 56 #define GRISU_UINT64_C V8_2PART_UINT64_C |
| 57 // The following include imports the precompiled cached powers. |
| 58 #include "powers_ten.h" // NOLINT |
| 59 |
| 60 static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10) |
| 61 |
| 62 // We can't use a function since we reference variables depending on the 'i'. |
| 63 // This way the compiler is able to see at compile time that only one |
| 64 // cache-array variable is used and thus can remove all the others. |
| 65 #define COMPUTE_FOR_CACHE(i) \ |
| 66 if (!found && (gamma - alpha + 1 >= GRISU_CACHE_MAX_DISTANCE(i))) { \ |
| 67 int kQ = DiyFp::kSignificandSize; \ |
| 68 int k = ceiling((alpha - e + kQ - 1) * kD_1_LOG2_10); \ |
| 69 int index = (GRISU_CACHE_OFFSET + k - 1) / i + 1; \ |
| 70 cached_power = GRISU_CACHE_NAME(i)[index]; \ |
| 71 found = true; \ |
| 72 } \ |
| 73 |
| 74 static void GetCachedPower(int e, int alpha, int gamma, int* mk, DiyFp* c_mk) { |
| 75 // The following if statement should be optimized by the compiler so that only |
| 76 // one array is referenced and the others are not included in the object file. |
| 77 bool found = false; |
| 78 CachedPower cached_power; |
| 79 COMPUTE_FOR_CACHE(20); |
| 80 COMPUTE_FOR_CACHE(19); |
| 81 COMPUTE_FOR_CACHE(18); |
| 82 COMPUTE_FOR_CACHE(17); |
| 83 COMPUTE_FOR_CACHE(16); |
| 84 COMPUTE_FOR_CACHE(15); |
| 85 COMPUTE_FOR_CACHE(14); |
| 86 COMPUTE_FOR_CACHE(13); |
| 87 COMPUTE_FOR_CACHE(12); |
| 88 COMPUTE_FOR_CACHE(11); |
| 89 COMPUTE_FOR_CACHE(10); |
| 90 COMPUTE_FOR_CACHE(9); |
| 91 COMPUTE_FOR_CACHE(8); |
| 92 COMPUTE_FOR_CACHE(7); |
| 93 COMPUTE_FOR_CACHE(6); |
| 94 COMPUTE_FOR_CACHE(5); |
| 95 COMPUTE_FOR_CACHE(4); |
| 96 COMPUTE_FOR_CACHE(3); |
| 97 COMPUTE_FOR_CACHE(2); |
| 98 COMPUTE_FOR_CACHE(1); |
| 99 if (!found) { |
| 100 UNIMPLEMENTED(); |
| 101 // Silence compiler warnings. |
| 102 cached_power.significand = 0; |
| 103 cached_power.binary_exponent = 0; |
| 104 cached_power.decimal_exponent = 0; |
| 105 } |
| 106 *c_mk = DiyFp(cached_power.significand, cached_power.binary_exponent); |
| 107 *mk = cached_power.decimal_exponent; |
| 108 ASSERT((alpha <= c_mk->e() + e) && (c_mk->e() + e <= gamma)); |
| 109 } |
| 110 #undef GRISU_REDUCTION |
| 111 #undef GRISU_CACHE_STRUCT |
| 112 #undef GRISU_CACHE_NAME |
| 113 #undef GRISU_CACHE_MAX_DISTANCE |
| 114 #undef GRISU_CACHE_OFFSET |
| 115 #undef GRISU_UINT64_C |
| 116 |
| 117 } } // namespace v8::internal |
| 118 |
| 119 #endif // V8_CACHED_POWERS_H_ |
OLD | NEW |