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
#ifdef WINDOWS
S
TD-4088  
Shengliang Guan 已提交
27

dengyihao's avatar
dengyihao 已提交
28
  #define TSWAP(a, b, c) \
S
TD-4088  
Shengliang Guan 已提交
29 30 31 32 33
    do {                \
      c __tmp = (c)(a); \
      (a) = (c)(b);     \
      (b) = __tmp;      \
    } while (0)
dengyihao's avatar
dengyihao 已提交
34 35
  #define TMAX(a, b) (((a) > (b)) ? (a) : (b))
  #define TMIN(a, b) (((a) < (b)) ? (a) : (b))
wafwerar's avatar
wafwerar 已提交
36
  #define TRANGE(aa, bb, cc) ((aa) = TMAX((aa), (bb)),(aa) = TMIN((aa), (cc)))
S
TD-4088  
Shengliang Guan 已提交
37 38 39

#else

L
Liu Jicong 已提交
40 41 42 43 44
  #define TSWAP(a, b, c)       \
    do {                       \
      __typeof(a) __tmp = (a); \
      (a) = (b);               \
      (b) = __tmp;             \
S
Shengliang Guan 已提交
45 46
    } while (0)

L
Liu Jicong 已提交
47 48 49 50 51
  #define TMAX(a, b)             \
    ({                           \
      __typeof(a) __a = (a);     \
      __typeof(b) __b = (b);     \
      (__a > __b) ? __a : __b;   \
S
Shengliang Guan 已提交
52 53
    })

L
Liu Jicong 已提交
54 55 56 57 58
  #define TMIN(a, b)             \
    ({                           \
      __typeof(a) __a = (a);     \
      __typeof(b) __b = (b);     \
      (__a < __b) ? __a : __b;   \
S
Shengliang Guan 已提交
59
    })
S
Shengliang Guan 已提交
60 61 62 63 64 65

#define TRANGE(a, b, c) \
  ({                    \
    a = TMAX(a, b);     \
    a = TMIN(a, c);     \
  })
S
Shengliang Guan 已提交
66 67 68 69 70 71
#endif

#ifdef __cplusplus
}
#endif

72
#endif /*_TD_OS_MATH_H_*/