sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 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 | #include <math.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | #include "dtoa.h" |
| 32 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 33 | #include "bignum-dtoa.h" |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 34 | #include "double.h" |
| 35 | #include "fast-dtoa.h" |
| 36 | #include "fixed-dtoa.h" |
| 37 | |
| 38 | namespace v8 { |
| 39 | namespace internal { |
| 40 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 41 | static BignumDtoaMode DtoaToBignumDtoaMode(DtoaMode dtoa_mode) { |
| 42 | switch (dtoa_mode) { |
| 43 | case DTOA_SHORTEST: return BIGNUM_DTOA_SHORTEST; |
| 44 | case DTOA_FIXED: return BIGNUM_DTOA_FIXED; |
| 45 | case DTOA_PRECISION: return BIGNUM_DTOA_PRECISION; |
| 46 | default: |
| 47 | UNREACHABLE(); |
| 48 | return BIGNUM_DTOA_SHORTEST; // To silence compiler. |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void DoubleToAscii(double v, DtoaMode mode, int requested_digits, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 54 | Vector<char> buffer, int* sign, int* length, int* point) { |
| 55 | ASSERT(!Double(v).IsSpecial()); |
| 56 | ASSERT(mode == DTOA_SHORTEST || requested_digits >= 0); |
| 57 | |
| 58 | if (Double(v).Sign() < 0) { |
| 59 | *sign = 1; |
| 60 | v = -v; |
| 61 | } else { |
| 62 | *sign = 0; |
| 63 | } |
| 64 | |
| 65 | if (v == 0) { |
| 66 | buffer[0] = '0'; |
| 67 | buffer[1] = '\0'; |
| 68 | *length = 1; |
| 69 | *point = 1; |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 70 | return; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (mode == DTOA_PRECISION && requested_digits == 0) { |
| 74 | buffer[0] = '\0'; |
| 75 | *length = 0; |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 76 | return; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 77 | } |
| 78 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 79 | bool fast_worked; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 80 | switch (mode) { |
| 81 | case DTOA_SHORTEST: |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 82 | fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point); |
| 83 | break; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 84 | case DTOA_FIXED: |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 85 | fast_worked = FastFixedDtoa(v, requested_digits, buffer, length, point); |
| 86 | break; |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 87 | case DTOA_PRECISION: |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 88 | fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits, |
| 89 | buffer, length, point); |
| 90 | break; |
| 91 | default: |
| 92 | UNREACHABLE(); |
| 93 | fast_worked = false; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 94 | } |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 95 | if (fast_worked) return; |
| 96 | |
| 97 | // If the fast dtoa didn't succeed use the slower bignum version. |
| 98 | BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode); |
| 99 | BignumDtoa(v, bignum_mode, requested_digits, buffer, length, point); |
| 100 | buffer[*length] = '\0'; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } } // namespace v8::internal |