* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
#define ECMASCRIPT_BUILTINS_BUILTINS_MATH_H
#include "ecmascript/base/builtins_base.h"
#define BUILTIN_MATH_CONSTANTS(V) \
V(E) \
V(LN10) \
V(LN2) \
V(LOG10E) \
V(LOG2E) \
V(PI) \
V(SQRT1_2) \
V(SQRT2)
#define BUILTIN_MATH_FUNCTIONS(V) \
V("abs", Abs, 1, MathAbs) \
V("acos", Acos, 1, MathAcos) \
V("acosh", Acosh, 1, MathAcosh) \
V("asin", Asin, 1, MathAsin) \
V("asinh", Asinh, 1, MathAsinh) \
V("atan", Atan, 1, MathAtan) \
V("atan2", Atan2, 2, MathAtan2) \
V("atanh", Atanh, 1, MathAtanh) \
V("cbrt", Cbrt, 1, MathCbrt) \
V("ceil", Ceil, 1, MathCeil) \
V("clz32", Clz32, 1, MathClz32) \
V("cos", Cos, 1, MathCos) \
V("cosh", Cosh, 1, MathCosh) \
V("exp", Exp, 1, MathExp) \
V("expm1", Expm1, 1, MathExpm1) \
V("floor", Floor, 1, MathFloor) \
V("fround", Fround, 1, MathFRound) \
V("hypot", Hypot, 2, INVALID) \
V("imul", Imul, 2, MathImul) \
V("log", Log, 1, MathLog) \
V("log10", Log10, 1, MathLog10) \
V("log1p", Log1p, 1, MathLog1p) \
V("log2", Log2, 1, MathLog2) \
V("max", Max, 2, MathMax) \
V("min", Min, 2, MathMin) \
V("pow", Pow, 2, MathPow) \
V("random", Random, 0, INVALID) \
V("round", Round, 1, MathRound) \
V("sign", Sign, 1, MathSign) \
V("sin", Sin, 1, MathSin) \
V("sinh", Sinh, 1, MathSinh) \
V("sqrt", Sqrt, 1, MathSqrt) \
V("tan", Tan, 1, MathTan) \
V("tanh", Tanh, 1, MathTanh) \
V("trunc", Trunc, 1, MathTrunc)
namespace panda::ecmascript::builtins {
class BuiltinsMath : public base::BuiltinsBase {
public:
static constexpr double E = 2.718281828459045;
static constexpr double LN10 = 2.302585092994046;
static constexpr double LN2 = 0.6931471805599453;
static constexpr double LOG10E = 0.4342944819032518;
static constexpr double LOG2E = 1.4426950408889634;
static constexpr double PI = 3.141592653589793;
static constexpr double SQRT1_2 = 0.7071067811865476;
static constexpr double SQRT2 = 1.4142135623730951;
static JSTaggedValue Abs(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Acos(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Acosh(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Asin(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Asinh(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Atan(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Atanh(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Atan2(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Cbrt(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Ceil(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Clz32(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Cos(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Cosh(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Exp(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Expm1(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Floor(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Fround(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Hypot(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Imul(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Log(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Log1p(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Log10(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Log2(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Max(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Min(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Pow(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Random(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Round(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Sign(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Sin(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Sinh(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Sqrt(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Tan(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Tanh(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Trunc(EcmaRuntimeCallInfo *argv);
static Span<const base::BuiltinConstantEntry> GetMathConstants()
{
return Span<const base::BuiltinConstantEntry>(MATH_CONSTANTS);
}
static Span<const base::BuiltinFunctionEntry> GetMathFunctions()
{
return Span<const base::BuiltinFunctionEntry>(MATH_FUNCTIONS);
}
private:
#define BUILTIN_MATH_CONSTANT_ENTRY(name) \
base::BuiltinConstantEntry::Create(#name, JSTaggedValue(BuiltinsMath::name)),
static inline std::array MATH_CONSTANTS = {
BUILTIN_MATH_CONSTANTS(BUILTIN_MATH_CONSTANT_ENTRY)
};
#undef BUILTIN_MATH_CONSTANT_ENTRY
#define BUILTIN_MATH_FUNCTION_ENTRY(name, func, length, builtinId) \
base::BuiltinFunctionEntry::Create(name, BuiltinsMath::func, length, BUILTINS_STUB_ID(builtinId)),
static constexpr std::array MATH_FUNCTIONS = {
BUILTIN_MATH_FUNCTIONS(BUILTIN_MATH_FUNCTION_ENTRY)
};
#undef BUILTIN_MATH_FUNCTION_ENTRY
};
}
#endif