Optimize number-to-string conversion
* Make constants static in `ecma_number_to_utf8_string`. * Make `ecma_number_to_utf8_string` use early returns, and rewrite its self-recursion in case of negative numbers. * Make the stringification of decimal exponent in `ecma_number_to_utf8_string` use `ecma_uint32_to_utf8_string`. * Changed ERROL0 dtoa implementation to use the `double` type instead of `ecma_number_t`. Thus, even is `ecma_number_t` is 32 bit wide, the algorithm works the same. * Changed `ecma_number_to_decimal` to use the ERROL0 dtoa algorithm for 32-bit floats as well. * Changed `ecma_number_to_decimal` to generate the decimal string representation of the mantissa instead of an `uint64_t` number. * Changed `ecma_number_to_utf8_string` to make use of the already available string representation of the mantissa, generated now by `ecma_number_to_decimal`. * Changed `ecma_number_to_utf8_string` not to use static arrays and variables for digit, "e", etc. generation. * Changed all `Number.prototype.toXXX` implementations and the `ecma_builtin_number_prototype_helper_round` helper to make use of the already available string representation of the mantissa, generated now by `ecma_number_to_decimal`. * Factored out the common stringification parts of all `Number.prototype.toXXX` implementations into a new helper `ecma_builtin_number_prototype_helper_to_string`. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
committed by
Zoltan Herczeg
parent
65542549af
commit
30fd549e7f
@@ -48,32 +48,110 @@
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Helper for stringifying numbers
|
||||
*
|
||||
* @return the length of the generated string representation
|
||||
*/
|
||||
static lit_utf8_size_t
|
||||
ecma_builtin_number_prototype_helper_to_string (lit_utf8_byte_t *digits_p, /**< number as string in decimal form */
|
||||
lit_utf8_size_t num_digits, /**< length of the string representation */
|
||||
int32_t exponent, /**< decimal exponent */
|
||||
lit_utf8_byte_t *to_digits_p, /**< [out] buffer to write */
|
||||
lit_utf8_size_t to_num_digits) /**< requested number of digits */
|
||||
{
|
||||
lit_utf8_byte_t *p = to_digits_p;
|
||||
|
||||
if (exponent <= 0)
|
||||
{
|
||||
/* Add zero to the integer part. */
|
||||
*p++ = '0';
|
||||
to_num_digits--;
|
||||
|
||||
if (to_num_digits > 0)
|
||||
{
|
||||
*p++ = '.';
|
||||
|
||||
/* Add leading zeros to the fraction part. */
|
||||
for (int i = 0; i < -exponent && to_num_digits > 0; i++)
|
||||
{
|
||||
*p++ = '0';
|
||||
to_num_digits--;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Add significant digits of the integer part. */
|
||||
lit_utf8_size_t to_copy = JERRY_MIN (num_digits, to_num_digits);
|
||||
to_copy = JERRY_MIN (to_copy, (lit_utf8_size_t) exponent);
|
||||
memmove (p, digits_p, (size_t) to_copy);
|
||||
p += to_copy;
|
||||
to_num_digits -= to_copy;
|
||||
digits_p += to_copy;
|
||||
num_digits -= to_copy;
|
||||
exponent -= (int32_t) to_copy;
|
||||
|
||||
/* Add zeros before decimal point. */
|
||||
while (exponent > 0 && to_num_digits > 0)
|
||||
{
|
||||
JERRY_ASSERT (num_digits == 0);
|
||||
*p++ = '0';
|
||||
to_num_digits--;
|
||||
exponent--;
|
||||
}
|
||||
|
||||
if (to_num_digits > 0)
|
||||
{
|
||||
*p++ = '.';
|
||||
}
|
||||
}
|
||||
|
||||
if (to_num_digits > 0)
|
||||
{
|
||||
/* Add significant digits of the fraction part. */
|
||||
lit_utf8_size_t to_copy = JERRY_MIN (num_digits, to_num_digits);
|
||||
memmove (p, digits_p, (size_t) to_copy);
|
||||
p += to_copy;
|
||||
to_num_digits -= to_copy;
|
||||
|
||||
/* Add trailing zeros. */
|
||||
while (to_num_digits > 0)
|
||||
{
|
||||
*p++ = '0';
|
||||
to_num_digits--;
|
||||
}
|
||||
}
|
||||
|
||||
return (lit_utf8_size_t) (p - to_digits_p);
|
||||
} /* ecma_builtin_number_prototype_helper_to_string */
|
||||
|
||||
/**
|
||||
* Helper for rounding numbers
|
||||
*
|
||||
* @return rounded number
|
||||
*/
|
||||
static uint64_t
|
||||
ecma_builtin_number_prototype_helper_round (uint64_t digits, /**< actual number **/
|
||||
int32_t round_num) /**< number of digits to round off **/
|
||||
static inline lit_utf8_size_t __attr_always_inline___
|
||||
ecma_builtin_number_prototype_helper_round (lit_utf8_byte_t *digits_p, /**< [in,out] number as a string in decimal
|
||||
* form */
|
||||
lit_utf8_size_t num_digits, /**< length of the string representation */
|
||||
int32_t round_num) /**< number of digits to keep */
|
||||
{
|
||||
int8_t digit = 0;
|
||||
|
||||
/* Remove unneeded precision digits. */
|
||||
while (round_num > 0)
|
||||
if (round_num < 1)
|
||||
{
|
||||
digit = (int8_t) (digits % 10);
|
||||
digits /= 10;
|
||||
round_num--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Round the last digit up if neccessary */
|
||||
if (digit >= 5)
|
||||
if ((lit_utf8_size_t) round_num >= num_digits)
|
||||
{
|
||||
digits++;
|
||||
return num_digits;
|
||||
}
|
||||
|
||||
return digits;
|
||||
if (digits_p[round_num] >= '5')
|
||||
{
|
||||
digits_p[round_num - 1]++;
|
||||
}
|
||||
return (lit_utf8_size_t) round_num;
|
||||
} /* ecma_builtin_number_prototype_helper_round */
|
||||
|
||||
/**
|
||||
@@ -107,7 +185,7 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
|
||||
}
|
||||
else
|
||||
{
|
||||
const lit_utf8_byte_t digit_chars[36] =
|
||||
static const lit_utf8_byte_t digit_chars[36] =
|
||||
{
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
||||
@@ -131,26 +209,23 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
|
||||
}
|
||||
else
|
||||
{
|
||||
uint64_t digits;
|
||||
int32_t num_digits;
|
||||
int32_t exponent;
|
||||
bool is_negative = false;
|
||||
bool should_round = false;
|
||||
|
||||
if (ecma_number_is_negative (this_arg_number))
|
||||
{
|
||||
this_arg_number = -this_arg_number;
|
||||
is_negative = true;
|
||||
}
|
||||
|
||||
ecma_number_to_decimal (this_arg_number, &digits, &num_digits, &exponent);
|
||||
lit_utf8_byte_t digits[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
|
||||
int32_t exponent;
|
||||
lit_utf8_size_t num_digits = ecma_number_to_decimal (this_arg_number, digits, &exponent);
|
||||
|
||||
exponent = exponent - num_digits;
|
||||
bool is_scale_negative = false;
|
||||
exponent = exponent - (int32_t) num_digits;
|
||||
|
||||
/* Calculate the scale of the number in the specified radix. */
|
||||
int scale = (int) -floor ((log (10) / log (radix)) * exponent);
|
||||
|
||||
bool is_scale_negative = false;
|
||||
if (scale < 0)
|
||||
{
|
||||
is_scale_negative = true;
|
||||
@@ -158,7 +233,6 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
|
||||
}
|
||||
|
||||
int buff_size;
|
||||
|
||||
if (is_scale_negative)
|
||||
{
|
||||
buff_size = (int) floor (log (this_arg_number) / log (radix)) + 1;
|
||||
@@ -174,13 +248,16 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
|
||||
}
|
||||
|
||||
/* Normalize the number, so that it is as close to 0 exponent as possible. */
|
||||
for (int i = 0; i < scale; i++)
|
||||
if (is_scale_negative)
|
||||
{
|
||||
if (is_scale_negative)
|
||||
for (int i = 0; i < scale; i++)
|
||||
{
|
||||
this_arg_number /= (ecma_number_t) radix;
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < scale; i++)
|
||||
{
|
||||
this_arg_number *= (ecma_number_t) radix;
|
||||
}
|
||||
@@ -189,6 +266,7 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this
|
||||
uint64_t whole = (uint64_t) this_arg_number;
|
||||
ecma_number_t fraction = this_arg_number - (ecma_number_t) whole;
|
||||
|
||||
bool should_round = false;
|
||||
if (!ecma_number_is_zero (fraction) && is_scale_negative)
|
||||
{
|
||||
/* Add one extra digit for rounding. */
|
||||
@@ -402,9 +480,8 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
|
||||
}
|
||||
else
|
||||
{
|
||||
bool is_negative = false;
|
||||
|
||||
/* 6. */
|
||||
bool is_negative = false;
|
||||
if (ecma_number_is_negative (this_num))
|
||||
{
|
||||
is_negative = true;
|
||||
@@ -431,20 +508,21 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
|
||||
}
|
||||
else
|
||||
{
|
||||
uint64_t digits = 0;
|
||||
int32_t num_digits = 0;
|
||||
int32_t exponent = 1;
|
||||
|
||||
/* 1. */
|
||||
int32_t frac_digits = ecma_number_to_int32 (arg_num);
|
||||
|
||||
/* Get the parameters of the number if non-zero. */
|
||||
lit_utf8_byte_t digits[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
|
||||
lit_utf8_size_t num_digits;
|
||||
int32_t exponent;
|
||||
|
||||
if (!ecma_number_is_zero (this_num))
|
||||
{
|
||||
ecma_number_to_decimal (this_num, &digits, &num_digits, &exponent);
|
||||
num_digits = ecma_number_to_decimal (this_num, digits, &exponent);
|
||||
}
|
||||
else
|
||||
{
|
||||
digits[0] = '0';
|
||||
num_digits = 1;
|
||||
exponent = 1;
|
||||
}
|
||||
|
||||
digits = ecma_builtin_number_prototype_helper_round (digits, num_digits - exponent - frac_digits);
|
||||
|
||||
/* 7. */
|
||||
if (exponent > 21)
|
||||
@@ -454,6 +532,11 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
|
||||
/* 8. */
|
||||
else
|
||||
{
|
||||
/* 1. */
|
||||
int32_t frac_digits = ecma_number_to_int32 (arg_num);
|
||||
|
||||
num_digits = ecma_builtin_number_prototype_helper_round (digits, num_digits, exponent + frac_digits);
|
||||
|
||||
/* Buffer that is used to construct the string. */
|
||||
int buffer_size = (exponent > 0) ? exponent + frac_digits + 2 : frac_digits + 3;
|
||||
|
||||
@@ -472,86 +555,13 @@ ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this_arg, /**< this
|
||||
*p++ = '-';
|
||||
}
|
||||
|
||||
int8_t digit = 0;
|
||||
uint64_t s = 1;
|
||||
|
||||
/* Calculate the magnitude of the number. This is used to get the digits from left to right. */
|
||||
while (s <= digits)
|
||||
{
|
||||
s *= 10;
|
||||
}
|
||||
|
||||
if (exponent <= 0)
|
||||
{
|
||||
/* Add leading zeros. */
|
||||
*p++ = '0';
|
||||
|
||||
if (frac_digits != 0)
|
||||
{
|
||||
*p++ = '.';
|
||||
}
|
||||
|
||||
for (int i = 0; i < -exponent && i < frac_digits; i++)
|
||||
{
|
||||
*p++ = '0';
|
||||
}
|
||||
|
||||
/* Add significant digits. */
|
||||
for (int i = -exponent; i < frac_digits; i++)
|
||||
{
|
||||
digit = 0;
|
||||
s /= 10;
|
||||
|
||||
while (digits >= s && s > 0)
|
||||
{
|
||||
digits -= s;
|
||||
digit++;
|
||||
}
|
||||
|
||||
*p = (lit_utf8_byte_t) ((lit_utf8_byte_t) digit + '0');
|
||||
p++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Add significant digits. */
|
||||
for (int i = 0; i < exponent; i++)
|
||||
{
|
||||
digit = 0;
|
||||
s /= 10;
|
||||
|
||||
while (digits >= s && s > 0)
|
||||
{
|
||||
digits -= s;
|
||||
digit++;
|
||||
}
|
||||
|
||||
*p = (lit_utf8_byte_t) ((lit_utf8_byte_t) digit + '0');
|
||||
p++;
|
||||
}
|
||||
|
||||
/* Add the decimal point after whole part. */
|
||||
if (frac_digits != 0)
|
||||
{
|
||||
*p++ = '.';
|
||||
}
|
||||
|
||||
/* Add neccessary fracion digits. */
|
||||
for (int i = 0; i < frac_digits; i++)
|
||||
{
|
||||
digit = 0;
|
||||
s /= 10;
|
||||
|
||||
while (digits >= s && s > 0)
|
||||
{
|
||||
digits -= s;
|
||||
digit++;
|
||||
}
|
||||
|
||||
*p = (lit_utf8_byte_t) ((lit_utf8_byte_t) digit + '0');
|
||||
p++;
|
||||
}
|
||||
}
|
||||
lit_utf8_size_t to_num_digits = ((exponent > 0) ? (lit_utf8_size_t) (exponent + frac_digits)
|
||||
: (lit_utf8_size_t) (frac_digits + 1));
|
||||
p += ecma_builtin_number_prototype_helper_to_string (digits,
|
||||
num_digits,
|
||||
exponent,
|
||||
p,
|
||||
to_num_digits);
|
||||
|
||||
JERRY_ASSERT (p - buff < buffer_size);
|
||||
/* String terminator. */
|
||||
@@ -606,9 +616,8 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
|
||||
}
|
||||
else
|
||||
{
|
||||
bool is_negative = false;
|
||||
|
||||
/* 5. */
|
||||
bool is_negative = false;
|
||||
if (ecma_number_is_negative (this_num) && !ecma_number_is_zero (this_num))
|
||||
{
|
||||
is_negative = true;
|
||||
@@ -635,27 +644,33 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
|
||||
}
|
||||
else
|
||||
{
|
||||
uint64_t digits = 0;
|
||||
int32_t num_digits = 0;
|
||||
int32_t exponent = 1;
|
||||
/* Get the parameters of the number if non zero. */
|
||||
lit_utf8_byte_t digits[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
|
||||
lit_utf8_size_t num_digits;
|
||||
int32_t exponent;
|
||||
|
||||
if (!ecma_number_is_zero (this_num))
|
||||
{
|
||||
/* Get the parameters of the number if non zero. */
|
||||
ecma_number_to_decimal (this_num, &digits, &num_digits, &exponent);
|
||||
num_digits = ecma_number_to_decimal (this_num, digits, &exponent);
|
||||
}
|
||||
else
|
||||
{
|
||||
digits[0] = '0';
|
||||
num_digits = 1;
|
||||
exponent = 1;
|
||||
}
|
||||
|
||||
int32_t frac_digits;
|
||||
if (ecma_is_value_undefined (arg))
|
||||
{
|
||||
frac_digits = num_digits - 1;
|
||||
frac_digits = (int32_t) num_digits - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
frac_digits = ecma_number_to_int32 (arg_num);
|
||||
}
|
||||
|
||||
digits = ecma_builtin_number_prototype_helper_round (digits, num_digits - frac_digits - 1);
|
||||
num_digits = ecma_builtin_number_prototype_helper_round (digits, num_digits, frac_digits + 1);
|
||||
|
||||
/* frac_digits + 2 characters for number, 5 characters for exponent, 1 for \0. */
|
||||
int buffer_size = frac_digits + 2 + 5 + 1;
|
||||
@@ -668,15 +683,6 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
|
||||
|
||||
JMEM_DEFINE_LOCAL_ARRAY (buff, buffer_size, lit_utf8_byte_t);
|
||||
|
||||
int digit = 0;
|
||||
uint64_t scale = 1;
|
||||
|
||||
/* Calculate the magnitude of the number. This is used to get the digits from left to right. */
|
||||
while (scale <= digits)
|
||||
{
|
||||
scale *= 10;
|
||||
}
|
||||
|
||||
lit_utf8_byte_t *actual_char_p = buff;
|
||||
|
||||
if (is_negative)
|
||||
@@ -684,25 +690,11 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
|
||||
*actual_char_p++ = '-';
|
||||
}
|
||||
|
||||
/* Add significant digits. */
|
||||
for (int i = 0; i <= frac_digits; i++)
|
||||
{
|
||||
digit = 0;
|
||||
scale /= 10;
|
||||
while (digits >= scale && scale > 0)
|
||||
{
|
||||
digits -= scale;
|
||||
digit++;
|
||||
}
|
||||
|
||||
*actual_char_p = (lit_utf8_byte_t) (digit + '0');
|
||||
actual_char_p++;
|
||||
|
||||
if (i == 0 && frac_digits != 0)
|
||||
{
|
||||
*actual_char_p++ = '.';
|
||||
}
|
||||
}
|
||||
actual_char_p += ecma_builtin_number_prototype_helper_to_string (digits,
|
||||
num_digits,
|
||||
1,
|
||||
actual_char_p,
|
||||
(lit_utf8_size_t) (frac_digits + 1));
|
||||
|
||||
*actual_char_p++ = 'e';
|
||||
|
||||
@@ -717,29 +709,8 @@ ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this_arg, /**<
|
||||
*actual_char_p++ = '+';
|
||||
}
|
||||
|
||||
/* Get magnitude of exponent. */
|
||||
int32_t scale_expt = 1;
|
||||
while (scale_expt <= exponent)
|
||||
{
|
||||
scale_expt *= 10;
|
||||
}
|
||||
scale_expt /= 10;
|
||||
|
||||
/* Add exponent digits. */
|
||||
if (exponent == 0)
|
||||
{
|
||||
*actual_char_p++ = '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
while (scale_expt > 0)
|
||||
{
|
||||
digit = exponent / scale_expt;
|
||||
exponent %= scale_expt;
|
||||
*actual_char_p++ = (lit_utf8_byte_t) (digit + '0');
|
||||
scale_expt /= 10;
|
||||
}
|
||||
}
|
||||
actual_char_p += ecma_uint32_to_utf8_string ((uint32_t) exponent, actual_char_p, 3);
|
||||
|
||||
JERRY_ASSERT (actual_char_p - buff < buffer_size);
|
||||
*actual_char_p = '\0';
|
||||
@@ -792,9 +763,8 @@ ecma_builtin_number_prototype_object_to_precision (ecma_value_t this_arg, /**< t
|
||||
}
|
||||
else
|
||||
{
|
||||
bool is_negative = false;
|
||||
|
||||
/* 6. */
|
||||
bool is_negative = false;
|
||||
if (ecma_number_is_negative (this_num) && !ecma_number_is_zero (this_num))
|
||||
{
|
||||
is_negative = true;
|
||||
@@ -826,19 +796,25 @@ ecma_builtin_number_prototype_object_to_precision (ecma_value_t this_arg, /**< t
|
||||
}
|
||||
else
|
||||
{
|
||||
uint64_t digits = 0;
|
||||
int32_t num_digits = 0;
|
||||
int32_t exponent = 1;
|
||||
/* Get the parameters of the number if non-zero. */
|
||||
lit_utf8_byte_t digits[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
|
||||
lit_utf8_size_t num_digits;
|
||||
int32_t exponent;
|
||||
|
||||
if (!ecma_number_is_zero (this_num))
|
||||
{
|
||||
num_digits = ecma_number_to_decimal (this_num, digits, &exponent);
|
||||
}
|
||||
else
|
||||
{
|
||||
digits[0] = '0';
|
||||
num_digits = 1;
|
||||
exponent = 1;
|
||||
}
|
||||
|
||||
int32_t precision = ecma_number_to_int32 (arg_num);
|
||||
|
||||
/* Get the parameters of the number if non-zero. */
|
||||
if (!ecma_number_is_zero (this_num))
|
||||
{
|
||||
ecma_number_to_decimal (this_num, &digits, &num_digits, &exponent);
|
||||
}
|
||||
|
||||
digits = ecma_builtin_number_prototype_helper_round (digits, num_digits - precision);
|
||||
num_digits = ecma_builtin_number_prototype_helper_round (digits, num_digits, precision);
|
||||
|
||||
int buffer_size;
|
||||
if (exponent < -5 || exponent > precision)
|
||||
@@ -865,42 +841,19 @@ ecma_builtin_number_prototype_object_to_precision (ecma_value_t this_arg, /**< t
|
||||
JMEM_DEFINE_LOCAL_ARRAY (buff, buffer_size, lit_utf8_byte_t);
|
||||
lit_utf8_byte_t *actual_char_p = buff;
|
||||
|
||||
uint64_t scale = 1;
|
||||
|
||||
/* Calculate the magnitude of the number. This is used to get the digits from left to right. */
|
||||
while (scale <= digits)
|
||||
{
|
||||
scale *= 10;
|
||||
}
|
||||
|
||||
if (is_negative)
|
||||
{
|
||||
*actual_char_p++ = '-';
|
||||
}
|
||||
|
||||
int digit = 0;
|
||||
|
||||
/* 10.c, Exponential notation.*/
|
||||
if (exponent < -5 || exponent > precision)
|
||||
{
|
||||
/* Add significant digits. */
|
||||
for (int i = 1; i <= precision; i++)
|
||||
{
|
||||
digit = 0;
|
||||
scale /= 10;
|
||||
while (digits >= scale && scale > 0)
|
||||
{
|
||||
digits -= scale;
|
||||
digit++;
|
||||
}
|
||||
|
||||
*actual_char_p++ = (lit_utf8_byte_t) (digit + '0');
|
||||
|
||||
if (i == 1 && i != precision)
|
||||
{
|
||||
*actual_char_p++ = '.';
|
||||
}
|
||||
}
|
||||
actual_char_p += ecma_builtin_number_prototype_helper_to_string (digits,
|
||||
num_digits,
|
||||
1,
|
||||
actual_char_p,
|
||||
(lit_utf8_size_t) precision);
|
||||
|
||||
*actual_char_p++ = 'e';
|
||||
|
||||
@@ -915,62 +868,20 @@ ecma_builtin_number_prototype_object_to_precision (ecma_value_t this_arg, /**< t
|
||||
*actual_char_p++ = '+';
|
||||
}
|
||||
|
||||
/* Get magnitude of exponent. */
|
||||
int32_t scale_expt = 1;
|
||||
while (scale_expt <= exponent)
|
||||
{
|
||||
scale_expt *= 10;
|
||||
}
|
||||
scale_expt /= 10;
|
||||
|
||||
/* Add exponent digits. */
|
||||
if (exponent == 0)
|
||||
{
|
||||
*actual_char_p++ = '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
while (scale_expt > 0)
|
||||
{
|
||||
digit = exponent / scale_expt;
|
||||
exponent %= scale_expt;
|
||||
*actual_char_p++ = (lit_utf8_byte_t) (digit + '0');
|
||||
scale_expt /= 10;
|
||||
}
|
||||
}
|
||||
actual_char_p += ecma_uint32_to_utf8_string ((uint32_t) exponent, actual_char_p, 3);
|
||||
}
|
||||
/* Fixed notation. */
|
||||
else
|
||||
{
|
||||
/* Add leading zeros if neccessary. */
|
||||
if (exponent <= 0)
|
||||
{
|
||||
*actual_char_p++ = '0';
|
||||
*actual_char_p++ = '.';
|
||||
for (int i = exponent; i < 0; i++)
|
||||
{
|
||||
*actual_char_p++ = '0';
|
||||
}
|
||||
}
|
||||
lit_utf8_size_t to_num_digits = ((exponent <= 0) ? (lit_utf8_size_t) (1 - exponent + precision)
|
||||
: (lit_utf8_size_t) precision);
|
||||
actual_char_p += ecma_builtin_number_prototype_helper_to_string (digits,
|
||||
num_digits,
|
||||
exponent,
|
||||
actual_char_p,
|
||||
to_num_digits);
|
||||
|
||||
/* Add significant digits. */
|
||||
for (int i = 1; i <= precision; i++)
|
||||
{
|
||||
digit = 0;
|
||||
scale /= 10;
|
||||
while (digits >= scale && scale > 0)
|
||||
{
|
||||
digits -= scale;
|
||||
digit++;
|
||||
}
|
||||
|
||||
*actual_char_p++ = (lit_utf8_byte_t) (digit + '0');
|
||||
|
||||
if (i == exponent && i != precision)
|
||||
{
|
||||
*actual_char_p++ = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_ASSERT (actual_char_p - buff < buffer_size);
|
||||
|
||||
Reference in New Issue
Block a user