mathops.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #if !defined(_mathops_H)
  2. # define _mathops_H (1)
  3. # include <ogg/ogg.h>
  4. # if __GNUC_PREREQ(3,4)
  5. # include <limits.h>
  6. /*Note the casts to (int) below: this prevents OC_CLZ{32|64}_OFFS from
  7. "upgrading" the type of an entire expression to an (unsigned) size_t.*/
  8. # if INT_MAX>=2147483647
  9. # define OC_CLZ32_OFFS ((int)sizeof(unsigned)*CHAR_BIT)
  10. # define OC_CLZ32(_x) (__builtin_clz(_x))
  11. # elif LONG_MAX>=2147483647L
  12. # define OC_CLZ32_OFFS ((int)sizeof(unsigned long)*CHAR_BIT)
  13. # define OC_CLZ32(_x) (__builtin_clzl(_x))
  14. # endif
  15. # if INT_MAX>=9223372036854775807LL
  16. # define OC_CLZ64_OFFS ((int)sizeof(unsigned)*CHAR_BIT)
  17. # define OC_CLZ64(_x) (__builtin_clz(_x))
  18. # elif LONG_MAX>=9223372036854775807LL
  19. # define OC_CLZ64_OFFS ((int)sizeof(unsigned long)*CHAR_BIT)
  20. # define OC_CLZ64(_x) (__builtin_clzl(_x))
  21. # elif LLONG_MAX>=9223372036854775807LL|| \
  22. __LONG_LONG_MAX__>=9223372036854775807LL
  23. # define OC_CLZ64_OFFS ((int)sizeof(unsigned long long)*CHAR_BIT)
  24. # define OC_CLZ64(_x) (__builtin_clzll(_x))
  25. # endif
  26. # endif
  27. /**
  28. * oc_ilog32 - Integer binary logarithm of a 32-bit value.
  29. * @_v: A 32-bit value.
  30. * Returns floor(log2(_v))+1, or 0 if _v==0.
  31. * This is the number of bits that would be required to represent _v in two's
  32. * complement notation with all of the leading zeros stripped.
  33. * The OC_ILOG_32() or OC_ILOGNZ_32() macros may be able to use a builtin
  34. * function instead, which should be faster.
  35. */
  36. int oc_ilog32(ogg_uint32_t _v);
  37. /**
  38. * oc_ilog64 - Integer binary logarithm of a 64-bit value.
  39. * @_v: A 64-bit value.
  40. * Returns floor(log2(_v))+1, or 0 if _v==0.
  41. * This is the number of bits that would be required to represent _v in two's
  42. * complement notation with all of the leading zeros stripped.
  43. * The OC_ILOG_64() or OC_ILOGNZ_64() macros may be able to use a builtin
  44. * function instead, which should be faster.
  45. */
  46. int oc_ilog64(ogg_int64_t _v);
  47. # if defined(OC_CLZ32)
  48. /**
  49. * OC_ILOGNZ_32 - Integer binary logarithm of a non-zero 32-bit value.
  50. * @_v: A non-zero 32-bit value.
  51. * Returns floor(log2(_v))+1.
  52. * This is the number of bits that would be required to represent _v in two's
  53. * complement notation with all of the leading zeros stripped.
  54. * If _v is zero, the return value is undefined; use OC_ILOG_32() instead.
  55. */
  56. # define OC_ILOGNZ_32(_v) (OC_CLZ32_OFFS-OC_CLZ32(_v))
  57. /**
  58. * OC_ILOG_32 - Integer binary logarithm of a 32-bit value.
  59. * @_v: A 32-bit value.
  60. * Returns floor(log2(_v))+1, or 0 if _v==0.
  61. * This is the number of bits that would be required to represent _v in two's
  62. * complement notation with all of the leading zeros stripped.
  63. */
  64. # define OC_ILOG_32(_v) (OC_ILOGNZ_32(_v)&-!!(_v))
  65. # else
  66. # define OC_ILOGNZ_32(_v) (oc_ilog32(_v))
  67. # define OC_ILOG_32(_v) (oc_ilog32(_v))
  68. # endif
  69. # if defined(CLZ64)
  70. /**
  71. * OC_ILOGNZ_64 - Integer binary logarithm of a non-zero 64-bit value.
  72. * @_v: A non-zero 64-bit value.
  73. * Returns floor(log2(_v))+1.
  74. * This is the number of bits that would be required to represent _v in two's
  75. * complement notation with all of the leading zeros stripped.
  76. * If _v is zero, the return value is undefined; use OC_ILOG_64() instead.
  77. */
  78. # define OC_ILOGNZ_64(_v) (CLZ64_OFFS-CLZ64(_v))
  79. /**
  80. * OC_ILOG_64 - Integer binary logarithm of a 64-bit value.
  81. * @_v: A 64-bit value.
  82. * Returns floor(log2(_v))+1, or 0 if _v==0.
  83. * This is the number of bits that would be required to represent _v in two's
  84. * complement notation with all of the leading zeros stripped.
  85. */
  86. # define OC_ILOG_64(_v) (OC_ILOGNZ_64(_v)&-!!(_v))
  87. # else
  88. # define OC_ILOGNZ_64(_v) (oc_ilog64(_v))
  89. # define OC_ILOG_64(_v) (oc_ilog64(_v))
  90. # endif
  91. # define OC_STATIC_ILOG0(_v) (!!(_v))
  92. # define OC_STATIC_ILOG1(_v) (((_v)&0x2)?2:OC_STATIC_ILOG0(_v))
  93. # define OC_STATIC_ILOG2(_v) \
  94. (((_v)&0xC)?2+OC_STATIC_ILOG1((_v)>>2):OC_STATIC_ILOG1(_v))
  95. # define OC_STATIC_ILOG3(_v) \
  96. (((_v)&0xF0)?4+OC_STATIC_ILOG2((_v)>>4):OC_STATIC_ILOG2(_v))
  97. # define OC_STATIC_ILOG4(_v) \
  98. (((_v)&0xFF00)?8+OC_STATIC_ILOG3((_v)>>8):OC_STATIC_ILOG3(_v))
  99. # define OC_STATIC_ILOG5(_v) \
  100. (((_v)&0xFFFF0000)?16+OC_STATIC_ILOG4((_v)>>16):OC_STATIC_ILOG4(_v))
  101. # define OC_STATIC_ILOG6(_v) \
  102. (((_v)&0xFFFFFFFF00000000ULL)?32+OC_STATIC_ILOG5((_v)>>32):OC_STATIC_ILOG5(_v))
  103. /**
  104. * OC_STATIC_ILOG_32 - The integer logarithm of an (unsigned, 32-bit) constant.
  105. * @_v: A non-negative 32-bit constant.
  106. * Returns floor(log2(_v))+1, or 0 if _v==0.
  107. * This is the number of bits that would be required to represent _v in two's
  108. * complement notation with all of the leading zeros stripped.
  109. * This macro is suitable for evaluation at compile time, but it should not be
  110. * used on values that can change at runtime, as it operates via exhaustive
  111. * search.
  112. */
  113. # define OC_STATIC_ILOG_32(_v) (OC_STATIC_ILOG5((ogg_uint32_t)(_v)))
  114. /**
  115. * OC_STATIC_ILOG_64 - The integer logarithm of an (unsigned, 64-bit) constant.
  116. * @_v: A non-negative 64-bit constant.
  117. * Returns floor(log2(_v))+1, or 0 if _v==0.
  118. * This is the number of bits that would be required to represent _v in two's
  119. * complement notation with all of the leading zeros stripped.
  120. * This macro is suitable for evaluation at compile time, but it should not be
  121. * used on values that can change at runtime, as it operates via exhaustive
  122. * search.
  123. */
  124. # define OC_STATIC_ILOG_64(_v) (OC_STATIC_ILOG6((ogg_int64_t)(_v)))
  125. #define OC_Q57(_v) ((ogg_int64_t)(_v)<<57)
  126. #define OC_Q10(_v) ((_v)<<10)
  127. ogg_int64_t oc_bexp64(ogg_int64_t _z);
  128. ogg_int64_t oc_blog64(ogg_int64_t _w);
  129. ogg_uint32_t oc_bexp32_q10(int _z);
  130. int oc_blog32_q10(ogg_uint32_t _w);
  131. #endif