2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2002 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
18 * Permission is hereby granted, free of charge, to any person (the
19 * "User") obtaining a copy of this software and associated documentation
20 * files (the "Software"), to deal in the Software without restriction,
21 * including without limitation the rights to use, copy, modify, merge,
22 * publish, distribute, and/or sell copies of the Software, and to permit
23 * persons to whom the Software is furnished to do so, subject to the
24 * following conditions:
26 * 1. The above copyright notices and this permission notice (which
27 * includes the disclaimer below) shall be included in all copies or
28 * substantial portions of the Software.
30 * 2. The name of a copyright holder shall not be used to endorse or
31 * promote products derived from the Software without specific prior
34 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
61 * __END_OF_JASPER_LICENSE__
65 * Fixed-Point Number Class
67 * $Id: jas_fix.h,v 1.1 2005/05/22 18:33:01 malaterre Exp $
73 /******************************************************************************\
75 \******************************************************************************/
81 #include <jasper/jas_config.h>
82 #include <jasper/jas_types.h>
88 /******************************************************************************\
90 \******************************************************************************/
92 /* The representation of the value zero. */
93 #define JAS_FIX_ZERO(fix_t, fracbits) \
96 /* The representation of the value one. */
97 #define JAS_FIX_ONE(fix_t, fracbits) \
98 (JAS_CAST(fix_t, 1) << (fracbits))
100 /* The representation of the value one half. */
101 #define JAS_FIX_HALF(fix_t, fracbits) \
102 (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
104 /******************************************************************************\
105 * Conversion operations.
106 \******************************************************************************/
108 /* Convert an int to a fixed-point number. */
109 #define JAS_INTTOFIX(fix_t, fracbits, x) \
110 JAS_CAST(fix_t, (x) << (fracbits))
112 /* Convert a fixed-point number to an int. */
113 #define JAS_FIXTOINT(fix_t, fracbits, x) \
114 JAS_CAST(int, (x) >> (fracbits))
116 /* Convert a fixed-point number to a double. */
117 #define JAS_FIXTODBL(fix_t, fracbits, x) \
118 (JAS_CAST(double, x) / (JAS_CAST(fix_t, 1) << (fracbits)))
120 /* Convert a double to a fixed-point number. */
121 #define JAS_DBLTOFIX(fix_t, fracbits, x) \
122 JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_CAST(fix_t, 1) << (fracbits))))
124 /******************************************************************************\
125 * Basic arithmetic operations.
126 * All other arithmetic operations are synthesized from these basic operations.
127 * There are three macros for each type of arithmetic operation.
128 * One macro always performs overflow/underflow checking, one never performs
129 * overflow/underflow checking, and one is generic with its behavior
130 * depending on compile-time flags.
131 * Only the generic macros should be invoked directly by application code.
132 \******************************************************************************/
134 /* Calculate the sum of two fixed-point numbers. */
135 #if !defined(DEBUG_OVERFLOW)
136 #define JAS_FIX_ADD JAS_FIX_ADD_FAST
138 #define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
141 /* Calculate the sum of two fixed-point numbers without overflow checking. */
142 #define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
144 /* Calculate the sum of two fixed-point numbers with overflow checking. */
145 #define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
147 (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
149 (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
152 /* Calculate the product of two fixed-point numbers. */
153 #if !defined(DEBUG_OVERFLOW)
154 #define JAS_FIX_MUL JAS_FIX_MUL_FAST
156 #define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
159 /* Calculate the product of two fixed-point numbers without overflow
161 #define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \
162 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \
165 /* Calculate the product of two fixed-point numbers with overflow
167 #define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \
168 ((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \
169 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
171 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
172 (fracbits))) : JAS_FIX_OFLOW())
174 /* Calculate the product of a fixed-point number and an int. */
175 #if !defined(DEBUG_OVERFLOW)
176 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
178 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
181 /* Calculate the product of a fixed-point number and an int without overflow
183 #define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
184 JAS_CAST(fix_t, ((x) * (y)))
186 /* Calculate the product of a fixed-point number and an int with overflow
188 #define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
189 JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
191 /* Calculate the quotient of two fixed-point numbers. */
192 #if !defined(DEBUG_OVERFLOW)
193 #define JAS_FIX_DIV JAS_FIX_DIV_FAST
195 #define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
198 /* Calculate the quotient of two fixed-point numbers without underflow
200 #define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \
201 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))
203 /* Calculate the quotient of two fixed-point numbers with underflow
205 #define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \
206 JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)
208 /* Negate a fixed-point number. */
209 #if !defined(DEBUG_OVERFLOW)
210 #define JAS_FIX_NEG JAS_FIX_NEG_FAST
212 #define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
215 /* Negate a fixed-point number without overflow checking. */
216 #define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
219 /* Negate a fixed-point number with overflow checking. */
220 /* Yes, overflow is actually possible for two's complement representations,
221 although highly unlikely to occur. */
222 #define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
223 (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
225 /* Perform an arithmetic shift left of a fixed-point number. */
226 #if !defined(DEBUG_OVERFLOW)
227 #define JAS_FIX_ASL JAS_FIX_ASL_FAST
229 #define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
232 /* Perform an arithmetic shift left of a fixed-point number without overflow
234 #define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
237 /* Perform an arithmetic shift left of a fixed-point number with overflow
239 #define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
240 ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
242 /* Perform an arithmetic shift right of a fixed-point number. */
243 #if !defined(DEBUG_OVERFLOW)
244 #define JAS_FIX_ASR JAS_FIX_ASR_FAST
246 #define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
249 /* Perform an arithmetic shift right of a fixed-point number without underflow
251 #define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
254 /* Perform an arithmetic shift right of a fixed-point number with underflow
256 #define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
257 JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
259 /******************************************************************************\
260 * Other basic arithmetic operations.
261 \******************************************************************************/
263 /* Calculate the difference between two fixed-point numbers. */
264 #define JAS_FIX_SUB(fix_t, fracbits, x, y) \
265 JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))
267 /* Add one fixed-point number to another. */
268 #define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
269 ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
271 /* Subtract one fixed-point number from another. */
272 #define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
273 ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
275 /* Multiply one fixed-point number by another. */
276 #define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
277 ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
279 /******************************************************************************\
280 * Miscellaneous operations.
281 \******************************************************************************/
283 /* Calculate the absolute value of a fixed-point number. */
284 #define JAS_FIX_ABS(fix_t, fracbits, x) \
285 (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
287 /* Is a fixed-point number an integer? */
288 #define JAS_FIX_ISINT(fix_t, fracbits, x) \
289 (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
291 /* Get the sign of a fixed-point number. */
292 #define JAS_FIX_SGN(fix_t, fracbits, x) \
293 ((x) >= 0 ? 1 : (-1))
295 /******************************************************************************\
296 * Relational operations.
297 \******************************************************************************/
299 /* Compare two fixed-point numbers. */
300 #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
301 ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
304 #define JAS_FIX_LT(fix_t, fracbits, x, y) \
307 /* Less than or equal. */
308 #define JAS_FIX_LTE(fix_t, fracbits, x, y) \
312 #define JAS_FIX_GT(fix_t, fracbits, x, y) \
315 /* Greater than or equal. */
316 #define JAS_FIX_GTE(fix_t, fracbits, x, y) \
319 /******************************************************************************\
320 * Rounding functions.
321 \******************************************************************************/
323 /* Round a fixed-point number to the nearest integer. */
324 #define JAS_FIX_ROUND(fix_t, fracbits, x) \
325 (((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \
326 (x), JAS_FIX_HALF(fix_t, fracbits))) : \
327 JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \
328 JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))
330 /* Round a fixed-point number to the nearest integer in the direction of
331 negative infinity (i.e., the floor function). */
332 #define JAS_FIX_FLOOR(fix_t, fracbits, x) \
333 ((x) & (~((JAS_CAST(fix_t, 1) << (fracbits)) - 1)))
335 /* Round a fixed-point number to the nearest integer in the direction
337 #define JAS_FIX_TRUNC(fix_t, fracbits, x) \
338 (((x) >= 0) ? JAS_FIX_FLOOR(fix_t, fracbits, x) : \
339 JAS_FIX_CEIL(fix_t, fracbits, x))
341 /******************************************************************************\
342 * The below macros are for internal library use only. Do not invoke them
343 * directly in application code.
344 \******************************************************************************/
346 /* Handle overflow. */
347 #define JAS_FIX_OFLOW() \
348 fprintf(stderr, "overflow error: file %s, line %d\n", __FILE__, __LINE__)
350 /* Handle underflow. */
351 #define JAS_FIX_UFLOW() \
352 fprintf(stderr, "underflow error: file %s, line %d\n", __FILE__, __LINE__)