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