• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
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 
16 #ifndef ECMASCRIPT_BASE_NUMBER_HELPER_H
17 #define ECMASCRIPT_BASE_NUMBER_HELPER_H
18 
19 #include <cstdint>
20 
21 #include "ecmascript/ecma_string.h"
22 #include "ecmascript/js_tagged_value.h"
23 
24 namespace panda::ecmascript::base {
25 constexpr double MIN_RADIX = 2;
26 constexpr double MAX_RADIX = 36;
27 constexpr double MIN_FRACTION = 0;
28 constexpr double MAX_FRACTION = 100;
29 
30 // Coversion flags
31 static constexpr uint32_t NO_FLAGS = 0U;
32 static constexpr uint32_t ALLOW_BINARY = 1U << 0U;
33 static constexpr uint32_t ALLOW_OCTAL = 1U << 1U;
34 static constexpr uint32_t ALLOW_HEX = 1U << 2U;
35 static constexpr uint32_t IGNORE_TRAILING = 1U << 3U;
36 
37 static constexpr uint32_t MAX_PRECISION = 16;
38 static constexpr uint8_t BINARY = 2;
39 static constexpr uint8_t OCTAL = 8;
40 static constexpr uint8_t DECIMAL = 10;
41 static constexpr uint8_t HEXADECIMAL = 16;
42 static constexpr double HALF = 0.5;
43 static constexpr double EPSILON = std::numeric_limits<double>::epsilon();
44 static constexpr int64_t MAX_SAFE_INTEGER = 9007199254740991;
45 static constexpr double MAX_VALUE = std::numeric_limits<double>::max();
46 static constexpr double MIN_VALUE = std::numeric_limits<double>::min();
47 static constexpr double POSITIVE_INFINITY = std::numeric_limits<double>::infinity();
48 static constexpr double NAN_VALUE = std::numeric_limits<double>::quiet_NaN();
49 
50 // Helper defines for double
51 static constexpr int DOUBLE_MAX_PRECISION = 17;
52 static constexpr int DOUBLE_EXPONENT_BIAS = 0x3FF;
53 static constexpr size_t DOUBLE_SIGNIFICAND_SIZE = 52;
54 static constexpr uint64_t DOUBLE_SIGN_MASK = 0x8000000000000000ULL;
55 static constexpr uint64_t DOUBLE_EXPONENT_MASK = 0x7FFULL << DOUBLE_SIGNIFICAND_SIZE;
56 static constexpr uint64_t DOUBLE_SIGNIFICAND_MASK = 0x000FFFFFFFFFFFFFULL;
57 static constexpr uint64_t DOUBLE_HIDDEN_BIT = 1ULL << DOUBLE_SIGNIFICAND_SIZE;
58 static constexpr int32_t MINUS_ZERO_LOBITS = static_cast<int32_t>(0);
59 static constexpr int32_t MINUS_ZERO_HIBITS = static_cast<int32_t>(1) << 31;
60 static constexpr int64_t MINUS_ZERO_BITS = (static_cast<uint64_t>(MINUS_ZERO_HIBITS) << 32) | MINUS_ZERO_LOBITS;
61 
62 static constexpr size_t INT64_BITS = 64;
63 static constexpr size_t INT32_BITS = 32;
64 static constexpr size_t INT16_BITS = 16;
65 static constexpr size_t INT8_BITS = 8;
66 static constexpr size_t JS_DTOA_BUF_SIZE = 128;
67 
68 // help defines for random
69 static constexpr int LEFT52 = 52 ;
70 static constexpr int RIGHT12 = 12;
71 static constexpr uint32_t USE_LEFT = 0x3ff;
72 static constexpr int SECONDS_TO_SUBTLE = 1000000;
73 static constexpr int RIGHT27 = 27;
74 static constexpr int LEFT25 = 25;
75 static constexpr uint64_t  GET_MULTIPLY = 0x2545F4914F6CDD1D;
76 
77 class NumberHelper {
78 public:
GetNaN()79     static inline JSTaggedType GetNaN()
80     {
81         return JSTaggedValue(NAN_VALUE).GetRawData();
82     }
83 
GetPositiveInfinity()84     static inline JSTaggedType GetPositiveInfinity()
85     {
86         return JSTaggedValue(POSITIVE_INFINITY).GetRawData();
87     }
88 
IsFinite(JSTaggedValue number)89     static bool IsFinite(JSTaggedValue number)
90     {
91         return number.IsInt() || (number.IsDouble() && std::isfinite(number.GetDouble()));
92     }
IsNaN(JSTaggedValue number)93     static bool IsNaN(JSTaggedValue number)
94     {
95         return number.IsDouble() && std::isnan(number.GetDouble());
96     }
97     static JSTaggedValue DoubleToString(JSThread *thread, double number, int radix);
98     static bool IsEmptyString(const uint8_t *start, const uint8_t *end);
99     static JSHandle<EcmaString> NumberToString(const JSThread *thread, JSTaggedValue number);
100     static double TruncateDouble(double d);
101     static int64_t DoubleToInt64(double d);
102     static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix, uint32_t flags = NO_FLAGS);
103     static int32_t DoubleToInt(double d, size_t bits);
104     static int32_t DoubleInRangeInt32(double d);
105     static JSTaggedValue DoubleToExponential(JSThread *thread, double number, int digit);
106     static JSTaggedValue DoubleToFixed(JSThread *thread, double number, int digit);
107     static JSTaggedValue DoubleToPrecision(JSThread *thread, double number, int digit);
108     static JSTaggedValue StringToDoubleWithRadix(const uint8_t *start, const uint8_t *end, int radix);
109     static CString IntToString(int number);
110     static CString IntegerToString(int64_t number, int radix);
111     static JSTaggedValue StringToBigInt(JSThread *thread, JSHandle<JSTaggedValue> strVal);
112 
113 private:
114     static char Carry(char current, int radix);
115     static double Strtod(const char *str, int exponent, uint8_t radix);
116     static CString DecimalsToString(int64_t *numberInteger, double fraction, int radix, double delta);
117     static bool GotoNonspace(uint8_t **ptr, const uint8_t *end);
118     static void GetBase(double d, int digits, int *decpt, char *buf, char *bufTmp, int size);
119     static int GetMinmumDigits(double d, int *decpt, char *buf);
120 };
121 class RandomGenerator {
122 public:
123     static uint64_t& GetRandomState();
124     static uint64_t XorShift64(uint64_t *pVal);
125     static void InitRandom();
126 private:
127     static thread_local uint64_t randomState;
128 };
129 }  // namespace panda::ecmascript::base
130 #endif  // ECMASCRIPT_BASE_NUMBER_HELPER_H
131