opj_intmath.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 and round upwards
  165. @return Returns a divided by b
  166. */
  167. static INLINE OPJ_UINT32 opj_uint64_ceildiv_res_uint32(OPJ_UINT64 a,
  168. OPJ_UINT64 b)
  169. {
  170. assert(b);
  171. return (OPJ_UINT32)((a + b - 1) / b);
  172. }
  173. /**
  174. Divide an integer by a power of 2 and round upwards
  175. @return Returns a divided by 2^b
  176. */
  177. static INLINE OPJ_INT32 opj_int_ceildivpow2(OPJ_INT32 a, OPJ_INT32 b)
  178. {
  179. return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
  180. }
  181. /**
  182. Divide a 64bits integer by a power of 2 and round upwards
  183. @return Returns a divided by 2^b
  184. */
  185. static INLINE OPJ_INT32 opj_int64_ceildivpow2(OPJ_INT64 a, OPJ_INT32 b)
  186. {
  187. return (OPJ_INT32)((a + ((OPJ_INT64)1 << b) - 1) >> b);
  188. }
  189. /**
  190. Divide an integer by a power of 2 and round upwards
  191. @return Returns a divided by 2^b
  192. */
  193. static INLINE OPJ_UINT32 opj_uint_ceildivpow2(OPJ_UINT32 a, OPJ_UINT32 b)
  194. {
  195. return (OPJ_UINT32)((a + ((OPJ_UINT64)1U << b) - 1U) >> b);
  196. }
  197. /**
  198. Divide an integer by a power of 2 and round downwards
  199. @return Returns a divided by 2^b
  200. */
  201. static INLINE OPJ_INT32 opj_int_floordivpow2(OPJ_INT32 a, OPJ_INT32 b)
  202. {
  203. return a >> b;
  204. }
  205. /**
  206. Divide an integer by a power of 2 and round downwards
  207. @return Returns a divided by 2^b
  208. */
  209. static INLINE OPJ_UINT32 opj_uint_floordivpow2(OPJ_UINT32 a, OPJ_UINT32 b)
  210. {
  211. return a >> b;
  212. }
  213. /**
  214. Get logarithm of an integer and round downwards
  215. @return Returns log2(a)
  216. */
  217. static INLINE OPJ_INT32 opj_int_floorlog2(OPJ_INT32 a)
  218. {
  219. OPJ_INT32 l;
  220. for (l = 0; a > 1; l++) {
  221. a >>= 1;
  222. }
  223. return l;
  224. }
  225. /**
  226. Get logarithm of an integer and round downwards
  227. @return Returns log2(a)
  228. */
  229. static INLINE OPJ_UINT32 opj_uint_floorlog2(OPJ_UINT32 a)
  230. {
  231. OPJ_UINT32 l;
  232. for (l = 0; a > 1; ++l) {
  233. a >>= 1;
  234. }
  235. return l;
  236. }
  237. /**
  238. Multiply two fixed-precision rational numbers.
  239. @param a
  240. @param b
  241. @return Returns a * b
  242. */
  243. static INLINE OPJ_INT32 opj_int_fix_mul(OPJ_INT32 a, OPJ_INT32 b)
  244. {
  245. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
  246. OPJ_INT64 temp = __emul(a, b);
  247. #else
  248. OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
  249. #endif
  250. temp += 4096;
  251. assert((temp >> 13) <= (OPJ_INT64)0x7FFFFFFF);
  252. assert((temp >> 13) >= (-(OPJ_INT64)0x7FFFFFFF - (OPJ_INT64)1));
  253. return (OPJ_INT32)(temp >> 13);
  254. }
  255. static INLINE OPJ_INT32 opj_int_fix_mul_t1(OPJ_INT32 a, OPJ_INT32 b)
  256. {
  257. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__INTEL_COMPILER) && defined(_M_IX86)
  258. OPJ_INT64 temp = __emul(a, b);
  259. #else
  260. OPJ_INT64 temp = (OPJ_INT64) a * (OPJ_INT64) b ;
  261. #endif
  262. temp += 4096;
  263. assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) <= (OPJ_INT64)0x7FFFFFFF);
  264. assert((temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) >= (-(OPJ_INT64)0x7FFFFFFF -
  265. (OPJ_INT64)1));
  266. return (OPJ_INT32)(temp >> (13 + 11 - T1_NMSEDEC_FRACBITS)) ;
  267. }
  268. /**
  269. Addition two signed integers with a wrap-around behaviour.
  270. Assumes complement-to-two signed integers.
  271. @param a
  272. @param b
  273. @return Returns a + b
  274. */
  275. static INLINE OPJ_INT32 opj_int_add_no_overflow(OPJ_INT32 a, OPJ_INT32 b)
  276. {
  277. void* pa = &a;
  278. void* pb = &b;
  279. OPJ_UINT32* upa = (OPJ_UINT32*)pa;
  280. OPJ_UINT32* upb = (OPJ_UINT32*)pb;
  281. OPJ_UINT32 ures = *upa + *upb;
  282. void* pures = &ures;
  283. OPJ_INT32* ipres = (OPJ_INT32*)pures;
  284. return *ipres;
  285. }
  286. /**
  287. Subtract two signed integers with a wrap-around behaviour.
  288. Assumes complement-to-two signed integers.
  289. @param a
  290. @param b
  291. @return Returns a - b
  292. */
  293. static INLINE OPJ_INT32 opj_int_sub_no_overflow(OPJ_INT32 a, OPJ_INT32 b)
  294. {
  295. void* pa = &a;
  296. void* pb = &b;
  297. OPJ_UINT32* upa = (OPJ_UINT32*)pa;
  298. OPJ_UINT32* upb = (OPJ_UINT32*)pb;
  299. OPJ_UINT32 ures = *upa - *upb;
  300. void* pures = &ures;
  301. OPJ_INT32* ipres = (OPJ_INT32*)pures;
  302. return *ipres;
  303. }
  304. /* ----------------------------------------------------------------------- */
  305. /*@}*/
  306. /*@}*/
  307. #endif /* OPJ_INTMATH_H */