14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci// List of constants in Math, excluding '@@' internal properties.
224514f5e3Sopenharmony_ci#define BUILTIN_MATH_CONSTANTS(V)   \
234514f5e3Sopenharmony_ci    V(E)                            \
244514f5e3Sopenharmony_ci    V(LN10)                         \
254514f5e3Sopenharmony_ci    V(LN2)                          \
264514f5e3Sopenharmony_ci    V(LOG10E)                       \
274514f5e3Sopenharmony_ci    V(LOG2E)                        \
284514f5e3Sopenharmony_ci    V(PI)                           \
294514f5e3Sopenharmony_ci    V(SQRT1_2)                      \
304514f5e3Sopenharmony_ci    V(SQRT2)
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ci// List of functions in Math.
334514f5e3Sopenharmony_ci// V(name, func, length, stubIndex)
344514f5e3Sopenharmony_ci// where BuiltinsMath::func refers to the native implementation of Math[name].
354514f5e3Sopenharmony_ci//       kungfu::BuiltinsStubCSigns::stubIndex refers to the builtin stub index, or INVALID if no stub available.
364514f5e3Sopenharmony_ci#define BUILTIN_MATH_FUNCTIONS(V)                                           \
374514f5e3Sopenharmony_ci    V("abs",    Abs,    1, MathAbs)     /* Math.abs ( x ) */                \
384514f5e3Sopenharmony_ci    V("acos",   Acos,   1, MathAcos)    /* Math.acos ( x ) */               \
394514f5e3Sopenharmony_ci    V("acosh",  Acosh,  1, MathAcosh)   /* Math.acosh ( x ) */              \
404514f5e3Sopenharmony_ci    V("asin",   Asin,   1, MathAsin)    /* Math.asin ( x ) */               \
414514f5e3Sopenharmony_ci    V("asinh",  Asinh,  1, MathAsinh)   /* Math.asinh ( x ) */              \
424514f5e3Sopenharmony_ci    V("atan",   Atan,   1, MathAtan)    /* Math.atan ( x ) */               \
434514f5e3Sopenharmony_ci    V("atan2",  Atan2,  2, MathAtan2)   /* Math.atan2 ( y, x ) */           \
444514f5e3Sopenharmony_ci    V("atanh",  Atanh,  1, MathAtanh)   /* Math.atanh ( x ) */              \
454514f5e3Sopenharmony_ci    V("cbrt",   Cbrt,   1, MathCbrt)     /* Math.cbrt ( x ) */              \
464514f5e3Sopenharmony_ci    V("ceil",   Ceil,   1, MathCeil)    /* Math.ceil ( x ) */               \
474514f5e3Sopenharmony_ci    V("clz32",  Clz32,  1, MathClz32)   /* Math.clz32 ( x ) */              \
484514f5e3Sopenharmony_ci    V("cos",    Cos,    1, MathCos)     /* Math.cos ( x ) */                \
494514f5e3Sopenharmony_ci    V("cosh",   Cosh,   1, MathCosh)    /* Math.cosh ( x ) */               \
504514f5e3Sopenharmony_ci    V("exp",    Exp,    1, MathExp)     /* Math.exp ( x ) */                \
514514f5e3Sopenharmony_ci    V("expm1",  Expm1,  1, MathExpm1)   /* Math.expm1 ( x ) */              \
524514f5e3Sopenharmony_ci    V("floor",  Floor,  1, MathFloor)       /* Math.floor ( x ) */              \
534514f5e3Sopenharmony_ci    V("fround", Fround, 1, MathFRound)  /* Math.fround ( x ) */             \
544514f5e3Sopenharmony_ci    V("hypot",  Hypot,  2, INVALID)     /* Math.hypot ( ...args ) */        \
554514f5e3Sopenharmony_ci    V("imul",   Imul,   2, MathImul)    /* Math.imul ( x, y ) */            \
564514f5e3Sopenharmony_ci    V("log",    Log,    1, MathLog)     /* Math.log ( x ) */                \
574514f5e3Sopenharmony_ci    V("log10",  Log10,  1, MathLog10)   /* Math.log10 ( x ) */              \
584514f5e3Sopenharmony_ci    V("log1p",  Log1p,  1, MathLog1p)   /* Math.log1p ( x ) */              \
594514f5e3Sopenharmony_ci    V("log2",   Log2,   1, MathLog2)    /* Math.log2 ( x ) */               \
604514f5e3Sopenharmony_ci    V("max",    Max,    2, MathMax)     /* Math.max ( ...args ) */          \
614514f5e3Sopenharmony_ci    V("min",    Min,    2, MathMin)     /* Math.min ( ...args ) */          \
624514f5e3Sopenharmony_ci    V("pow",    Pow,    2, MathPow)     /* Math.pow ( base, exponent ) */   \
634514f5e3Sopenharmony_ci    V("random", Random, 0, INVALID)     /* Math.random ( ) */               \
644514f5e3Sopenharmony_ci    V("round",  Round,  1, MathRound)   /* Math.round ( x ) */              \
654514f5e3Sopenharmony_ci    V("sign",   Sign,   1, MathSign)    /* Math.sign ( x ) */               \
664514f5e3Sopenharmony_ci    V("sin",    Sin,    1, MathSin)     /* Math.sin ( x ) */                \
674514f5e3Sopenharmony_ci    V("sinh",   Sinh,   1, MathSinh)    /* Math.sinh ( x ) */               \
684514f5e3Sopenharmony_ci    V("sqrt",   Sqrt,   1, MathSqrt)    /* Math.sqrt ( x ) */               \
694514f5e3Sopenharmony_ci    V("tan",    Tan,    1, MathTan)     /* Math.tan ( x ) */                \
704514f5e3Sopenharmony_ci    V("tanh",   Tanh,   1, MathTanh)    /* Math.tanh ( x ) */               \
714514f5e3Sopenharmony_ci    V("trunc",  Trunc,  1, MathTrunc)   /* Math.trunc ( x ) */
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
744514f5e3Sopenharmony_ciclass BuiltinsMath : public base::BuiltinsBase {
754514f5e3Sopenharmony_cipublic:
764514f5e3Sopenharmony_ci    // 20.2.1.1
774514f5e3Sopenharmony_ci    static constexpr double E = 2.718281828459045;
784514f5e3Sopenharmony_ci    // 20.2.1.2
794514f5e3Sopenharmony_ci    static constexpr double LN10 = 2.302585092994046;
804514f5e3Sopenharmony_ci    // 20.2.1.3
814514f5e3Sopenharmony_ci    static constexpr double LN2 = 0.6931471805599453;
824514f5e3Sopenharmony_ci    // 20.2.1.4
834514f5e3Sopenharmony_ci    static constexpr double LOG10E = 0.4342944819032518;
844514f5e3Sopenharmony_ci    // 20.2.1.5
854514f5e3Sopenharmony_ci    static constexpr double LOG2E = 1.4426950408889634;
864514f5e3Sopenharmony_ci    // 20.2.1.6
874514f5e3Sopenharmony_ci    static constexpr double PI = 3.141592653589793;
884514f5e3Sopenharmony_ci    // 20.2.1.7
894514f5e3Sopenharmony_ci    static constexpr double SQRT1_2 = 0.7071067811865476;
904514f5e3Sopenharmony_ci    // 20.2.1.8
914514f5e3Sopenharmony_ci    static constexpr double SQRT2 = 1.4142135623730951;
924514f5e3Sopenharmony_ci    // 20.2.2.1
934514f5e3Sopenharmony_ci    static JSTaggedValue Abs(EcmaRuntimeCallInfo *argv);
944514f5e3Sopenharmony_ci    // 20.2.2.2
954514f5e3Sopenharmony_ci    static JSTaggedValue Acos(EcmaRuntimeCallInfo *argv);
964514f5e3Sopenharmony_ci    // 20.2.2.3
974514f5e3Sopenharmony_ci    static JSTaggedValue Acosh(EcmaRuntimeCallInfo *argv);
984514f5e3Sopenharmony_ci    // 20.2.2.4
994514f5e3Sopenharmony_ci    static JSTaggedValue Asin(EcmaRuntimeCallInfo *argv);
1004514f5e3Sopenharmony_ci    // 20.2.2.5
1014514f5e3Sopenharmony_ci    static JSTaggedValue Asinh(EcmaRuntimeCallInfo *argv);
1024514f5e3Sopenharmony_ci    // 20.2.2.6
1034514f5e3Sopenharmony_ci    static JSTaggedValue Atan(EcmaRuntimeCallInfo *argv);
1044514f5e3Sopenharmony_ci    // 20.2.2.7
1054514f5e3Sopenharmony_ci    static JSTaggedValue Atanh(EcmaRuntimeCallInfo *argv);
1064514f5e3Sopenharmony_ci    // 20.2.2.8
1074514f5e3Sopenharmony_ci    static JSTaggedValue Atan2(EcmaRuntimeCallInfo *argv);
1084514f5e3Sopenharmony_ci    // 20.2.2.9
1094514f5e3Sopenharmony_ci    static JSTaggedValue Cbrt(EcmaRuntimeCallInfo *argv);
1104514f5e3Sopenharmony_ci    // 20.2.2.10
1114514f5e3Sopenharmony_ci    static JSTaggedValue Ceil(EcmaRuntimeCallInfo *argv);
1124514f5e3Sopenharmony_ci    // 20.2.2.11
1134514f5e3Sopenharmony_ci    static JSTaggedValue Clz32(EcmaRuntimeCallInfo *argv);
1144514f5e3Sopenharmony_ci    // 20.2.2.12
1154514f5e3Sopenharmony_ci    static JSTaggedValue Cos(EcmaRuntimeCallInfo *argv);
1164514f5e3Sopenharmony_ci    // 20.2.2.13
1174514f5e3Sopenharmony_ci    static JSTaggedValue Cosh(EcmaRuntimeCallInfo *argv);
1184514f5e3Sopenharmony_ci    // 20.2.2.14
1194514f5e3Sopenharmony_ci    static JSTaggedValue Exp(EcmaRuntimeCallInfo *argv);
1204514f5e3Sopenharmony_ci    // 20.2.2.15
1214514f5e3Sopenharmony_ci    static JSTaggedValue Expm1(EcmaRuntimeCallInfo *argv);
1224514f5e3Sopenharmony_ci    // 20.2.2.16
1234514f5e3Sopenharmony_ci    static JSTaggedValue Floor(EcmaRuntimeCallInfo *argv);
1244514f5e3Sopenharmony_ci    // 20.2.2.17
1254514f5e3Sopenharmony_ci    static JSTaggedValue Fround(EcmaRuntimeCallInfo *argv);
1264514f5e3Sopenharmony_ci    // 20.2.2.18
1274514f5e3Sopenharmony_ci    static JSTaggedValue Hypot(EcmaRuntimeCallInfo *argv);
1284514f5e3Sopenharmony_ci    // 20.2.2.19
1294514f5e3Sopenharmony_ci    static JSTaggedValue Imul(EcmaRuntimeCallInfo *argv);
1304514f5e3Sopenharmony_ci    // 20.2.2.20
1314514f5e3Sopenharmony_ci    static JSTaggedValue Log(EcmaRuntimeCallInfo *argv);
1324514f5e3Sopenharmony_ci    // 20.2.2.21
1334514f5e3Sopenharmony_ci    static JSTaggedValue Log1p(EcmaRuntimeCallInfo *argv);
1344514f5e3Sopenharmony_ci    // 20.2.2.22
1354514f5e3Sopenharmony_ci    static JSTaggedValue Log10(EcmaRuntimeCallInfo *argv);
1364514f5e3Sopenharmony_ci    // 20.2.2.23
1374514f5e3Sopenharmony_ci    static JSTaggedValue Log2(EcmaRuntimeCallInfo *argv);
1384514f5e3Sopenharmony_ci    // 20.2.2.24
1394514f5e3Sopenharmony_ci    static JSTaggedValue Max(EcmaRuntimeCallInfo *argv);
1404514f5e3Sopenharmony_ci    // 20.2.2.25
1414514f5e3Sopenharmony_ci    static JSTaggedValue Min(EcmaRuntimeCallInfo *argv);
1424514f5e3Sopenharmony_ci    // 20.2.2.26
1434514f5e3Sopenharmony_ci    static JSTaggedValue Pow(EcmaRuntimeCallInfo *argv);
1444514f5e3Sopenharmony_ci    // 20.2.2.27
1454514f5e3Sopenharmony_ci    static JSTaggedValue Random(EcmaRuntimeCallInfo *argv);
1464514f5e3Sopenharmony_ci    // 20.2.2.28
1474514f5e3Sopenharmony_ci    static JSTaggedValue Round(EcmaRuntimeCallInfo *argv);
1484514f5e3Sopenharmony_ci    // 20.2.2.29
1494514f5e3Sopenharmony_ci    static JSTaggedValue Sign(EcmaRuntimeCallInfo *argv);
1504514f5e3Sopenharmony_ci    // 20.2.2.30
1514514f5e3Sopenharmony_ci    static JSTaggedValue Sin(EcmaRuntimeCallInfo *argv);
1524514f5e3Sopenharmony_ci    // 20.2.2.31
1534514f5e3Sopenharmony_ci    static JSTaggedValue Sinh(EcmaRuntimeCallInfo *argv);
1544514f5e3Sopenharmony_ci    // 20.2.2.32
1554514f5e3Sopenharmony_ci    static JSTaggedValue Sqrt(EcmaRuntimeCallInfo *argv);
1564514f5e3Sopenharmony_ci    // 20.2.2.33
1574514f5e3Sopenharmony_ci    static JSTaggedValue Tan(EcmaRuntimeCallInfo *argv);
1584514f5e3Sopenharmony_ci    // 20.2.2.34
1594514f5e3Sopenharmony_ci    static JSTaggedValue Tanh(EcmaRuntimeCallInfo *argv);
1604514f5e3Sopenharmony_ci    // 20.2.2.35
1614514f5e3Sopenharmony_ci    static JSTaggedValue Trunc(EcmaRuntimeCallInfo *argv);
1624514f5e3Sopenharmony_ci
1634514f5e3Sopenharmony_ci    // Excluding the '@@' internal properties.
1644514f5e3Sopenharmony_ci    static Span<const base::BuiltinConstantEntry> GetMathConstants()
1654514f5e3Sopenharmony_ci    {
1664514f5e3Sopenharmony_ci        return Span<const base::BuiltinConstantEntry>(MATH_CONSTANTS);
1674514f5e3Sopenharmony_ci    }
1684514f5e3Sopenharmony_ci
1694514f5e3Sopenharmony_ci    static Span<const base::BuiltinFunctionEntry> GetMathFunctions()
1704514f5e3Sopenharmony_ci    {
1714514f5e3Sopenharmony_ci        return Span<const base::BuiltinFunctionEntry>(MATH_FUNCTIONS);
1724514f5e3Sopenharmony_ci    }
1734514f5e3Sopenharmony_ci
1744514f5e3Sopenharmony_ciprivate:
1754514f5e3Sopenharmony_ci#define BUILTIN_MATH_CONSTANT_ENTRY(name) \
1764514f5e3Sopenharmony_ci    base::BuiltinConstantEntry::Create(#name, JSTaggedValue(BuiltinsMath::name)),
1774514f5e3Sopenharmony_ci
1784514f5e3Sopenharmony_ci    static inline std::array MATH_CONSTANTS = {
1794514f5e3Sopenharmony_ci        BUILTIN_MATH_CONSTANTS(BUILTIN_MATH_CONSTANT_ENTRY)
1804514f5e3Sopenharmony_ci    };
1814514f5e3Sopenharmony_ci#undef BUILTIN_MATH_CONSTANT_ENTRY
1824514f5e3Sopenharmony_ci
1834514f5e3Sopenharmony_ci#define BUILTIN_MATH_FUNCTION_ENTRY(name, func, length, builtinId) \
1844514f5e3Sopenharmony_ci    base::BuiltinFunctionEntry::Create(name, BuiltinsMath::func, length, kungfu::BuiltinsStubCSigns::builtinId),
1854514f5e3Sopenharmony_ci
1864514f5e3Sopenharmony_ci    static constexpr std::array MATH_FUNCTIONS = {
1874514f5e3Sopenharmony_ci        BUILTIN_MATH_FUNCTIONS(BUILTIN_MATH_FUNCTION_ENTRY)
1884514f5e3Sopenharmony_ci    };
1894514f5e3Sopenharmony_ci#undef BUILTIN_MATH_FUNCTION_ENTRY
1904514f5e3Sopenharmony_ci};
1914514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
1924514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
193