Skip to content

Commit 453da11

Browse files
authored
Implement missing Math.cbrt function from ES6 (jerryscript-project#3680)
C implementation ported from fdlibm Part of Issue jerryscript-project#3568 JerryScript-DCO-1.0-Signed-off-by: Rafal Walczyna [email protected]
1 parent 1b01171 commit 453da11

File tree

7 files changed

+186
-4
lines changed

7 files changed

+186
-4
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-math.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,7 @@ ecma_builtin_math_dispatch_routine (uint16_t builtin_routine_id, /**< built-in w
552552
}
553553
case ECMA_MATH_OBJECT_CBRT:
554554
{
555-
#ifdef JERRY_LIBM_MATH_H
556-
return ecma_raise_type_error (ECMA_ERR_MSG ("UNIMPLEMENTED: Math.cbrt"));
557-
#else /* !JERRY_LIBM_MATH_H */
558555
x = DOUBLE_TO_ECMA_NUMBER_T (cbrt (x));
559-
#endif /* JERRY_LIBM_MATH_H */
560556
break;
561557
}
562558
case ECMA_MATH_OBJECT_COSH:

jerry-libm/cbrt.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* This file is based on work under the following copyright and permission
16+
* notice:
17+
*
18+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19+
*
20+
* Developed at SunSoft, a Sun Microsystems, Inc. business.
21+
* Permission to use, copy, modify, and distribute this
22+
* software is freely granted, provided that this notice
23+
* is preserved.
24+
*
25+
* @(#)s_cbrt.c 1.3 95/01/18
26+
*/
27+
28+
#include "jerry-libm-internal.h"
29+
30+
/* cbrt(x)
31+
* Return cube root of x
32+
*/
33+
34+
#define B1 715094163 /* B1 = (682 - 0.03306235651) * 2**20 */
35+
#define B2 696219795 /* B2 = (664 - 0.03306235651) * 2**20 */
36+
#define C 5.42857142857142815906e-01 /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
37+
#define D -7.05306122448979611050e-01 /* -864/1225 = 0xBFE691DE, 0x2532C834 */
38+
#define E 1.41428571428571436819e+00 /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
39+
#define F 1.60714285714285720630e+00 /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
40+
#define G 3.57142857142857150787e-01 /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
41+
42+
double
43+
cbrt (double x)
44+
{
45+
double r, s, w;
46+
double_accessor t, temp;
47+
unsigned int sign;
48+
t.dbl = 0.0;
49+
temp.dbl = x;
50+
51+
sign = temp.as_int.hi & 0x80000000; /* sign = sign(x) */
52+
temp.as_int.hi ^= sign;
53+
54+
if (temp.as_int.hi >= 0x7ff00000)
55+
{
56+
/* cbrt(NaN, INF) is itself */
57+
return (x + x);
58+
}
59+
if ((temp.as_int.hi | temp.as_int.lo) == 0)
60+
{
61+
/* cbrt(0) is itself */
62+
return (x);
63+
}
64+
/* rough cbrt to 5 bits */
65+
if (temp.as_int.hi < 0x00100000) /* subnormal number */
66+
{
67+
t.as_int.hi = 0x43500000; /* set t= 2**54 */
68+
t.dbl *= temp.dbl;
69+
t.as_int.hi = t.as_int.hi / 3 + B2;
70+
}
71+
else
72+
{
73+
t.as_int.hi = temp.as_int.hi / 3 + B1;
74+
}
75+
76+
/* new cbrt to 23 bits, may be implemented in single precision */
77+
r = t.dbl * t.dbl / temp.dbl;
78+
s = C + r * t.dbl;
79+
t.dbl *= G + F / (s + E + D / s);
80+
81+
/* chopped to 20 bits and make it larger than cbrt(x) */
82+
t.as_int.lo = 0;
83+
t.as_int.hi += 0x00000001;
84+
85+
/* one step newton iteration to 53 bits with error less than 0.667 ulps */
86+
s = t.dbl * t.dbl; /* t*t is exact */
87+
r = temp.dbl / s;
88+
w = t.dbl + t.dbl;
89+
r = (r - t.dbl) / (w + r); /* r-s is exact */
90+
t.dbl = t.dbl + (t.dbl * r);
91+
92+
/* retore the sign bit */
93+
t.as_int.hi |= sign;
94+
return (t.dbl);
95+
} /* cbrt */
96+
97+
#undef B1
98+
#undef B2
99+
#undef C
100+
#undef D
101+
#undef E
102+
#undef F
103+
#undef G

jerry-libm/include/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ double log10 (double);
6767
/* Power functions. */
6868
double pow (double, double);
6969
double sqrt (double);
70+
double cbrt (double);
7071

7172
/* Rounding and remainder functions. */
7273
double ceil (double);

jerry-libm/jerry-libm-internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ double log10 (double);
9797

9898
double pow (double x, double y);
9999
double sqrt (double x);
100+
double cbrt (double);
100101

101102
double ceil (double x);
102103
double fabs (double x);

tests/jerry/es2015/math-cbrt.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var p_zero = 0.0;
16+
var n_zero = -p_zero;
17+
18+
function isSameZero (x, y)
19+
{
20+
return x === 0 && (1 / x) === (1 / y);
21+
}
22+
23+
assert(isNaN(Math.cbrt(NaN)));
24+
assert(isSameZero(Math.cbrt(p_zero), p_zero));
25+
assert(isSameZero(Math.cbrt(n_zero), n_zero));
26+
assert(Math.cbrt(Number.POSITIVE_INFINITY) === Number.POSITIVE_INFINITY);
27+
assert(Math.cbrt(Number.NEGATIVE_INFINITY) === Number.NEGATIVE_INFINITY);
28+
29+
assert(Math.cbrt(1.0) === 1.0);
30+
assert(Math.cbrt(-1.0) === -1.0);
31+
32+
assert(Math.cbrt(27.0) === 3.0);
33+
assert(Math.cbrt(0.001) === 0.1);

tests/unit-libm/test-libm.inc.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,29 @@ check_double ("sqrt (4)", sqrt (4), 2.00000000000000000000E+00);
531531
check_double ("sqrt (0.25)", sqrt (0.25), 5.00000000000000000000E-01);
532532
check_double ("sqrt (6642.25)", sqrt (6642.25), 8.15000000000000000000E+01);
533533
check_double ("sqrt (15239.9025)", sqrt (15239.9025), 1.23450000000000002842E+02);
534+
check_double ("cbrt (0.0)", cbrt (0.0), 0.00000000000000000000E+00);
535+
check_double ("cbrt (-0.0)", cbrt (-0.0), -0.00000000000000000000E+00);
536+
check_double ("cbrt (1.0)", cbrt (1.0), 1.00000000000000000000E+00);
537+
check_double ("cbrt (-1.0)", cbrt (-1.0), -1.00000000000000000000E+00);
538+
check_double ("cbrt (INFINITY)", cbrt (INFINITY), INF);
539+
check_double ("cbrt (-INFINITY)", cbrt (-INFINITY), -INF);
540+
check_double ("cbrt (NAN)", cbrt (NAN), NAN);
541+
check_double ("cbrt (0.7)", cbrt (0.7), 8.87904001742600645919E-01);
542+
check_double ("cbrt (2)", cbrt (2), 1.25992104989487319067E+00);
543+
check_double ("cbrt (10)", cbrt (10), 2.15443469003188381450E+00);
544+
check_double ("cbrt (2.22e-308)", cbrt (2.22e-308), 2.81050475771047639693E-103);
545+
check_double ("cbrt (2.23e-308)", cbrt (2.23e-308), 2.81471841433133404618E-103);
546+
check_double ("cbrt (3.72e-09)", cbrt (3.72e-09), 1.54946217899915657419E-03);
547+
check_double ("cbrt (7.37e+19)", cbrt (7.37e+19), 4.19265534205965511501E+06);
548+
check_double ("cbrt (2209)", cbrt (2209), 1.30236256766892157799E+01);
549+
check_double ("cbrt (4)", cbrt (4), 1.58740105196819958344E+00);
550+
check_double ("cbrt (0.25)", cbrt (0.25), 6.29960524947436595333E-01);
551+
check_double ("cbrt (6642.25)", cbrt (6642.25), 1.87977155063238647870E+01);
552+
check_double ("cbrt (15239.9025)", cbrt (15239.9025), 2.47929038511971775449E+01);
553+
check_double ("cbrt (3)", cbrt (3), 1.44224957030740830177E+00);
554+
check_double ("cbrt (9)", cbrt (9), 2.08008382305190409056E+00);
555+
check_double ("cbrt (-17.87)", cbrt (-17.87), -2.61441695192974155049E+00);
556+
check_double ("cbrt (-8941)", cbrt (-8941), -2.07552848589356599973E+01);
534557
check_double ("sin (0.0)", sin (0.0), 0.00000000000000000000E+00);
535558
check_double ("sin (-0.0)", sin (-0.0), -0.00000000000000000000E+00);
536559
check_double ("sin (1.0)", sin (1.0), 8.41470984807896504876E-01);

tools/unit-tests/gen-test-libm.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,31 @@ main (int argc, char **args)
717717
GEN_DBL_TEST (sqrt (6642.25));
718718
GEN_DBL_TEST (sqrt (15239.9025));
719719

720+
/* cbrt tests */
721+
GEN_DBL_TEST (cbrt (0.0));
722+
GEN_DBL_TEST (cbrt (-0.0));
723+
GEN_DBL_TEST (cbrt (1.0));
724+
GEN_DBL_TEST (cbrt (-1.0));
725+
GEN_DBL_TEST (cbrt (INFINITY));
726+
GEN_DBL_TEST (cbrt (-INFINITY));
727+
GEN_DBL_TEST (cbrt (NAN));
728+
GEN_DBL_TEST (cbrt (0.7));
729+
GEN_DBL_TEST (cbrt (2));
730+
GEN_DBL_TEST (cbrt (10));
731+
GEN_DBL_TEST (cbrt (2.22e-308));
732+
GEN_DBL_TEST (cbrt (2.23e-308));
733+
GEN_DBL_TEST (cbrt (3.72e-09));
734+
GEN_DBL_TEST (cbrt (7.37e+19));
735+
GEN_DBL_TEST (cbrt (2209));
736+
GEN_DBL_TEST (cbrt (4));
737+
GEN_DBL_TEST (cbrt (0.25));
738+
GEN_DBL_TEST (cbrt (6642.25));
739+
GEN_DBL_TEST (cbrt (15239.9025));
740+
GEN_DBL_TEST (cbrt (3));
741+
GEN_DBL_TEST (cbrt (9));
742+
GEN_DBL_TEST (cbrt (-17.87));
743+
GEN_DBL_TEST (cbrt (-8941));
744+
720745
/* sin tests */
721746
GEN_DBL_TEST (sin (0.0));
722747
GEN_DBL_TEST (sin (-0.0));

0 commit comments

Comments
 (0)