osMath.h 1.8 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16 17
#ifndef _TD_OS_MATH_H_
#define _TD_OS_MATH_H_
S
Shengliang Guan 已提交
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

dengyihao's avatar
dengyihao 已提交
23
#define TPOW2(x) ((x) * (x))
dengyihao's avatar
dengyihao 已提交
24
#define TABS(x) ((x) > 0 ? (x) : -(x))
S
Shengliang Guan 已提交
25

wafwerar's avatar
wafwerar 已提交
26 27
#define TSWAP(a, b)                              \
  do {                                           \
wafwerar's avatar
wafwerar 已提交
28
    char *__tmp = alloca(sizeof(a));             \
wafwerar's avatar
wafwerar 已提交
29 30 31
    memcpy(__tmp, &(a), sizeof(a));              \
    memcpy(&(a), &(b), sizeof(a));               \
    memcpy(&(b), __tmp, sizeof(a));              \
wafwerar's avatar
wafwerar 已提交
32 33
  } while (0)

wafwerar's avatar
wafwerar 已提交
34
#ifdef WINDOWS
S
TD-4088  
Shengliang Guan 已提交
35

dengyihao's avatar
dengyihao 已提交
36 37
  #define TMAX(a, b) (((a) > (b)) ? (a) : (b))
  #define TMIN(a, b) (((a) < (b)) ? (a) : (b))
wafwerar's avatar
wafwerar 已提交
38
  #define TRANGE(aa, bb, cc) ((aa) = TMAX((aa), (bb)),(aa) = TMIN((aa), (cc)))
S
TD-4088  
Shengliang Guan 已提交
39 40 41

#else

L
Liu Jicong 已提交
42 43 44 45 46
  #define TMAX(a, b)             \
    ({                           \
      __typeof(a) __a = (a);     \
      __typeof(b) __b = (b);     \
      (__a > __b) ? __a : __b;   \
S
Shengliang Guan 已提交
47 48
    })

wafwerar's avatar
wafwerar 已提交
49 50 51 52 53 54
#define TMIN(a, b)             \
  ({                           \
    __typeof(a) __a = (a);     \
    __typeof(b) __b = (b);     \
    (__a < __b) ? __a : __b;   \
  })
S
Shengliang Guan 已提交
55 56 57 58 59 60

#define TRANGE(a, b, c) \
  ({                    \
    a = TMAX(a, b);     \
    a = TMIN(a, c);     \
  })
S
Shengliang Guan 已提交
61 62 63 64 65 66
#endif

#ifdef __cplusplus
}
#endif

67
#endif /*_TD_OS_MATH_H_*/