ei_math.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. *------------------------------------------------------------------------------
  3. * @File : ei_math.h
  4. * @Date : 2021-3-16
  5. * @Author : lomboswer <lomboswer@lombotech.com>
  6. * @Brief : Common file for MDP(Media Development Platform).
  7. *
  8. * Copyright (C) 2020-2021, LomboTech Co.Ltd. All rights reserved.
  9. *------------------------------------------------------------------------------
  10. */
  11. #ifndef __EI_MATH_H__
  12. #define __EI_MATH_H__
  13. #ifndef __KERNEL__
  14. #include <unistd.h>
  15. #endif
  16. #include "ei_type.h"
  17. #ifdef __cplusplus
  18. #if __cplusplus
  19. extern "C" {
  20. #endif
  21. #endif /* __cplusplus */
  22. #define EI_ABS(x) ( (x) >= 0 ? (x) : (-(x)) )
  23. #define EI_SIGN(x) ( (x) >= 0 ? 1 : -1 )
  24. #define EI_CMP(x,y) (((x) == (y)) ? 0 : (((x) > (y)) ? 1 : -1))
  25. #define EI_BIT_SHIFT(v, s) ((s > 0) ? (v << s) : (v >> (-s)))
  26. #define EI_MAX2(x,y) ( (x)>(y) ? (x):(y) )
  27. #define EI_MIN2(x,y) ( (x)<(y) ? (x):(y) )
  28. #define EI_MAX3(x,y,z) ( (x)>(y) ? EI_MAX2(x,z) : EI_MAX2(y,z) )
  29. #define EI_MIN3(x,y,z) ( (x)<(y) ? EI_MIN2(x,z) : EI_MIN2(y,z) )
  30. #define EI_MEDIAN(x,y,z) (((x)+(y)+(z) - EI_MAX3(x,y,z)) - EI_MIN3(x,y,z) )
  31. #define EI_MEAN2(x,y) (((x)+(y)) >> 1 )
  32. #define EI_CONSTRAIN(x, lo, hi) (EI_MIN2(EI_MAX2((x), (lo)), (hi)))
  33. #define EI_CLIP_MIN(x,min) (((x) >= min) ? (x) : min)
  34. #define EI_CLIP3(x,min,max) ( (x)< (min) ? (min) : ((x)>(max)?(max):(x)) )
  35. #define EI_CLIP_MAX(x,max) ((x)>(max)?(max):(x))
  36. #define EI_WRAP_MAX(x,max,min) ( (x)>=(max) ? (min) : (x) )
  37. #define EI_WRAP_MIN(x,min,max) ( (x)<=(min) ? (max) : (x) )
  38. #define EI_VALUE_BETWEEN(x,min,max) (((x)>=(min)) && ((x) <= (max)))
  39. #define EI_MULTI_OF_2_POWER(x,a) (!((x) & ((a) - 1) ) )
  40. #define EI_EICEILING(x, a) (((x)+(a)-1)/(a))
  41. #define EI_ALIGN_UP(x, a) ( ( ((x) + ((a) - 1) ) / a ) * a )
  42. #define EI_ALIGN_DOWN(x, a) ( ( (x) / (a)) * (a) )
  43. #define EI_DIV_UP(x, a) ( ((x) + ((a) - 1) ) / a )
  44. #define EI_DIV_ROUND(x, a) (((x) < 0) ^ ((a) < 0)) ? (((x) - (a)/2)/(a)) : (((x) + (a)/2)/(a))
  45. #define EI_SPAN(type, begin, end)\
  46. ({ \
  47. type b = (begin); \
  48. type e = (end); \
  49. (type)((b >= e) ? (b - e) : (b + ((~((type)0))-e))); \
  50. })
  51. #define EI_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  52. #define ei_usleep(usec) \
  53. do { \
  54. usleep(usec); \
  55. } while (0)
  56. #define ei_msleep(msec) \
  57. do { \
  58. ei_usleep(msec * 1000); \
  59. } while (0)
  60. #ifdef __cplusplus
  61. #if __cplusplus
  62. }
  63. #endif
  64. #endif /* __cplusplus */
  65. #endif /* __EI_MATH_H__ */