v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
dtoa.cc
Go to the documentation of this file.
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <cmath>
8
9#include "src/base/logging.h"
14
15namespace v8 {
16namespace base {
17
19 switch (dtoa_mode) {
20 case DTOA_SHORTEST:
22 case DTOA_FIXED:
23 return BIGNUM_DTOA_FIXED;
24 case DTOA_PRECISION:
26 default:
28 }
29}
30
31void DoubleToAscii(double v, DtoaMode mode, int requested_digits,
32 Vector<char> buffer, int* sign, int* length, int* point) {
33 DCHECK(!Double(v).IsSpecial());
34 DCHECK(mode == DTOA_SHORTEST || requested_digits >= 0);
35
36 if (Double(v).Sign() < 0) {
37 *sign = 1;
38 v = -v;
39 } else {
40 *sign = 0;
41 }
42
43 if (v == 0) {
44 buffer[0] = '0';
45 buffer[1] = '\0';
46 *length = 1;
47 *point = 1;
48 return;
49 }
50
51 if (mode == DTOA_PRECISION && requested_digits == 0) {
52 buffer[0] = '\0';
53 *length = 0;
54 return;
55 }
56
57 bool fast_worked;
58 switch (mode) {
59 case DTOA_SHORTEST:
60 fast_worked = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, length, point);
61 break;
62 case DTOA_FIXED:
63 fast_worked = FastFixedDtoa(v, requested_digits, buffer, length, point);
64 break;
65 case DTOA_PRECISION:
66 fast_worked = FastDtoa(v, FAST_DTOA_PRECISION, requested_digits, buffer,
67 length, point);
68 break;
69 default:
71 }
72 if (fast_worked) return;
73
74 // If the fast dtoa didn't succeed use the slower bignum version.
75 BignumDtoaMode bignum_mode = DtoaToBignumDtoaMode(mode);
76 BignumDtoa(v, bignum_mode, requested_digits, buffer, length, point);
77 buffer[*length] = '\0';
78}
79
80} // namespace base
81} // namespace v8
void DoubleToAscii(double v, DtoaMode mode, int requested_digits, Vector< char > buffer, int *sign, int *length, int *point)
Definition dtoa.cc:31
void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, Vector< char > buffer, int *length, int *decimal_point)
static BignumDtoaMode DtoaToBignumDtoaMode(DtoaMode dtoa_mode)
Definition dtoa.cc:18
@ BIGNUM_DTOA_SHORTEST
Definition bignum-dtoa.h:17
@ BIGNUM_DTOA_FIXED
Definition bignum-dtoa.h:21
@ BIGNUM_DTOA_PRECISION
Definition bignum-dtoa.h:23
DtoaMode
Definition dtoa.h:13
@ DTOA_FIXED
Definition dtoa.h:21
@ DTOA_SHORTEST
Definition dtoa.h:17
@ DTOA_PRECISION
Definition dtoa.h:23
@ FAST_DTOA_SHORTEST
Definition fast-dtoa.h:17
@ FAST_DTOA_PRECISION
Definition fast-dtoa.h:20
bool FastFixedDtoa(double v, int fractional_count, Vector< char > buffer, int *length, int *decimal_point)
bool FastDtoa(double v, FastDtoaMode mode, int requested_digits, Vector< char > buffer, int *length, int *decimal_point)
Definition fast-dtoa.cc:713
#define UNREACHABLE()
Definition logging.h:67
#define DCHECK(condition)
Definition logging.h:482