• 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 static constexpr uint64_t MAX_UINT64_VALUE = std::numeric_limits<uint64_t>::max();
50 
51 // Helper defines for double
52 static constexpr int DOUBLE_MAX_PRECISION = 17;
53 static constexpr int DOUBLE_EXPONENT_BIAS = 0x3FF;
54 static constexpr size_t DOUBLE_SIGNIFICAND_SIZE = 52;
55 static constexpr uint64_t DOUBLE_SIGN_MASK = 0x8000000000000000ULL;
56 static constexpr uint64_t DOUBLE_EXPONENT_MASK = 0x7FFULL << DOUBLE_SIGNIFICAND_SIZE;
57 static constexpr uint64_t DOUBLE_SIGNIFICAND_MASK = 0x000FFFFFFFFFFFFFULL;
58 static constexpr uint64_t DOUBLE_HIDDEN_BIT = 1ULL << DOUBLE_SIGNIFICAND_SIZE;
59 static constexpr int32_t MINUS_ZERO_LOBITS = static_cast<int32_t>(0);
60 static constexpr int32_t MINUS_ZERO_HIBITS = static_cast<int32_t>(1) << 31;
61 static constexpr int64_t MINUS_ZERO_BITS = (static_cast<uint64_t>(MINUS_ZERO_HIBITS) << 32) | MINUS_ZERO_LOBITS;
62 
63 static constexpr size_t INT64_BITS = 64;
64 static constexpr size_t INT32_BITS = 32;
65 static constexpr size_t INT16_BITS = 16;
66 static constexpr size_t INT8_BITS = 8;
67 static constexpr size_t JS_DTOA_BUF_SIZE = 128;
68 
69 // help defines for random
70 static constexpr int RIGHT12 = 12;
71 static constexpr int SECONDS_TO_SUBTLE = 1000000;
72 static constexpr int RIGHT27 = 27;
73 static constexpr int LEFT25 = 25;
74 static constexpr uint64_t GET_MULTIPLY = 0x2545F4914F6CDD1D;
75 // Exponent bits for double value between [1.0, 2.0)
76 static constexpr uint64_t EXPONENTBITS_RANGE_IN_ONE_AND_TWO = 0x3FF0000000000000;
77 
78 class NumberHelper {
79 public:
GetNaN()80     static inline JSTaggedType GetNaN()
81     {
82         return JSTaggedValue(NAN_VALUE).GetRawData();
83     }
84 
GetPositiveInfinity()85     static inline JSTaggedType GetPositiveInfinity()
86     {
87         return JSTaggedValue(POSITIVE_INFINITY).GetRawData();
88     }
89 
IsFinite(JSTaggedValue number)90     static bool IsFinite(JSTaggedValue number)
91     {
92         return number.IsInt() || (number.IsDouble() && std::isfinite(number.GetDouble()));
93     }
IsNaN(JSTaggedValue number)94     static bool IsNaN(JSTaggedValue number)
95     {
96         return number.IsDouble() && std::isnan(number.GetDouble());
97     }
98     static JSTaggedValue DoubleToString(JSThread *thread, double number, int radix);
99     static bool IsEmptyString(const uint8_t *start, const uint8_t *end);
100     static JSHandle<EcmaString> IntToEcmaString(const JSThread *thread, int number);
101     static JSHandle<EcmaString> NumberToString(const JSThread *thread, JSTaggedValue number);
102     static double TruncateDouble(double d);
103     static int64_t DoubleToInt64(double d);
104     static double StringToDouble(const uint8_t *start, const uint8_t *end, uint8_t radix, uint32_t flags = NO_FLAGS);
105     static int32_t DoubleToInt(double d, size_t bits);
106     static int32_t DoubleInRangeInt32(double d);
107     static JSTaggedValue DoubleToExponential(JSThread *thread, double number, int digit);
108     static JSTaggedValue DoubleToFixed(JSThread *thread, double number, int digit);
109     static JSTaggedValue DoubleToPrecision(JSThread *thread, double number, int digit);
110     static JSTaggedValue StringToDoubleWithRadix(const uint8_t *start, const uint8_t *end, int radix);
111     static CString IntToString(int number);
112     static CString IntegerToString(double number, int radix);
113     static JSTaggedValue StringToBigInt(JSThread *thread, JSHandle<JSTaggedValue> strVal);
114 
115 private:
116     static char Carry(char current, int radix);
117     static double Strtod(const char *str, int exponent, uint8_t radix);
118     static CString DecimalsToString(double *numberInteger, double fraction, int radix, double delta);
119     static bool GotoNonspace(uint8_t **ptr, const uint8_t *end);
120     static void GetBase(double d, int digits, int *decpt, char *buf, char *bufTmp, int size);
121     static int GetMinmumDigits(double d, int *decpt, char *buf);
122 };
123 
124 // This class is used to generate 0~1 uniform distribution pseudo-random numbers.
125 // It uses a 64-bit seed which is current timestamp to generate state value.
126 // The value is used in xorshift64* random generator to generate result.
127 class RandomGenerator {
128 public:
129     static void InitRandom();
130     static double NextDouble();
131     static int32_t GenerateIdentityHash();
132     static int32_t Next(int bits);
133 
134 private:
135     static uint64_t XorShift64(uint64_t *pVal);
136     static double ToDouble(uint64_t state);
137 
138 private:
139     static thread_local uint64_t randomState_;
140 };
141 }  // namespace panda::ecmascript::base
142 #endif  // ECMASCRIPT_BASE_NUMBER_HELPER_H
143