/src/tdengine/include/os/osMath.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_OS_MATH_H_ |
17 | | #define _TD_OS_MATH_H_ |
18 | | |
19 | | #ifdef __cplusplus |
20 | | extern "C" { |
21 | | #endif |
22 | | |
23 | | // If the error is in a third-party library, place this header file under the third-party library header file. |
24 | | // When you want to use this feature, you should find or add the same function in the following sectio |
25 | | #ifndef ALLOW_FORBID_FUNC |
26 | | #define qsort QSORT_FUNC_TAOS_FORBID |
27 | | #endif |
28 | | |
29 | | #define TPOW2(x) ((x) * (x)) |
30 | 0 | #define TABS(x) ((x) > 0 ? (x) : -(x)) |
31 | | |
32 | | #ifndef TD_ASTRA |
33 | | #define TSWAP(a, b) \ |
34 | 2.03k | do { \ |
35 | 2.03k | char *__tmp = (char *)alloca(sizeof(a)); \ |
36 | 2.03k | (void)memcpy(__tmp, &(a), sizeof(a)); \ |
37 | 2.03k | (void)memcpy(&(a), &(b), sizeof(a)); \ |
38 | 2.03k | (void)memcpy(&(b), __tmp, sizeof(a)); \ |
39 | 2.03k | } while (0) |
40 | | #else |
41 | | #define TSWAP(a, b) \ |
42 | | do { \ |
43 | | char __tmp[sizeof(a)]; \ |
44 | | (void)memcpy(__tmp, &(a), sizeof(a)); \ |
45 | | (void)memcpy(&(a), &(b), sizeof(a)); \ |
46 | | (void)memcpy(&(b), __tmp, sizeof(a)); \ |
47 | | } while (0) |
48 | | #endif |
49 | | |
50 | | #ifdef WINDOWS |
51 | | |
52 | | #define TMAX(a, b) (((a) > (b)) ? (a) : (b)) |
53 | | #define TMIN(a, b) (((a) < (b)) ? (a) : (b)) |
54 | | #define TRANGE(aa, bb, cc) ((aa) = TMAX((aa), (bb)), (aa) = TMIN((aa), (cc))) |
55 | | |
56 | | #else |
57 | | |
58 | | #define TMAX(a, b) \ |
59 | 0 | ({ \ |
60 | 0 | __typeof(a) __a = (a); \ |
61 | 0 | __typeof(b) __b = (b); \ |
62 | 0 | (__a > __b) ? __a : __b; \ |
63 | 0 | }) |
64 | | |
65 | | #define TMIN(a, b) \ |
66 | 1.11M | ({ \ |
67 | 1.11M | __typeof(a) __a = (a); \ |
68 | 1.11M | __typeof(b) __b = (b); \ |
69 | 1.11M | (__a < __b) ? __a : __b; \ |
70 | 1.11M | }) |
71 | | |
72 | | #define TRANGE(a, b, c) \ |
73 | 0 | ({ \ |
74 | 0 | a = TMAX(a, b); \ |
75 | 0 | a = TMIN(a, c); \ |
76 | 0 | }) |
77 | | #endif |
78 | | |
79 | | #ifndef __COMPAR_FN_T |
80 | | #define __COMPAR_FN_T |
81 | | typedef int32_t (*__compar_fn_t)(const void *, const void *); |
82 | | #endif |
83 | | |
84 | | void taosSort(void *arr, int64_t sz, int64_t width, __compar_fn_t compar); |
85 | | |
86 | | #ifdef __cplusplus |
87 | | } |
88 | | #endif |
89 | | |
90 | | #endif /*_TD_OS_MATH_H_*/ |