opj_intmath.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef OPJ_INTMATH_H
  38. #define OPJ_INTMATH_H
  39. /**
  40. @file opj_intmath.h
  41. @brief Implementation of operations on integers (INT)
  42. The functions in OPJ_INTMATH.H have for goal to realize operations on integers.
  43. */
  44. /** @defgroup OPJ_INTMATH OPJ_INTMATH - Implementation of operations on integers */
  45. /*@{*/
  46. /** @name Exported functions (see also openjpeg.h) */
  47. /*@{*/
  48. /* ----------------------------------------------------------------------- */
  49. /**
  50. Get the minimum of two integers
  51. @return Returns a if a < b else b
  52. */
  53. static INLINE OPJ_INT32 opj_int_min(OPJ_INT32 a, OPJ_INT32 b)
  54. {
  55. return a < b ? a : b;
  56. }
  57. /**
  58. Get the minimum of two integers
  59. @return Returns a if a < b else b
  60. */
  61. static INLINE OPJ_UINT32 opj_uint_min(OPJ_UINT32 a, OPJ_UINT32 b)
  62. {
  63. return a < b ? a : b;
  64. }
  65. /**
  66. Get the maximum of two integers
  67. @return Returns a if a > b else b
  68. */
  69. static INLINE OPJ_INT32 opj_int_max(OPJ_INT32 a, OPJ_INT32 b)
  70. {
  71. return (a > b) ? a : b;
  72. }
  73. /**
  74. Get the maximum of two integers
  75. @return Returns a if a > b else b
  76. */
  77. static INLINE OPJ_UINT32 opj_uint_max(OPJ_UINT32 a, OPJ_UINT32 b)
  78. {
  79. return (a > b) ? a : b;
  80. }
  81. /**
  82. Get the saturated sum of two unsigned integers
  83. @return Returns saturated sum of a+b
  84. */
  85. static INLINE OPJ_UINT32 opj_uint_adds(OPJ_UINT32 a, OPJ_UINT32 b)
  86. {
  87. OPJ_UINT64 sum = (OPJ_UINT64)a + (OPJ_UINT64)b;
  88. return (OPJ_UINT32)(-(OPJ_INT32)(sum >> 32)) | (OPJ_UINT32)sum;
  89. }
  90. /**
  91. Get the saturated difference of two unsigned integers
  92. @return Returns saturated sum of a-b
  93. */
  94. static INLINE OPJ_UINT32 opj_uint_subs(OPJ_UINT32 a, OPJ_UINT32 b)
  95. {
  96. return (a >= b) ? a - b : 0;
  97. }
  98. /**
  99. Clamp an integer inside an interval
  100. @return
  101. <ul>
  102. <li>Returns a if (min < a < max)
  103. <li>Returns max if (a > max)
  104. <li>Returns min if (a < min)
  105. </ul>
  106. */
  107. static INLINE OPJ_INT32 opj_int_clamp(OPJ_INT32 a, OPJ_INT32 min,
  108. OPJ_INT32 max)
  109. {
  110. if (a < min) {
  111. return min;
  112. }
  113. if (a > max) {
  114. return max;
  115. }
  116. return a;
  117. }
  118. /**
  119. Clamp an integer inside an interval
  120. @return
  121. <ul>
  122. <li>Returns a if (min < a < max)
  123. <li>Returns max if (a > max)
  124. <li>Returns min if (a < min)
  125. </ul>
  126. */
  127. static INLINE OPJ_INT64 opj_int64_clamp(OPJ_INT64 a, OPJ_INT64 min,
  128. OPJ_INT64 max)
  129. {
  130. if (a < min) {
  131. return min;
  132. }
  133. if (a > max) {
  134. return max;
  135. }
  136. return a;
  137. }
  138. /**
  139. @return Get absolute value of integer
  140. */
  141. static INLINE OPJ_INT32 opj_int_abs(OPJ_INT32 a)
  142. {
  143. return a < 0 ? -a : a;
  144. }
  145. /**
  146. Divide an integer and round upwards
  147. @return Returns a divided by b
  148. */
  149. static INLINE OPJ_INT32 opj_int_ceildiv(OPJ_INT32 a, OPJ_INT32 b)
  150. {
  151. assert(b);
  152. return (OPJ_INT32)(((OPJ_INT64)a + b - 1) / b);
  153. }
  154. /**
  155. Divide an integer and round upwards
  156. @return Returns a divided by b
  157. */
  158. static INLINE OPJ_UINT32 opj_uint_ceildiv(OPJ_UINT32 a, OPJ_UINT32 b)
  159. {
  160. assert(b);
  161. return (OPJ_UINT32)(((OPJ_UINT64)a + b - 1) / b);
  162. }
  163. /**
  164. Divide an integer by a power of 2 and round upwards
  165. @return Returns a divided by 2^b
  166. */
  167. static INLINE OPJ_INT32 opj_int_ceildivpow2(OPJ_INT32 a, OPJ_INT32 b)
  168. {
  169. return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
  170. }
  171. /**
  172. Divide a 64bits integer by a power of 2 and round upwards
  173. @return Returns a divided by 2^b
  174. */
  175. static INLINE OPJ_INT32 opj_int64_ceildivpow2(OPJ_INT64 a, OPJ_INT32 b)
  176. {
  177. return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
  178. }
  179. /**
  180. Divide an integer by a power of 2 and round upwards
  181. @return Returns a divided by 2^b
  182. */
  183. static INLINE OPJ_UINT32 opj_uint_ceildivpow2(OPJ_UINT32 a, OPJ_UINT32 b)
  184. {
  185. return (OPJ_UINT32)((a + ((OPJ_UINT64)1U << b) - 1U) >> b);
  186. }
  187. /**
  188. Divide an integer by a power of 2 and round downwards
  189. @return Returns a divided by 2^b
  190. */
  191. static INLINE OPJ_INT32 opj_int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b)
  192. {
  193. return a >> b;
  194. }
  195. /**
  196. Divide an integer by a power of 2 and round downwards
  197. @return Returns a divided by 2^b
  198. */
  199. static INLINE OPJ_UINT32 opj_uint_floordivpow2(OPJ_UINT32 a, OPJ_UINT32 b)
  200. {
  201. return a >> b;
  202. }
  203. /**
  204. Get logarithm of an integer and round downwards
  205. @return Returns log2(a)
  206. */
  207. static INLINE OPJ_INT32 opj_int_floorlog2(OPJ_INT32 a)
  208. {
  209. OPJ_INT32 l;
  210. for (l = 0; a > 1; l++) {
  211. a >>= 1;
  212. }
  213. return l;
  214. }
  215. /**
  216. Get logarithm of an integer and round downwards
  217. @return Returns log2(a)
  218. */
  219. static INLINE OPJ_UINT32 opj_uint_floorlog2(OPJ_UINT32 a)
  220. {
  221. OPJ_UINT32 l;
  222. for (l = 0; a > 1; ++l) {
  223. a >>= 1;
  224. }
  225. return l;
  226. }
  227. /**
  228. Multiply two fixed-precision rational numbers.
  229. @param a
  230. @param b
  231. @return Returns a * b
  232. */
  233. static INLINE OPJ_INT32 opj_int_fix_mul(OPJ_INT32 a, OPJ_INT32 b)
  234. {
  235. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
  236. OPJ_INT64 temp = __emul(a, b);
  237. #else
  238. OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
  239. #endif
  240. temp += 4096;
  241. assert((temp >> 13) <= (OPJ_INT64)0x7FFFFFFF);
  242. assert((temp >> 13) >= (-(OPJ_INT64)0x7FFFFFFF - (OPJ_INT64)1));
  243. return (OPJ_INT32)(temp >> 13);
  244. }
  245. static INLINE OPJ_INT32 opj_int_fix_mul_t1(OPJ_INT32 a, OPJ_INT32 b)
  246. {
  247. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
  248. OPJ_INT64 temp = __emul(a, b);
  249. #else
  250. OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
  251. #endif
  252. temp += 4096;
  253. assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) <= (OPJ_INT64)0x7FFFFFFF);
  254. assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) >= (-(OPJ_INT64)0x7FFFFFFF -
  255. (OPJ_INT64)1));
  256. return (OPJ_INT32)(temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) ;
  257. }
  258. /**
  259. Addition two signed integers with a wrap-around behaviour.
  260. Assumes complement-to-two signed integers.
  261. @param a
  262. @param b
  263. @return Returns a + b
  264. */
  265. static INLINE OPJ_INT32 opj_int_add_no_overflow(OPJ_INT32 a, OPJ_INT32 b)
  266. {
  267. void* pa = &a;
  268. void* pb = &b;
  269. OPJ_UINT32* upa = (OPJ_UINT32*)pa;
  270. OPJ_UINT32* upb = (OPJ_UINT32*)pb;
  271. OPJ_UINT32 ures = *upa + *upb;
  272. void* pures = &ures;
  273. OPJ_INT32* ipres = (OPJ_INT32*)pures;
  274. return *ipres;
  275. }
  276. /**
  277. Subtract two signed integers with a wrap-around behaviour.
  278. Assumes complement-to-two signed integers.
  279. @param a
  280. @param b
  281. @return Returns a - b
  282. */
  283. static INLINE OPJ_INT32 opj_int_sub_no_overflow(OPJ_INT32 a, OPJ_INT32 b)
  284. {
  285. void* pa = &a;
  286. void* pb = &b;
  287. OPJ_UINT32* upa = (OPJ_UINT32*)pa;
  288. OPJ_UINT32* upb = (OPJ_UINT32*)pb;
  289. OPJ_UINT32 ures = *upa - *upb;
  290. void* pures = &ures;
  291. OPJ_INT32* ipres = (OPJ_INT32*)pures;
  292. return *ipres;
  293. }
  294. /* ----------------------------------------------------------------------- */
  295. /*@}*/
  296. /*@}*/
  297. #endif /* OPJ_INTMATH_H */