Added Math functions

JerryScript-DCO-1.0-Signed-off-by: Tamas Czene tczene.u-szeged@partner.samsung.com
This commit is contained in:
Tamas Czene
2015-05-08 13:49:09 +02:00
committed by Peter Gal
parent 7d703040d0
commit 7dfbc88cc0
44 changed files with 4647 additions and 388 deletions
@@ -1,4 +1,5 @@
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
* Copyright 2015 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +26,7 @@
#include "ecma-objects-general.h"
#include "ecma-try-catch-macro.h"
#include "jrt.h"
#include "fdlibm-math.h"
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_MATH_BUILTIN
@@ -64,14 +66,7 @@ ecma_builtin_math_object_abs (ecma_value_t this_arg __attr_unused___, /**< 'this
ecma_number_t *num_p = ecma_alloc_number ();
if (ecma_number_is_nan (arg_num))
{
*num_p = arg_num;
}
else
{
*num_p = ecma_number_abs (arg_num);
}
*num_p = DOUBLE_TO_ECMA_NUMBER_T (fabs (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
@@ -90,10 +85,20 @@ ecma_builtin_math_object_abs (ecma_value_t this_arg __attr_unused___, /**< 'this
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_math_object_acos (ecma_value_t this_arg, /**< 'this' argument */
ecma_builtin_math_object_acos (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = DOUBLE_TO_ECMA_NUMBER_T (acos (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_acos */
/**
@@ -106,10 +111,20 @@ ecma_builtin_math_object_acos (ecma_value_t this_arg, /**< 'this' argument */
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_math_object_asin (ecma_value_t this_arg, /**< 'this' argument */
ecma_builtin_math_object_asin (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = DOUBLE_TO_ECMA_NUMBER_T (asin (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_asin */
/**
@@ -122,10 +137,20 @@ ecma_builtin_math_object_asin (ecma_value_t this_arg, /**< 'this' argument */
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_math_object_atan (ecma_value_t this_arg, /**< 'this' argument */
ecma_builtin_math_object_atan (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = DOUBLE_TO_ECMA_NUMBER_T (atan (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_atan */
/**
@@ -138,11 +163,23 @@ ecma_builtin_math_object_atan (ecma_value_t this_arg, /**< 'this' argument */
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_math_object_atan2 (ecma_value_t this_arg, /**< 'this' argument */
ecma_builtin_math_object_atan2 (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
ecma_value_t arg1, /**< first routine's argument */
ecma_value_t arg2) /**< second routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg1, arg2);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (x, arg1, ret_value);
ECMA_OP_TO_NUMBER_TRY_CATCH (y, arg2, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = DOUBLE_TO_ECMA_NUMBER_T (atan2 (x, y));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (y);
ECMA_OP_TO_NUMBER_FINALIZE (x);
return ret_value;
} /* ecma_builtin_math_object_atan2 */
/**
@@ -155,10 +192,19 @@ ecma_builtin_math_object_atan2 (ecma_value_t this_arg, /**< 'this' argument */
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_math_object_ceil (ecma_value_t this_arg, /**< 'this' argument */
ecma_builtin_math_object_ceil (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = DOUBLE_TO_ECMA_NUMBER_T (ceil (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_ceil */
/**
@@ -179,53 +225,10 @@ ecma_builtin_math_object_cos (ecma_value_t this_arg __attr_unused___, /**< 'this
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
if (ecma_number_is_nan (arg_num)
|| ecma_number_is_infinity (arg_num))
{
*num_p = ecma_number_make_nan ();
}
else if (ecma_number_is_zero (arg_num))
{
*num_p = ECMA_NUMBER_ONE;
}
else
{
/* Taylor series of cos (x) around x = 0 is 1 - x^2/2! + x^4/4! - x^6/6! + ... */
ecma_number_t x = ecma_op_number_remainder (arg_num, 2 * ECMA_NUMBER_PI);
ecma_number_t neg_sqr_x = ecma_number_negate (ecma_number_multiply (x, x));
ecma_number_t sum = ECMA_NUMBER_ZERO;
ecma_number_t next_addendum = ECMA_NUMBER_ONE;
ecma_number_t next_factorial_factor = ECMA_NUMBER_ZERO;
ecma_number_t diff = ecma_number_make_infinity (false);
while ((ecma_number_is_zero (sum) && !ecma_number_is_zero (diff))
|| (!ecma_number_is_zero (sum)
&& ecma_number_abs (ecma_number_divide (diff, sum)) > ecma_number_relative_eps))
{
ecma_number_t next_sum = ecma_number_add (sum, next_addendum);
next_addendum = ecma_number_multiply (next_addendum, neg_sqr_x);
next_factorial_factor = ecma_number_add (next_factorial_factor, ECMA_NUMBER_ONE);
next_addendum = ecma_number_divide (next_addendum, next_factorial_factor);
next_factorial_factor = ecma_number_add (next_factorial_factor, ECMA_NUMBER_ONE);
next_addendum = ecma_number_divide (next_addendum, next_factorial_factor);
diff = ecma_number_abs (ecma_number_substract (sum, next_sum));
sum = next_sum;
}
*num_p = sum;
}
*num_p = DOUBLE_TO_ECMA_NUMBER_T (cos (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_cos */
@@ -248,29 +251,7 @@ ecma_builtin_math_object_exp (ecma_value_t this_arg __attr_unused___, /**< 'this
ecma_number_t *num_p = ecma_alloc_number ();
if (ecma_number_is_nan (arg_num))
{
*num_p = arg_num;
}
else if (ecma_number_is_zero (arg_num))
{
*num_p = ECMA_NUMBER_ONE;
}
else if (ecma_number_is_infinity (arg_num))
{
if (ecma_number_is_negative (arg_num))
{
*num_p = ECMA_NUMBER_ZERO;
}
else
{
*num_p = arg_num;
}
}
else
{
*num_p = ecma_number_exp (arg_num);
}
*num_p = DOUBLE_TO_ECMA_NUMBER_T (exp (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
@@ -289,10 +270,19 @@ ecma_builtin_math_object_exp (ecma_value_t this_arg __attr_unused___, /**< 'this
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_math_object_floor (ecma_value_t this_arg, /**< 'this' argument */
ecma_builtin_math_object_floor (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = DOUBLE_TO_ECMA_NUMBER_T (floor (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_floor */
/**
@@ -314,26 +304,7 @@ ecma_builtin_math_object_log (ecma_value_t this_arg __attr_unused___, /**< 'this
ecma_number_t *num_p = ecma_alloc_number ();
if (ecma_number_is_nan (arg_num))
{
*num_p = arg_num;
}
else if (ecma_number_is_zero (arg_num))
{
*num_p = ecma_number_make_infinity (true);
}
else if (ecma_number_is_negative (arg_num))
{
*num_p = ecma_number_make_nan ();
}
else if (ecma_number_is_infinity (arg_num))
{
*num_p = arg_num;
}
else
{
*num_p = ecma_number_ln (arg_num);
}
*num_p = DOUBLE_TO_ECMA_NUMBER_T (log (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
@@ -536,212 +507,7 @@ ecma_builtin_math_object_pow (ecma_value_t this_arg __attr_unused___, /**< 'this
ECMA_OP_TO_NUMBER_TRY_CATCH (y, arg2, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
if (ecma_number_is_nan (y)
|| (ecma_number_is_nan (x)
&& !ecma_number_is_zero (y)))
{
*num_p = ecma_number_make_nan ();
}
else if (ecma_number_is_zero (y))
{
*num_p = ECMA_NUMBER_ONE;
}
else if (ecma_number_is_infinity (y))
{
const ecma_number_t x_abs = ecma_number_abs (x);
if (x_abs == ECMA_NUMBER_ONE)
{
*num_p = ecma_number_make_nan ();
}
else if ((ecma_number_is_negative (y) && x_abs < ECMA_NUMBER_ONE)
|| (!ecma_number_is_negative (y) && x_abs > ECMA_NUMBER_ONE))
{
*num_p = ecma_number_make_infinity (false);
}
else
{
JERRY_ASSERT ((ecma_number_is_negative (y) && x_abs > ECMA_NUMBER_ONE)
|| (!ecma_number_is_negative (y) && x_abs < ECMA_NUMBER_ONE));
*num_p = ECMA_NUMBER_ZERO;
}
}
else
{
const ecma_number_t diff_is_int = ecma_op_number_remainder (y, ECMA_NUMBER_ONE);
const ecma_number_t rel_diff_is_int = ecma_number_abs (ecma_number_divide (diff_is_int,
y));
const ecma_number_t y_int = ecma_number_substract (y, diff_is_int);
const ecma_number_t y_int_half = ecma_number_multiply (y_int, ECMA_NUMBER_HALF);
const ecma_number_t diff_is_odd = ecma_op_number_remainder (y_int_half, ECMA_NUMBER_ONE);
const ecma_number_t rel_diff_is_odd = ecma_number_abs (ecma_number_divide (diff_is_odd,
y_int_half));
const bool is_y_int = (rel_diff_is_int < ecma_number_relative_eps);
const bool is_y_odd = (is_y_int && rel_diff_is_odd > ecma_number_relative_eps);
if (ecma_number_is_infinity (x))
{
if (!ecma_number_is_negative (x))
{
if (y > ECMA_NUMBER_ZERO)
{
*num_p = ecma_number_make_infinity (false);
}
else
{
JERRY_ASSERT (y < ECMA_NUMBER_ZERO);
*num_p = ECMA_NUMBER_ZERO;
}
}
else
{
if (y > ECMA_NUMBER_ZERO)
{
*num_p = ecma_number_make_infinity (is_y_odd);
}
else
{
JERRY_ASSERT (y < ECMA_NUMBER_ZERO);
if (is_y_odd)
{
*num_p = ecma_number_negate (ECMA_NUMBER_ZERO);
}
else
{
*num_p = ECMA_NUMBER_ZERO;
}
}
}
}
else if (ecma_number_is_zero (x))
{
if (!ecma_number_is_negative (x))
{
if (y > ECMA_NUMBER_ZERO)
{
*num_p = ECMA_NUMBER_ZERO;
}
else
{
JERRY_ASSERT (y < ECMA_NUMBER_ZERO);
*num_p = ecma_number_make_infinity (false);
}
}
else
{
if (y > ECMA_NUMBER_ZERO)
{
if (is_y_odd)
{
*num_p = ecma_number_negate (ECMA_NUMBER_ZERO);
}
else
{
*num_p = ECMA_NUMBER_ZERO;
}
}
else
{
*num_p = ecma_number_make_infinity (is_y_odd);
}
}
}
else if (!ecma_number_is_infinity (x)
&& x < ECMA_NUMBER_ZERO
&& !ecma_number_is_infinity (y)
&& !is_y_int)
{
*num_p = ecma_number_make_nan ();
}
else
{
JERRY_ASSERT (!ecma_number_is_infinity (x)
&& !ecma_number_is_zero (x));
JERRY_ASSERT (!ecma_number_is_infinity (y)
&& !ecma_number_is_zero (y));
const bool sign = (x < ECMA_NUMBER_ZERO && is_y_odd);
const bool invert = (y < ECMA_NUMBER_ZERO);
JERRY_ASSERT (is_y_int || !sign);
ecma_number_t positive_x;
ecma_number_t positive_y;
if (x < ECMA_NUMBER_ZERO)
{
JERRY_ASSERT (x < ECMA_NUMBER_ZERO);
positive_x = ecma_number_negate (x);
}
else
{
positive_x = x;
}
if (invert)
{
positive_y = ecma_number_negate (y);
}
else
{
positive_y = y;
}
ecma_number_t ret_num;
if (is_y_int
&& ecma_uint32_to_number (ecma_number_to_uint32 (positive_y)) == positive_y)
{
TODO (/* Check for license issues */);
uint32_t power_uint32 = ecma_number_to_uint32 (positive_y);
ret_num = ECMA_NUMBER_ONE;
ecma_number_t power_accumulator = positive_x;
while (power_uint32 != 0)
{
if (power_uint32 % 2)
{
ret_num = ecma_number_multiply (ret_num, power_accumulator);
power_uint32--;
}
power_accumulator = ecma_number_multiply (power_accumulator, power_accumulator);
power_uint32 /= 2;
}
}
else
{
/* pow (x, y) = exp (y * ln (x)) */
ecma_number_t ln_x = ecma_number_ln (positive_x);
ecma_number_t y_m_ln_x = ecma_number_multiply (positive_y, ln_x);
ret_num = ecma_number_exp (y_m_ln_x);
}
if (sign)
{
ret_num = ecma_number_negate (ret_num);
}
if (invert)
{
ret_num = ecma_number_divide (ECMA_NUMBER_ONE, ret_num);
}
*num_p = ret_num;
}
}
*num_p = DOUBLE_TO_ECMA_NUMBER_T (pow (x, y));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (y);
@@ -863,53 +629,10 @@ ecma_builtin_math_object_sin (ecma_value_t this_arg __attr_unused___, /**< 'this
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
if (ecma_number_is_nan (arg_num)
|| ecma_number_is_infinity (arg_num))
{
*num_p = ecma_number_make_nan ();
}
else if (ecma_number_is_zero (arg_num))
{
*num_p = arg_num;
}
else
{
/* Taylor series of sin (x) around x = 0 is x - x^3/3! + x^5/5! - x^7/7! + ... */
ecma_number_t x = ecma_op_number_remainder (arg_num, 2 * ECMA_NUMBER_PI);
ecma_number_t neg_sqr_x = ecma_number_negate (ecma_number_multiply (x, x));
ecma_number_t sum = ECMA_NUMBER_ZERO;
ecma_number_t next_addendum = ecma_number_divide (x, ECMA_NUMBER_ONE);
ecma_number_t next_factorial_factor = ECMA_NUMBER_ONE;
ecma_number_t diff = ecma_number_make_infinity (false);
while ((ecma_number_is_zero (sum) && !ecma_number_is_zero (diff))
|| (!ecma_number_is_zero (sum)
&& ecma_number_abs (ecma_number_divide (diff, sum)) > ecma_number_relative_eps))
{
ecma_number_t next_sum = ecma_number_add (sum, next_addendum);
next_addendum = ecma_number_multiply (next_addendum, neg_sqr_x);
next_factorial_factor = ecma_number_add (next_factorial_factor, ECMA_NUMBER_ONE);
next_addendum = ecma_number_divide (next_addendum, next_factorial_factor);
next_factorial_factor = ecma_number_add (next_factorial_factor, ECMA_NUMBER_ONE);
next_addendum = ecma_number_divide (next_addendum, next_factorial_factor);
diff = ecma_number_abs (ecma_number_substract (sum, next_sum));
sum = next_sum;
}
*num_p = sum;
}
*num_p = DOUBLE_TO_ECMA_NUMBER_T (sin (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_sin */
@@ -930,36 +653,11 @@ ecma_builtin_math_object_sqrt (ecma_value_t this_arg __attr_unused___, /**< 'thi
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t ret_num;
if (ecma_number_is_nan (arg_num)
|| (!ecma_number_is_zero (arg_num)
&& ecma_number_is_negative (arg_num)))
{
ret_num = ecma_number_make_nan ();
}
else if (ecma_number_is_zero (arg_num))
{
ret_num = arg_num;
}
else if (ecma_number_is_infinity (arg_num))
{
JERRY_ASSERT (!ecma_number_is_negative (arg_num));
ret_num = arg_num;
}
else
{
ret_num = ecma_number_sqrt (arg_num);
}
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = ret_num;
*num_p = DOUBLE_TO_ECMA_NUMBER_T (sqrt (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_sqrt */
@@ -973,10 +671,20 @@ ecma_builtin_math_object_sqrt (ecma_value_t this_arg __attr_unused___, /**< 'thi
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_math_object_tan (ecma_value_t this_arg, /**< 'this' argument */
ecma_builtin_math_object_tan (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
ecma_value_t arg) /**< routine's argument */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, arg, ret_value);
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = DOUBLE_TO_ECMA_NUMBER_T (tan (arg_num));
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
ECMA_OP_TO_NUMBER_FINALIZE (arg_num);
return ret_value;
} /* ecma_builtin_math_object_tan */
/**