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
@@ -919,14 +919,13 @@ ecma_number_to_int32 (ecma_number_t num) /**< ecma-number */
|
||||
*
|
||||
* Note:
|
||||
* The calculated values correspond to s, n, k parameters in ECMA-262 v5, 9.8.1, item 5:
|
||||
* - s represents digits of the number;
|
||||
* - k is the number of digits;
|
||||
* - n is the decimal exponent.
|
||||
* - parameter out_digits_p corresponds to s, the digits of the number;
|
||||
* - parameter out_decimal_exp_p corresponds to n, the decimal exponent;
|
||||
* - return value corresponds to k, the number of digits.
|
||||
*/
|
||||
void
|
||||
lit_utf8_size_t
|
||||
ecma_number_to_decimal (ecma_number_t num, /**< ecma-number */
|
||||
uint64_t *out_digits_p, /**< [out] digits */
|
||||
int32_t *out_digits_num_p, /**< [out] number of digits */
|
||||
lit_utf8_byte_t *out_digits_p, /**< [out] buffer to fill with digits */
|
||||
int32_t *out_decimal_exp_p) /**< [out] decimal exponent */
|
||||
{
|
||||
JERRY_ASSERT (!ecma_number_is_nan (num));
|
||||
@@ -934,130 +933,7 @@ ecma_number_to_decimal (ecma_number_t num, /**< ecma-number */
|
||||
JERRY_ASSERT (!ecma_number_is_infinity (num));
|
||||
JERRY_ASSERT (!ecma_number_is_negative (num));
|
||||
|
||||
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
|
||||
|
||||
*out_digits_p = ecma_errol0_dtoa (num, out_digits_num_p, out_decimal_exp_p);
|
||||
|
||||
#elif CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT32
|
||||
/* Less precise conversion */
|
||||
uint64_t fraction_uint64;
|
||||
uint32_t fraction;
|
||||
int32_t exponent;
|
||||
int32_t dot_shift;
|
||||
int32_t decimal_exp = 0;
|
||||
|
||||
dot_shift = ecma_number_get_fraction_and_exponent (num, &fraction_uint64, &exponent);
|
||||
|
||||
fraction = (uint32_t) fraction_uint64;
|
||||
JERRY_ASSERT (fraction == fraction_uint64);
|
||||
|
||||
if (exponent != 0)
|
||||
{
|
||||
ecma_number_t t = 1.0f;
|
||||
bool do_divide;
|
||||
|
||||
if (exponent < 0)
|
||||
{
|
||||
do_divide = true;
|
||||
|
||||
while (exponent <= 0)
|
||||
{
|
||||
t *= 2.0f;
|
||||
exponent++;
|
||||
|
||||
if (t >= 10.0f)
|
||||
{
|
||||
t /= 10.0f;
|
||||
decimal_exp--;
|
||||
}
|
||||
|
||||
JERRY_ASSERT (t < 10.0f);
|
||||
}
|
||||
|
||||
while (t > 1.0f)
|
||||
{
|
||||
exponent--;
|
||||
t /= 2.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
do_divide = false;
|
||||
|
||||
while (exponent >= 0)
|
||||
{
|
||||
t *= 2.0f;
|
||||
exponent--;
|
||||
|
||||
if (t >= 10.0f)
|
||||
{
|
||||
t /= 10.0f;
|
||||
decimal_exp++;
|
||||
}
|
||||
|
||||
JERRY_ASSERT (t < 10.0f);
|
||||
}
|
||||
|
||||
while (t > 2.0f)
|
||||
{
|
||||
exponent++;
|
||||
t /= 2.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if (do_divide)
|
||||
{
|
||||
fraction = (uint32_t) ((ecma_number_t) fraction / t);
|
||||
}
|
||||
else
|
||||
{
|
||||
fraction = (uint32_t) ((ecma_number_t) fraction * t);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t s;
|
||||
int32_t n;
|
||||
int32_t k;
|
||||
|
||||
if (exponent > 0)
|
||||
{
|
||||
fraction <<= exponent;
|
||||
}
|
||||
else
|
||||
{
|
||||
fraction >>= -exponent;
|
||||
}
|
||||
|
||||
const int32_t int_part_shift = dot_shift;
|
||||
const uint32_t frac_part_mask = ((((uint32_t) 1) << int_part_shift) - 1);
|
||||
|
||||
uint32_t int_part = fraction >> int_part_shift;
|
||||
uint32_t frac_part = fraction & frac_part_mask;
|
||||
|
||||
s = int_part;
|
||||
k = 1;
|
||||
n = decimal_exp + 1;
|
||||
|
||||
JERRY_ASSERT (int_part < 10);
|
||||
|
||||
while (k < ECMA_NUMBER_MAX_DIGITS
|
||||
&& frac_part != 0)
|
||||
{
|
||||
frac_part *= 10;
|
||||
|
||||
uint32_t new_frac_part = frac_part & frac_part_mask;
|
||||
uint32_t digit = (frac_part - new_frac_part) >> int_part_shift;
|
||||
s = s * 10 + digit;
|
||||
k++;
|
||||
frac_part = new_frac_part;
|
||||
}
|
||||
|
||||
JERRY_ASSERT (k > 0);
|
||||
|
||||
*out_digits_p = s;
|
||||
*out_digits_num_p = k;
|
||||
*out_decimal_exp_p = n;
|
||||
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
|
||||
return ecma_errol0_dtoa ((double) num, out_digits_p, out_decimal_exp_p);
|
||||
} /* ecma_number_to_decimal */
|
||||
|
||||
/**
|
||||
@@ -1074,204 +950,119 @@ ecma_number_to_utf8_string (ecma_number_t num, /**< ecma-number */
|
||||
lit_utf8_byte_t *buffer_p, /**< buffer for utf-8 string */
|
||||
lit_utf8_size_t buffer_size) /**< size of buffer */
|
||||
{
|
||||
const lit_utf8_byte_t digits[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
||||
const lit_utf8_byte_t e_chars[2] = { 'e', 'E' };
|
||||
const lit_utf8_byte_t plus_char = '+';
|
||||
const lit_utf8_byte_t minus_char = '-';
|
||||
const lit_utf8_byte_t dot_char = '.';
|
||||
lit_utf8_size_t size;
|
||||
lit_utf8_byte_t *dst_p;
|
||||
|
||||
if (ecma_number_is_nan (num))
|
||||
{
|
||||
// 1.
|
||||
lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_NAN, buffer_p, buffer_size);
|
||||
size = lit_get_magic_string_size (LIT_MAGIC_STRING_NAN);
|
||||
dst_p = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_NAN, buffer_p, buffer_size);
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
|
||||
if (ecma_number_is_zero (num))
|
||||
{
|
||||
// 2.
|
||||
*buffer_p = LIT_CHAR_0;
|
||||
JERRY_ASSERT (1 <= buffer_size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
dst_p = buffer_p;
|
||||
|
||||
if (ecma_number_is_negative (num))
|
||||
{
|
||||
// 3.
|
||||
*dst_p++ = LIT_CHAR_MINUS;
|
||||
num = ecma_number_negate (num);
|
||||
}
|
||||
|
||||
if (ecma_number_is_infinity (num))
|
||||
{
|
||||
// 4.
|
||||
dst_p = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_INFINITY_UL, dst_p,
|
||||
(lit_utf8_size_t) (buffer_p + buffer_size - dst_p));
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
|
||||
JERRY_ASSERT (ecma_number_get_next (ecma_number_get_prev (num)) == num);
|
||||
|
||||
// 5.
|
||||
uint32_t num_uint32 = ecma_number_to_uint32 (num);
|
||||
|
||||
if (((ecma_number_t) num_uint32) == num)
|
||||
{
|
||||
dst_p += ecma_uint32_to_utf8_string (num_uint32, dst_p, (lit_utf8_size_t) (buffer_p + buffer_size - dst_p));
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
|
||||
/* decimal exponent */
|
||||
int32_t n;
|
||||
/* number of digits in mantissa */
|
||||
int32_t k;
|
||||
|
||||
k = (int32_t) ecma_number_to_decimal (num, dst_p, &n);
|
||||
|
||||
if (k <= n && n <= 21)
|
||||
{
|
||||
// 6.
|
||||
dst_p += k;
|
||||
|
||||
memset (dst_p, LIT_CHAR_0, (size_t) (n - k));
|
||||
dst_p += n - k;
|
||||
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
|
||||
if (0 < n && n <= 21)
|
||||
{
|
||||
// 7.
|
||||
memmove (dst_p + n + 1, dst_p + n, (size_t) (k - n));
|
||||
*(dst_p + n) = LIT_CHAR_DOT;
|
||||
dst_p += k + 1;
|
||||
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
|
||||
if (-6 < n && n <= 0)
|
||||
{
|
||||
// 8.
|
||||
memmove (dst_p + 2 - n, dst_p, (size_t) k);
|
||||
memset (dst_p + 2, LIT_CHAR_0, (size_t) -n);
|
||||
*dst_p = LIT_CHAR_0;
|
||||
*(dst_p + 1) = LIT_CHAR_DOT;
|
||||
dst_p += k - n + 2;
|
||||
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
|
||||
if (k == 1)
|
||||
{
|
||||
// 9.
|
||||
dst_p++;
|
||||
}
|
||||
else
|
||||
{
|
||||
lit_utf8_byte_t *dst_p = buffer_p;
|
||||
|
||||
if (ecma_number_is_zero (num))
|
||||
{
|
||||
// 2.
|
||||
*dst_p++ = digits[0];
|
||||
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
size = (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
else if (ecma_number_is_negative (num))
|
||||
{
|
||||
// 3.
|
||||
*dst_p++ = minus_char;
|
||||
lit_utf8_size_t new_buffer_size = (lit_utf8_size_t) ((buffer_p + buffer_size) - dst_p);
|
||||
size = 1 + ecma_number_to_utf8_string (ecma_number_negate (num), dst_p, new_buffer_size);
|
||||
}
|
||||
else if (ecma_number_is_infinity (num))
|
||||
{
|
||||
// 4.
|
||||
dst_p = lit_copy_magic_string_to_buffer (LIT_MAGIC_STRING_INFINITY_UL, buffer_p, buffer_size);
|
||||
size = (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_number_t p = ecma_number_get_prev (num);
|
||||
ecma_number_t q = ecma_number_get_next (p);
|
||||
JERRY_ASSERT (q == num);
|
||||
|
||||
// 5.
|
||||
uint32_t num_uint32 = ecma_number_to_uint32 (num);
|
||||
|
||||
if (((ecma_number_t) num_uint32) == num)
|
||||
{
|
||||
size = ecma_uint32_to_utf8_string (num_uint32, dst_p, buffer_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* mantissa */
|
||||
uint64_t s;
|
||||
/* decimal exponent */
|
||||
int32_t n;
|
||||
/* number of digits in k */
|
||||
int32_t k;
|
||||
|
||||
ecma_number_to_decimal (num, &s, &k, &n);
|
||||
|
||||
// 6.
|
||||
if (k <= n && n <= 21)
|
||||
{
|
||||
dst_p += n;
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
|
||||
size = (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
|
||||
for (int32_t i = 0; i < n - k; i++)
|
||||
{
|
||||
*--dst_p = digits[0];
|
||||
}
|
||||
|
||||
for (int32_t i = 0; i < k; i++)
|
||||
{
|
||||
*--dst_p = digits[s % 10];
|
||||
s /= 10;
|
||||
}
|
||||
}
|
||||
else if (0 < n && n <= 21)
|
||||
{
|
||||
// 7.
|
||||
dst_p += k + 1;
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
|
||||
size = (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
|
||||
for (int32_t i = 0; i < k - n; i++)
|
||||
{
|
||||
*--dst_p = digits[s % 10];
|
||||
s /= 10;
|
||||
}
|
||||
|
||||
*--dst_p = dot_char;
|
||||
|
||||
for (int32_t i = 0; i < n; i++)
|
||||
{
|
||||
*--dst_p = digits[s % 10];
|
||||
s /= 10;
|
||||
}
|
||||
}
|
||||
else if (-6 < n && n <= 0)
|
||||
{
|
||||
// 8.
|
||||
dst_p += k - n + 1 + 1;
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
|
||||
size = (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
|
||||
for (int32_t i = 0; i < k; i++)
|
||||
{
|
||||
*--dst_p = digits[s % 10];
|
||||
s /= 10;
|
||||
}
|
||||
|
||||
for (int32_t i = 0; i < -n; i++)
|
||||
{
|
||||
*--dst_p = digits[0];
|
||||
}
|
||||
|
||||
*--dst_p = dot_char;
|
||||
*--dst_p = digits[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (k == 1)
|
||||
{
|
||||
// 9.
|
||||
JERRY_ASSERT (1 <= buffer_size);
|
||||
|
||||
size = 1;
|
||||
|
||||
*dst_p++ = digits[s % 10];
|
||||
s /= 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 10.
|
||||
dst_p += k + 1;
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
|
||||
for (int32_t i = 0; i < k - 1; i++)
|
||||
{
|
||||
*--dst_p = digits[s % 10];
|
||||
s /= 10;
|
||||
}
|
||||
|
||||
*--dst_p = dot_char;
|
||||
*--dst_p = digits[s % 10];
|
||||
s /= 10;
|
||||
|
||||
dst_p += k + 1;
|
||||
}
|
||||
|
||||
// 9., 10.
|
||||
JERRY_ASSERT (dst_p + 2 <= buffer_p + buffer_size);
|
||||
*dst_p++ = e_chars[0];
|
||||
*dst_p++ = (n >= 1) ? plus_char : minus_char;
|
||||
int32_t t = (n >= 1) ? (n - 1) : -(n - 1);
|
||||
|
||||
if (t == 0)
|
||||
{
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
*dst_p++ = digits[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
int32_t t_mod = 1000000000u;
|
||||
|
||||
while ((t / t_mod) == 0)
|
||||
{
|
||||
t_mod /= 10;
|
||||
|
||||
JERRY_ASSERT (t != 0);
|
||||
}
|
||||
|
||||
while (t_mod != 0)
|
||||
{
|
||||
JERRY_ASSERT (dst_p + 1 <= buffer_p + buffer_size);
|
||||
*dst_p++ = digits[t / t_mod];
|
||||
|
||||
t -= (t / t_mod) * t_mod;
|
||||
t_mod /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
size = (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
}
|
||||
|
||||
JERRY_ASSERT (s == 0);
|
||||
}
|
||||
}
|
||||
// 10.
|
||||
memmove (dst_p + 2, dst_p + 1, (size_t) (k - 1));
|
||||
*(dst_p + 1) = LIT_CHAR_DOT;
|
||||
dst_p += k + 1;
|
||||
}
|
||||
|
||||
return size;
|
||||
// 9., 10.
|
||||
*dst_p++ = LIT_CHAR_LOWERCASE_E;
|
||||
*dst_p++ = (n >= 1) ? LIT_CHAR_PLUS : LIT_CHAR_MINUS;
|
||||
uint32_t t = (uint32_t) (n >= 1 ? (n - 1) : -(n - 1));
|
||||
|
||||
dst_p += ecma_uint32_to_utf8_string (t, dst_p, (lit_utf8_size_t) (buffer_p + buffer_size - dst_p));
|
||||
|
||||
JERRY_ASSERT (dst_p <= buffer_p + buffer_size);
|
||||
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
} /* ecma_number_to_utf8_string */
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,8 +49,6 @@
|
||||
* @{
|
||||
*/
|
||||
|
||||
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
|
||||
|
||||
/**
|
||||
* Printing Floating-Point Numbers
|
||||
*
|
||||
@@ -70,8 +68,8 @@
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
ecma_number_t value; /**< value */
|
||||
ecma_number_t offset; /**< offset */
|
||||
double value; /**< value */
|
||||
double offset; /**< offset */
|
||||
} ecma_high_prec_t;
|
||||
|
||||
/**
|
||||
@@ -80,7 +78,7 @@ typedef struct
|
||||
static inline void __attr_always_inline___
|
||||
ecma_normalize_high_prec_data (ecma_high_prec_t *hp_data_p) /**< [in, out] float pair */
|
||||
{
|
||||
ecma_number_t val = hp_data_p->value;
|
||||
double val = hp_data_p->value;
|
||||
|
||||
hp_data_p->value += hp_data_p->offset;
|
||||
hp_data_p->offset += val - hp_data_p->value;
|
||||
@@ -92,12 +90,12 @@ ecma_normalize_high_prec_data (ecma_high_prec_t *hp_data_p) /**< [in, out] float
|
||||
static inline void __attr_always_inline___
|
||||
ecma_multiply_high_prec_by_10 (ecma_high_prec_t *hp_data_p) /**< [in, out] high-precision number */
|
||||
{
|
||||
ecma_number_t value = hp_data_p->value;
|
||||
double value = hp_data_p->value;
|
||||
|
||||
hp_data_p->value *= 10.0;
|
||||
hp_data_p->offset *= 10.0;
|
||||
|
||||
ecma_number_t offset = hp_data_p->value;
|
||||
double offset = hp_data_p->value;
|
||||
|
||||
offset -= value * 8.0;
|
||||
offset -= value * 2.0;
|
||||
@@ -113,7 +111,7 @@ ecma_multiply_high_prec_by_10 (ecma_high_prec_t *hp_data_p) /**< [in, out] high-
|
||||
static void
|
||||
ecma_divide_high_prec_by_10 (ecma_high_prec_t *hp_data_p) /**< [in, out] high-precision number */
|
||||
{
|
||||
ecma_number_t value = hp_data_p->value;
|
||||
double value = hp_data_p->value;
|
||||
|
||||
hp_data_p->value /= 10.0;
|
||||
hp_data_p->offset /= 10.0;
|
||||
@@ -129,15 +127,13 @@ ecma_divide_high_prec_by_10 (ecma_high_prec_t *hp_data_p) /**< [in, out] high-pr
|
||||
/**
|
||||
* Errol0 double to ASCII conversion, guaranteed correct but possibly not optimal.
|
||||
*
|
||||
* @return digits
|
||||
* @return number of generated digits
|
||||
*/
|
||||
inline uint64_t __attr_always_inline___
|
||||
ecma_errol0_dtoa (ecma_number_t val, /**< ecma number */
|
||||
int32_t *num_of_digits_p, /**< [out] number of digits */
|
||||
inline lit_utf8_size_t __attr_always_inline___
|
||||
ecma_errol0_dtoa (double val, /**< ecma number */
|
||||
lit_utf8_byte_t *buffer_p, /**< buffer to generate digits into */
|
||||
int32_t *exp_p) /**< [out] exponent */
|
||||
{
|
||||
uint64_t digits = 0u;
|
||||
int32_t num_of_digits = 0;
|
||||
double power_of_10 = 1.0;
|
||||
int32_t exp = 1;
|
||||
|
||||
@@ -195,6 +191,8 @@ ecma_errol0_dtoa (ecma_number_t val, /**< ecma number */
|
||||
|
||||
/* digit generation */
|
||||
|
||||
lit_utf8_byte_t *dst_p = buffer_p;
|
||||
|
||||
while (high_bound.value != 0.0 || high_bound.offset != 0.0)
|
||||
{
|
||||
uint8_t high_digit = (uint8_t) high_bound.value;
|
||||
@@ -216,9 +214,7 @@ ecma_errol0_dtoa (ecma_number_t val, /**< ecma number */
|
||||
break;
|
||||
}
|
||||
|
||||
digits *= 10;
|
||||
digits += (uint64_t) high_digit;
|
||||
num_of_digits++;
|
||||
*dst_p++ = (lit_utf8_byte_t) ('0' + high_digit);
|
||||
|
||||
high_bound.value -= high_digit;
|
||||
ecma_multiply_high_prec_by_10 (&high_bound);
|
||||
@@ -228,16 +224,13 @@ ecma_errol0_dtoa (ecma_number_t val, /**< ecma number */
|
||||
}
|
||||
|
||||
double mdig = (high_bound.value + low_bound.value) / 2.0 + 0.5;
|
||||
*dst_p++ = (lit_utf8_byte_t) ('0' + (uint8_t) mdig);
|
||||
|
||||
*num_of_digits_p = num_of_digits + 1;
|
||||
*exp_p = exp;
|
||||
digits *= 10;
|
||||
|
||||
return digits + (uint64_t) mdig;
|
||||
return (lit_utf8_size_t) (dst_p - buffer_p);
|
||||
} /* ecma_errol0_dtoa */
|
||||
|
||||
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -217,7 +217,7 @@ extern ecma_number_t ecma_number_add (ecma_number_t, ecma_number_t);
|
||||
extern ecma_number_t ecma_number_substract (ecma_number_t, ecma_number_t);
|
||||
extern ecma_number_t ecma_number_multiply (ecma_number_t, ecma_number_t);
|
||||
extern ecma_number_t ecma_number_divide (ecma_number_t, ecma_number_t);
|
||||
extern void ecma_number_to_decimal (ecma_number_t, uint64_t *, int32_t *, int32_t *);
|
||||
extern lit_utf8_size_t ecma_number_to_decimal (ecma_number_t, lit_utf8_byte_t *, int32_t *);
|
||||
|
||||
/* ecma-helpers-values-collection.c */
|
||||
extern ecma_collection_header_t *ecma_new_values_collection (const ecma_value_t[], ecma_length_t, bool);
|
||||
@@ -327,9 +327,7 @@ extern int32_t ecma_number_to_int32 (ecma_number_t);
|
||||
extern lit_utf8_size_t ecma_number_to_utf8_string (ecma_number_t, lit_utf8_byte_t *, lit_utf8_size_t);
|
||||
|
||||
/* ecma-helpers-errol.c */
|
||||
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
|
||||
extern uint64_t ecma_errol0_dtoa (ecma_number_t, int32_t *, int32_t *);
|
||||
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
|
||||
extern lit_utf8_size_t ecma_errol0_dtoa (double, lit_utf8_byte_t *, int32_t *);
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
||||
@@ -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