My Project 2.0.16 |

jas_fix.h
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
8 /* __START_OF_JASPER_LICENSE__
9 *
10 * JasPer License Version 2.0
11 *
12 * Copyright (c) 2001-2006 Michael David Adams
13 * Copyright (c) 1999-2000 Image Power, Inc.
14 * Copyright (c) 1999-2000 The University of British Columbia
15 *
16 * All rights reserved.
17 *
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:
25 *
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.
29 *
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
32 * written permission.
33 *
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.
60 *
61 * __END_OF_JASPER_LICENSE__
62 */
63
64 /*
65 * Fixed-Point Number Class
66 *
67 * $Id$
68 */
69
70 #ifndef JAS_FIX_H
71 #define JAS_FIX_H
72
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76
77 /* The configuration header file should be included first. */
78 #include <jasper/jas_config.h>
79
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <math.h>
83
84 #include <jasper/jas_types.h>
85 #include <jasper/jas_debug.h>
86
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90
91 /******************************************************************************\
92 * Constants.
93 \******************************************************************************/
94
95 /* The representation of the value zero. */
96 #define JAS_FIX_ZERO(fix_t, fracbits) \
97 JAS_CAST(fix_t, 0)
98
99 /* The representation of the value one. */
100 #define JAS_FIX_ONE(fix_t, fracbits) \
101 (JAS_CAST(fix_t, 1) << (fracbits))
102
103 /* The representation of the value one half. */
104 #define JAS_FIX_HALF(fix_t, fracbits) \
105 (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
106
107 /******************************************************************************\
108 * Conversion operations.
109 \******************************************************************************/
110
111 /* Convert an int to a fixed-point number. */
112 #define JAS_INTTOFIX(fix_t, fracbits, x) \
113 JAS_CAST(fix_t, (x) << (fracbits))
114
115 /* Convert a fixed-point number to an int. */
116 #define JAS_FIXTOINT(fix_t, fracbits, x) \
117 JAS_CAST(int, (x) >> (fracbits))
118
119 /* Convert a fixed-point number to a double. */
120 #define JAS_FIXTODBL(fix_t, fracbits, x) \
121 (JAS_CAST(double, x) / (JAS_CAST(fix_t, 1) << (fracbits)))
122
123 /* Convert a double to a fixed-point number. */
124 #define JAS_DBLTOFIX(fix_t, fracbits, x) \
125 JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_CAST(fix_t, 1) << (fracbits))))
126
127 /******************************************************************************\
128 * Basic arithmetic operations.
129 * All other arithmetic operations are synthesized from these basic operations.
130 * There are three macros for each type of arithmetic operation.
131 * One macro always performs overflow/underflow checking, one never performs
132 * overflow/underflow checking, and one is generic with its behavior
133 * depending on compile-time flags.
134 * Only the generic macros should be invoked directly by application code.
135 \******************************************************************************/
136
137 /* Calculate the sum of two fixed-point numbers. */
138 #if !defined(DEBUG_OVERFLOW)
139 #define JAS_FIX_ADD JAS_FIX_ADD_FAST
140 #else
141 #define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
142 #endif
143
144 /* Calculate the sum of two fixed-point numbers without overflow checking. */
145 #define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
146
147 /* Calculate the sum of two fixed-point numbers with overflow checking. */
148 #define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
149 ((x) >= 0) ? \
150 (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
151 ((x) + (y))) : \
152 (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
153 (x) + (y)))
154
155 /* Calculate the product of two fixed-point numbers. */
156 #if !defined(DEBUG_OVERFLOW)
157 #define JAS_FIX_MUL JAS_FIX_MUL_FAST
158 #else
159 #define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
160 #endif
161
162 /* Calculate the product of two fixed-point numbers without overflow
163 checking. */
164 #define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \
165 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \
166 (fracbits))
167
168 /* Calculate the product of two fixed-point numbers with overflow
169 checking. */
170 #define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \
171 ((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \
172 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
173 (fracbits))) ? \
174 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
175 (fracbits))) : JAS_FIX_OFLOW())
176
177 /* Calculate the product of a fixed-point number and an int. */
178 #if !defined(DEBUG_OVERFLOW)
179 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
180 #else
181 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
182 #endif
183
184 /* Calculate the product of a fixed-point number and an int without overflow
185 checking. */
186 #define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
187 JAS_CAST(fix_t, ((x) * (y)))
188
189 /* Calculate the product of a fixed-point number and an int with overflow
190 checking. */
191 #define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
192 JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
193
194 /* Calculate the quotient of two fixed-point numbers. */
195 #if !defined(DEBUG_OVERFLOW)
196 #define JAS_FIX_DIV JAS_FIX_DIV_FAST
197 #else
198 #define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
199 #endif
200
201 /* Calculate the quotient of two fixed-point numbers without underflow
202 checking. */
203 #define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \
204 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))
205
206 /* Calculate the quotient of two fixed-point numbers with underflow
207 checking. */
208 #define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \
209 JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)
210
211 /* Negate a fixed-point number. */
212 #if !defined(DEBUG_OVERFLOW)
213 #define JAS_FIX_NEG JAS_FIX_NEG_FAST
214 #else
215 #define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
216 #endif
217
218 /* Negate a fixed-point number without overflow checking. */
219 #define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
220 (-(x))
221
222 /* Negate a fixed-point number with overflow checking. */
223 /* Yes, overflow is actually possible for two's complement representations,
224 although highly unlikely to occur. */
225 #define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
226 (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
227
228 /* Perform an arithmetic shift left of a fixed-point number. */
229 #if !defined(DEBUG_OVERFLOW)
230 #define JAS_FIX_ASL JAS_FIX_ASL_FAST
231 #else
232 #define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
233 #endif
234
235 /* Perform an arithmetic shift left of a fixed-point number without overflow
236 checking. */
237 #define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
238 ((x) << (n))
239
240 /* Perform an arithmetic shift left of a fixed-point number with overflow
241 checking. */
242 #define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
243 ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
244
245 /* Perform an arithmetic shift right of a fixed-point number. */
246 #if !defined(DEBUG_OVERFLOW)
247 #define JAS_FIX_ASR JAS_FIX_ASR_FAST
248 #else
249 #define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
250 #endif
251
252 /* Perform an arithmetic shift right of a fixed-point number without underflow
253 checking. */
254 #define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
255 ((x) >> (n))
256
257 /* Perform an arithmetic shift right of a fixed-point number with underflow
258 checking. */
259 #define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
260 JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
261
262 /******************************************************************************\
263 * Other basic arithmetic operations.
264 \******************************************************************************/
265
266 /* Calculate the difference between two fixed-point numbers. */
267 #define JAS_FIX_SUB(fix_t, fracbits, x, y) \
268 JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))
269
270 /* Add one fixed-point number to another. */
271 #define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
272 ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
273
274 /* Subtract one fixed-point number from another. */
275 #define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
276 ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
277
278 /* Multiply one fixed-point number by another. */
279 #define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
280 ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
281
282 /******************************************************************************\
283 * Miscellaneous operations.
284 \******************************************************************************/
285
286 /* Calculate the absolute value of a fixed-point number. */
287 #define JAS_FIX_ABS(fix_t, fracbits, x) \
288 (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
289
290 /* Is a fixed-point number an integer? */
291 #define JAS_FIX_ISINT(fix_t, fracbits, x) \
292 (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
293
294 /* Get the sign of a fixed-point number. */
295 #define JAS_FIX_SGN(fix_t, fracbits, x) \
296 ((x) >= 0 ? 1 : (-1))
297
298 /******************************************************************************\
299 * Relational operations.
300 \******************************************************************************/
301
302 /* Compare two fixed-point numbers. */
303 #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
304 ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
305
306 /* Less than. */
307 #define JAS_FIX_LT(fix_t, fracbits, x, y) \
308 ((x) < (y))
309
310 /* Less than or equal. */
311 #define JAS_FIX_LTE(fix_t, fracbits, x, y) \
312 ((x) <= (y))
313
314 /* Greater than. */
315 #define JAS_FIX_GT(fix_t, fracbits, x, y) \
316 ((x) > (y))
317
318 /* Greater than or equal. */
319 #define JAS_FIX_GTE(fix_t, fracbits, x, y) \
320 ((x) >= (y))
321
322 /******************************************************************************\
323 * Rounding functions.
324 \******************************************************************************/
325
326 /* Round a fixed-point number to the nearest integer. */
327 #define JAS_FIX_ROUND(fix_t, fracbits, x) \
328 (((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \
329 (x), JAS_FIX_HALF(fix_t, fracbits))) : \
330 JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \
331 JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))
332
333 /* Round a fixed-point number to the nearest integer in the direction of
334 negative infinity (i.e., the floor function). */
335 #define JAS_FIX_FLOOR(fix_t, fracbits, x) \
336 ((x) & (~((JAS_CAST(fix_t, 1) << (fracbits)) - 1)))
337
338 /* Round a fixed-point number to the nearest integer in the direction
339 of zero. */
340 #define JAS_FIX_TRUNC(fix_t, fracbits, x) \
341 (((x) >= 0) ? JAS_FIX_FLOOR(fix_t, fracbits, x) : \
342 JAS_FIX_CEIL(fix_t, fracbits, x))
343
344 /******************************************************************************\
345 * The below macros are for internal library use only. Do not invoke them
346 * directly in application code.
347 \******************************************************************************/
348
349 /* Handle overflow. */
350 #define JAS_FIX_OFLOW() \
351 jas_eprintf("overflow error: file %s, line %d\n", __FILE__, __LINE__)
352
353 /* Handle underflow. */
354 #define JAS_FIX_UFLOW() \
355 jas_eprintf("underflow error: file %s, line %d\n", __FILE__, __LINE__)
356
357 #ifdef __cplusplus
358 }
359 #endif
360
361 #endif