jas_fix.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2002 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * Fixed-Point Number Class
  64. *
  65. * $Id: jas_fix.h,v 1.2 2008-05-26 09:41:51 vp153 Exp $
  66. */
  67. #ifndef JAS_FIX_H
  68. #define JAS_FIX_H
  69. /******************************************************************************\
  70. * Includes.
  71. \******************************************************************************/
  72. #include <stdio.h>
  73. #include <stdlib.h>
  74. #include <math.h>
  75. #include <jasper/jas_config.h>
  76. #include <jasper/jas_types.h>
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. /******************************************************************************\
  81. * Constants.
  82. \******************************************************************************/
  83. /* The representation of the value zero. */
  84. #define JAS_FIX_ZERO(fix_t, fracbits) \
  85. JAS_CAST(fix_t, 0)
  86. /* The representation of the value one. */
  87. #define JAS_FIX_ONE(fix_t, fracbits) \
  88. (JAS_CAST(fix_t, 1) << (fracbits))
  89. /* The representation of the value one half. */
  90. #define JAS_FIX_HALF(fix_t, fracbits) \
  91. (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
  92. /******************************************************************************\
  93. * Conversion operations.
  94. \******************************************************************************/
  95. /* Convert an int to a fixed-point number. */
  96. #define JAS_INTTOFIX(fix_t, fracbits, x) \
  97. JAS_CAST(fix_t, (x) << (fracbits))
  98. /* Convert a fixed-point number to an int. */
  99. #define JAS_FIXTOINT(fix_t, fracbits, x) \
  100. JAS_CAST(int, (x) >> (fracbits))
  101. /* Convert a fixed-point number to a double. */
  102. #define JAS_FIXTODBL(fix_t, fracbits, x) \
  103. (JAS_CAST(double, x) / (JAS_CAST(fix_t, 1) << (fracbits)))
  104. /* Convert a double to a fixed-point number. */
  105. #define JAS_DBLTOFIX(fix_t, fracbits, x) \
  106. JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_CAST(fix_t, 1) << (fracbits))))
  107. /******************************************************************************\
  108. * Basic arithmetic operations.
  109. * All other arithmetic operations are synthesized from these basic operations.
  110. * There are three macros for each type of arithmetic operation.
  111. * One macro always performs overflow/underflow checking, one never performs
  112. * overflow/underflow checking, and one is generic with its behavior
  113. * depending on compile-time flags.
  114. * Only the generic macros should be invoked directly by application code.
  115. \******************************************************************************/
  116. /* Calculate the sum of two fixed-point numbers. */
  117. #if !defined(DEBUG_OVERFLOW)
  118. #define JAS_FIX_ADD JAS_FIX_ADD_FAST
  119. #else
  120. #define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
  121. #endif
  122. /* Calculate the sum of two fixed-point numbers without overflow checking. */
  123. #define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
  124. /* Calculate the sum of two fixed-point numbers with overflow checking. */
  125. #define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
  126. ((x) >= 0) ? \
  127. (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
  128. ((x) + (y))) : \
  129. (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
  130. (x) + (y)))
  131. /* Calculate the product of two fixed-point numbers. */
  132. #if !defined(DEBUG_OVERFLOW)
  133. #define JAS_FIX_MUL JAS_FIX_MUL_FAST
  134. #else
  135. #define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
  136. #endif
  137. /* Calculate the product of two fixed-point numbers without overflow
  138. checking. */
  139. #define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \
  140. JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \
  141. (fracbits))
  142. /* Calculate the product of two fixed-point numbers with overflow
  143. checking. */
  144. #define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \
  145. ((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \
  146. JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
  147. (fracbits))) ? \
  148. JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
  149. (fracbits))) : JAS_FIX_OFLOW())
  150. /* Calculate the product of a fixed-point number and an int. */
  151. #if !defined(DEBUG_OVERFLOW)
  152. #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
  153. #else
  154. #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
  155. #endif
  156. /* Calculate the product of a fixed-point number and an int without overflow
  157. checking. */
  158. #define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
  159. JAS_CAST(fix_t, ((x) * (y)))
  160. /* Calculate the product of a fixed-point number and an int with overflow
  161. checking. */
  162. #define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
  163. JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
  164. /* Calculate the quotient of two fixed-point numbers. */
  165. #if !defined(DEBUG_OVERFLOW)
  166. #define JAS_FIX_DIV JAS_FIX_DIV_FAST
  167. #else
  168. #define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
  169. #endif
  170. /* Calculate the quotient of two fixed-point numbers without underflow
  171. checking. */
  172. #define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \
  173. JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))
  174. /* Calculate the quotient of two fixed-point numbers with underflow
  175. checking. */
  176. #define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \
  177. JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)
  178. /* Negate a fixed-point number. */
  179. #if !defined(DEBUG_OVERFLOW)
  180. #define JAS_FIX_NEG JAS_FIX_NEG_FAST
  181. #else
  182. #define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
  183. #endif
  184. /* Negate a fixed-point number without overflow checking. */
  185. #define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
  186. (-(x))
  187. /* Negate a fixed-point number with overflow checking. */
  188. /* Yes, overflow is actually possible for two's complement representations,
  189. although highly unlikely to occur. */
  190. #define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
  191. (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
  192. /* Perform an arithmetic shift left of a fixed-point number. */
  193. #if !defined(DEBUG_OVERFLOW)
  194. #define JAS_FIX_ASL JAS_FIX_ASL_FAST
  195. #else
  196. #define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
  197. #endif
  198. /* Perform an arithmetic shift left of a fixed-point number without overflow
  199. checking. */
  200. #define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
  201. ((x) << (n))
  202. /* Perform an arithmetic shift left of a fixed-point number with overflow
  203. checking. */
  204. #define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
  205. ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
  206. /* Perform an arithmetic shift right of a fixed-point number. */
  207. #if !defined(DEBUG_OVERFLOW)
  208. #define JAS_FIX_ASR JAS_FIX_ASR_FAST
  209. #else
  210. #define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
  211. #endif
  212. /* Perform an arithmetic shift right of a fixed-point number without underflow
  213. checking. */
  214. #define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
  215. ((x) >> (n))
  216. /* Perform an arithmetic shift right of a fixed-point number with underflow
  217. checking. */
  218. #define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
  219. JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
  220. /******************************************************************************\
  221. * Other basic arithmetic operations.
  222. \******************************************************************************/
  223. /* Calculate the difference between two fixed-point numbers. */
  224. #define JAS_FIX_SUB(fix_t, fracbits, x, y) \
  225. JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))
  226. /* Add one fixed-point number to another. */
  227. #define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
  228. ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
  229. /* Subtract one fixed-point number from another. */
  230. #define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
  231. ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
  232. /* Multiply one fixed-point number by another. */
  233. #define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
  234. ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
  235. /******************************************************************************\
  236. * Miscellaneous operations.
  237. \******************************************************************************/
  238. /* Calculate the absolute value of a fixed-point number. */
  239. #define JAS_FIX_ABS(fix_t, fracbits, x) \
  240. (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
  241. /* Is a fixed-point number an integer? */
  242. #define JAS_FIX_ISINT(fix_t, fracbits, x) \
  243. (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
  244. /* Get the sign of a fixed-point number. */
  245. #define JAS_FIX_SGN(fix_t, fracbits, x) \
  246. ((x) >= 0 ? 1 : (-1))
  247. /******************************************************************************\
  248. * Relational operations.
  249. \******************************************************************************/
  250. /* Compare two fixed-point numbers. */
  251. #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
  252. ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
  253. /* Less than. */
  254. #define JAS_FIX_LT(fix_t, fracbits, x, y) \
  255. ((x) < (y))
  256. /* Less than or equal. */
  257. #define JAS_FIX_LTE(fix_t, fracbits, x, y) \
  258. ((x) <= (y))
  259. /* Greater than. */
  260. #define JAS_FIX_GT(fix_t, fracbits, x, y) \
  261. ((x) > (y))
  262. /* Greater than or equal. */
  263. #define JAS_FIX_GTE(fix_t, fracbits, x, y) \
  264. ((x) >= (y))
  265. /******************************************************************************\
  266. * Rounding functions.
  267. \******************************************************************************/
  268. /* Round a fixed-point number to the nearest integer. */
  269. #define JAS_FIX_ROUND(fix_t, fracbits, x) \
  270. (((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \
  271. (x), JAS_FIX_HALF(fix_t, fracbits))) : \
  272. JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \
  273. JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))
  274. /* Round a fixed-point number to the nearest integer in the direction of
  275. negative infinity (i.e., the floor function). */
  276. #define JAS_FIX_FLOOR(fix_t, fracbits, x) \
  277. ((x) & (~((JAS_CAST(fix_t, 1) << (fracbits)) - 1)))
  278. /* Round a fixed-point number to the nearest integer in the direction
  279. of zero. */
  280. #define JAS_FIX_TRUNC(fix_t, fracbits, x) \
  281. (((x) >= 0) ? JAS_FIX_FLOOR(fix_t, fracbits, x) : \
  282. JAS_FIX_CEIL(fix_t, fracbits, x))
  283. /******************************************************************************\
  284. * The below macros are for internal library use only. Do not invoke them
  285. * directly in application code.
  286. \******************************************************************************/
  287. /* Handle overflow. */
  288. #define JAS_FIX_OFLOW() \
  289. jas_eprintf("overflow error: file %s, line %d\n", __FILE__, __LINE__)
  290. /* Handle underflow. */
  291. #define JAS_FIX_UFLOW() \
  292. jas_eprintf("underflow error: file %s, line %d\n", __FILE__, __LINE__)
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. #endif