My Project: jas_fix.h Source File

My Project 2.0.19 |

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