/src/tdengine/source/libs/decimal/inc/wideInteger.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com> |
3 | | * |
4 | | * This program is free software: you can use, redistribute, and/or modify |
5 | | * it under the terms of the GNU Affero General Public License, version 3 |
6 | | * or later ("AGPL"), as published by the Free Software Foundation. |
7 | | * |
8 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
9 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
10 | | * FITNESS FOR A PARTICULAR PURPOSE. |
11 | | * |
12 | | * You should have received a copy of the GNU Affero General Public License |
13 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
14 | | */ |
15 | | |
16 | | #ifndef _TD_WIDE_INTEGER_H_ |
17 | | #define _TD_WIDE_INTEGER_H_ |
18 | | |
19 | | #include <stdbool.h> |
20 | | #include <stdint.h> |
21 | | #ifdef __cplusplus |
22 | | extern "C" { |
23 | | #endif |
24 | | |
25 | | struct uint128 { |
26 | | uint64_t low; |
27 | | uint64_t high; |
28 | | }; |
29 | | |
30 | | struct int128 { |
31 | | uint64_t low; |
32 | | int64_t high; |
33 | | }; |
34 | | |
35 | | struct uint256 { |
36 | | struct uint128 low; |
37 | | struct uint128 high; |
38 | | }; |
39 | | |
40 | | struct int256 { |
41 | | struct uint128 low; |
42 | | struct int128 high; |
43 | | }; |
44 | | |
45 | 0 | #define UInt128 struct uint128 |
46 | 0 | #define UInt256 struct uint256 |
47 | 0 | #define Int128 struct int128 |
48 | 0 | #define Int256 struct int256 |
49 | | |
50 | 0 | #define SAFE_SIGNED_OP(a, b, SIGNED_TYPE, UNSIGNED_TYPE, OP) (SIGNED_TYPE)((UNSIGNED_TYPE)(a)OP(UNSIGNED_TYPE)(b)) |
51 | 0 | #define SAFE_INT64_ADD(a, b) SAFE_SIGNED_OP(a, b, int64_t, uint64_t, +) |
52 | 0 | #define SAFE_INT64_SUBTRACT(a, b) SAFE_SIGNED_OP(a, b, int64_t, uint64_t, -) |
53 | | |
54 | | void makeUInt128(UInt128* pInt, uint64_t hi, uint64_t lo); |
55 | | uint64_t uInt128Hi(const UInt128* pInt); |
56 | | uint64_t uInt128Lo(const UInt128* pInt); |
57 | | void uInt128Add(UInt128* pLeft, const UInt128* pRight); |
58 | | void uInt128Subtract(UInt128* pLeft, const UInt128* pRight); |
59 | | void uInt128Multiply(UInt128* pLeft, const UInt128* pRight); |
60 | | void uInt128Divide(UInt128* pLeft, const UInt128* pRight); |
61 | | void uInt128Mod(UInt128* pLeft, const UInt128* pRight); |
62 | | bool uInt128Lt(const UInt128* pLeft, const UInt128* pRight); |
63 | | bool uInt128Gt(const UInt128* pLeft, const UInt128* pRight); |
64 | | bool uInt128Eq(const UInt128* pLeft, const UInt128* pRight); |
65 | | |
66 | | extern const UInt128 uInt128_1e18; |
67 | | extern const UInt128 uInt128Zero; |
68 | | extern const uint64_t k1e18; |
69 | | extern const UInt128 uInt128One; |
70 | | extern const UInt128 uInt128Two; |
71 | | |
72 | | Int128 makeInt128(int64_t high, uint64_t low); |
73 | | int64_t int128Hi(const Int128* pUint128); |
74 | | uint64_t int128Lo(const Int128* pUint128); |
75 | | Int128 int128Abs(const Int128* pInt128); |
76 | | Int128 int128Negate(const Int128* pInt128); |
77 | | Int128 int128Add(const Int128* pLeft, const Int128* pRight); |
78 | | Int128 int128Subtract(const Int128* pLeft, const Int128* pRight); |
79 | | Int128 int128Multiply(const Int128* pLeft, const Int128* pRight); |
80 | | Int128 int128Divide(const Int128* pLeft, const Int128* pRight); |
81 | | Int128 int128Mod(const Int128* pLeft, const Int128* pRight); |
82 | | bool int128Lt(const Int128* pLeft, const Int128* pRight); |
83 | | bool int128Gt(const Int128* pLeft, const Int128* pRight); |
84 | | bool int128Eq(const Int128* pLeft, const Int128* pRight); |
85 | | Int128 int128RightShift(const Int128* pLeft, int32_t shift); |
86 | | |
87 | | extern const Int128 int128Zero; |
88 | | extern const Int128 int128One; |
89 | | |
90 | | UInt256 makeUint256(UInt128 high, UInt128 low); |
91 | | UInt128 uInt256Hi(const UInt256* pUint256); |
92 | | UInt128 uInt256Lo(const UInt256* pUint256); |
93 | | UInt256 uInt256Add(const UInt256* pLeft, const UInt256* pRight); |
94 | | UInt256 uInt256Subtract(const UInt256* pLeft, const UInt256* pRight); |
95 | | UInt256 uInt256Multiply(const UInt256* pLeft, const UInt256* pRight); |
96 | | UInt256 uInt256Divide(const UInt256* pLeft, const UInt256* pRight); |
97 | | UInt256 uInt256Mod(const UInt256* pLeft, const UInt256* pRight); |
98 | | bool uInt256Lt(const UInt256* pLeft, const UInt256* pRight); |
99 | | bool uInt256Gt(const UInt256* pLeft, const UInt256* pRight); |
100 | | bool uInt256Eq(const UInt256* pLeft, const UInt256* pRight); |
101 | | UInt256 uInt256RightShift(const UInt256* pLeft, int32_t shift); |
102 | | |
103 | | extern const UInt256 uInt256Zero; |
104 | | extern const UInt256 uInt256One; |
105 | | |
106 | | Int256 makeInt256(Int128 high, UInt128 low); |
107 | | Int128 int256Hi(const Int256* pUint256); |
108 | | UInt128 int256Lo(const Int256* pUint256); |
109 | | Int256 int256Abs(const Int256* pInt256); |
110 | | Int256 int256Negate(const Int256* pInt256); |
111 | | Int256 int256Add(const Int256* pLeft, const Int256* pRight); |
112 | | Int256 int256Subtract(const Int256* pLeft, const Int256* pRight); |
113 | | Int256 int256Multiply(const Int256* pLeft, const Int256* pRight); |
114 | | Int256 int256Divide(const Int256* pLeft, const Int256* pRight); |
115 | | Int256 int256Mod(const Int256* pLeft, const Int256* pRight); |
116 | | bool int256Lt(const Int256* pLeft, const Int256* pRight); |
117 | | bool int256Gt(const Int256* pLeft, const Int256* pRight); |
118 | | bool int256Eq(const Int256* pLeft, const Int256* pRight); |
119 | | Int256 int256RightShift(const Int256* pLeft, int32_t shift); |
120 | | |
121 | | extern const Int256 int256Zero; |
122 | | extern const Int256 int256One; |
123 | | extern const Int256 int256Two; |
124 | | |
125 | | #ifdef __cplusplus |
126 | | } |
127 | | #endif |
128 | | |
129 | 0 | static inline int32_t countLeadingZeros(uint64_t v) { |
130 | 0 | #if defined(__clang__) || defined(__GNUC__) |
131 | 0 | if (v == 0) return 64; |
132 | 0 | return __builtin_clzll(v); |
133 | | #else |
134 | | int32_t bitpos = 0; |
135 | | while (v != 0) { |
136 | | v >>= 1; |
137 | | ++bitpos; |
138 | | } |
139 | | return 64 - bitpos; |
140 | | #endif |
141 | 0 | } Unexecuted instantiation: decimal.c:countLeadingZeros Unexecuted instantiation: wideInteger.cpp:countLeadingZeros(unsigned long) |
142 | | |
143 | | |
144 | | #endif /* _TD_WIDE_INTEGER_H_ */ |