• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: jas_fix.h,v 1.2 2008-05-26 09:41:51 vp153 Exp $
68  */
69 
70 #ifndef JAS_FIX_H
71 #define JAS_FIX_H
72 
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76 
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <math.h>
80 
81 #include <jasper/jas_config.h>
82 #include <jasper/jas_types.h>
83 
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
88 /******************************************************************************\
89 * Constants.
90 \******************************************************************************/
91 
92 /* The representation of the value zero. */
93 #define	JAS_FIX_ZERO(fix_t, fracbits) \
94     JAS_CAST(fix_t, 0)
95 
96 /* The representation of the value one. */
97 #define	JAS_FIX_ONE(fix_t, fracbits) \
98     (JAS_CAST(fix_t, 1) << (fracbits))
99 
100 /* The representation of the value one half. */
101 #define	JAS_FIX_HALF(fix_t, fracbits) \
102     (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
103 
104 /******************************************************************************\
105 * Conversion operations.
106 \******************************************************************************/
107 
108 /* Convert an int to a fixed-point number. */
109 #define JAS_INTTOFIX(fix_t, fracbits, x) \
110     JAS_CAST(fix_t, (x) << (fracbits))
111 
112 /* Convert a fixed-point number to an int. */
113 #define JAS_FIXTOINT(fix_t, fracbits, x) \
114     JAS_CAST(int, (x) >> (fracbits))
115 
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)))
119 
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))))
123 
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 \******************************************************************************/
133 
134 /* Calculate the sum of two fixed-point numbers. */
135 #if !defined(DEBUG_OVERFLOW)
136 #define JAS_FIX_ADD			JAS_FIX_ADD_FAST
137 #else
138 #define JAS_FIX_ADD			JAS_FIX_ADD_OFLOW
139 #endif
140 
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))
143 
144 /* Calculate the sum of two fixed-point numbers with overflow checking. */
145 #define	JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
146     ((x) >= 0) ? \
147       (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
148       ((x) + (y))) : \
149       (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
150       (x) + (y)))
151 
152 /* Calculate the product of two fixed-point numbers. */
153 #if !defined(DEBUG_OVERFLOW)
154 #define JAS_FIX_MUL			JAS_FIX_MUL_FAST
155 #else
156 #define JAS_FIX_MUL			JAS_FIX_MUL_OFLOW
157 #endif
158 
159 /* Calculate the product of two fixed-point numbers without overflow
160   checking. */
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)) >> \
163       (fracbits))
164 
165 /* Calculate the product of two fixed-point numbers with overflow
166   checking. */
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) >> \
170       (fracbits))) ? \
171       JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
172       (fracbits))) : JAS_FIX_OFLOW())
173 
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
177 #else
178 #define	JAS_FIX_MULBYINT	JAS_FIX_MULBYINT_OFLOW
179 #endif
180 
181 /* Calculate the product of a fixed-point number and an int without overflow
182   checking. */
183 #define	JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
184     JAS_CAST(fix_t, ((x) * (y)))
185 
186 /* Calculate the product of a fixed-point number and an int with overflow
187   checking. */
188 #define	JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
189     JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
190 
191 /* Calculate the quotient of two fixed-point numbers. */
192 #if !defined(DEBUG_OVERFLOW)
193 #define JAS_FIX_DIV			JAS_FIX_DIV_FAST
194 #else
195 #define JAS_FIX_DIV			JAS_FIX_DIV_UFLOW
196 #endif
197 
198 /* Calculate the quotient of two fixed-point numbers without underflow
199   checking. */
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))
202 
203 /* Calculate the quotient of two fixed-point numbers with underflow
204   checking. */
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)
207 
208 /* Negate a fixed-point number. */
209 #if !defined(DEBUG_OVERFLOW)
210 #define	JAS_FIX_NEG			JAS_FIX_NEG_FAST
211 #else
212 #define	JAS_FIX_NEG			JAS_FIX_NEG_OFLOW
213 #endif
214 
215 /* Negate a fixed-point number without overflow checking. */
216 #define	JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
217     (-(x))
218 
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)))
224 
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
228 #else
229 #define	JAS_FIX_ASL			JAS_FIX_ASL_OFLOW
230 #endif
231 
232 /* Perform an arithmetic shift left of a fixed-point number without overflow
233   checking. */
234 #define	JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
235     ((x) << (n))
236 
237 /* Perform an arithmetic shift left of a fixed-point number with overflow
238   checking. */
239 #define	JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
240     ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
241 
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
245 #else
246 #define	JAS_FIX_ASR			JAS_FIX_ASR_UFLOW
247 #endif
248 
249 /* Perform an arithmetic shift right of a fixed-point number without underflow
250   checking. */
251 #define	JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
252     ((x) >> (n))
253 
254 /* Perform an arithmetic shift right of a fixed-point number with underflow
255   checking. */
256 #define	JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
257     JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
258 
259 /******************************************************************************\
260 * Other basic arithmetic operations.
261 \******************************************************************************/
262 
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))
266 
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))
270 
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))
274 
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))
278 
279 /******************************************************************************\
280 * Miscellaneous operations.
281 \******************************************************************************/
282 
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)))
286 
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))
290 
291 /* Get the sign of a fixed-point number. */
292 #define JAS_FIX_SGN(fix_t, fracbits, x) \
293     ((x) >= 0 ? 1 : (-1))
294 
295 /******************************************************************************\
296 * Relational operations.
297 \******************************************************************************/
298 
299 /* Compare two fixed-point numbers. */
300 #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
301     ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
302 
303 /* Less than. */
304 #define	JAS_FIX_LT(fix_t, fracbits, x, y) \
305     ((x) < (y))
306 
307 /* Less than or equal. */
308 #define	JAS_FIX_LTE(fix_t, fracbits, x, y) \
309     ((x) <= (y))
310 
311 /* Greater than. */
312 #define	JAS_FIX_GT(fix_t, fracbits, x, y) \
313     ((x) > (y))
314 
315 /* Greater than or equal. */
316 #define	JAS_FIX_GTE(fix_t, fracbits, x, y) \
317     ((x) >= (y))
318 
319 /******************************************************************************\
320 * Rounding functions.
321 \******************************************************************************/
322 
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)))))
329 
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)))
334 
335 /* Round a fixed-point number to the nearest integer in the direction
336   of zero. */
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))
340 
341 /******************************************************************************\
342 * The below macros are for internal library use only.  Do not invoke them
343 * directly in application code.
344 \******************************************************************************/
345 
346 /* Handle overflow. */
347 #define	JAS_FIX_OFLOW() \
348     jas_eprintf("overflow error: file %s, line %d\n", __FILE__, __LINE__)
349 
350 /* Handle underflow. */
351 #define	JAS_FIX_UFLOW() \
352     jas_eprintf("underflow error: file %s, line %d\n", __FILE__, __LINE__)
353 
354 #ifdef __cplusplus
355 }
356 #endif
357 
358 #endif
359