Debian Codegraph @

converter/other/jpeg2000/libjasper/include/jasper/jas_fix.h

raw
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 Software License
11 *
12 * IMAGE POWER JPEG-2000 PUBLIC LICENSE
13 * ************************************
14 *
15 * GRANT:
16 *
17 * Permission is hereby granted, free of charge, to any person (the "User")
18 * obtaining a copy of this software and associated documentation, to deal
19 * in the JasPer Software without restriction, including without limitation
20 * the right to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the JasPer Software (in source and binary forms),
22 * and to permit persons to whom the JasPer Software is furnished to do so,
23 * provided further that the License Conditions below are met.
24 *
25 * License Conditions
26 * ******************
27 *
28 * A. Redistributions of source code must retain the above copyright notice,
29 * and this list of conditions, and the following disclaimer.
30 *
31 * B. Redistributions in binary form must reproduce the above copyright
32 * notice, and this list of conditions, and the following disclaimer in
33 * the documentation and/or other materials provided with the distribution.
34 *
35 * C. Neither the name of Image Power, Inc. nor any other contributor
36 * (including, but not limited to, the University of British Columbia and
37 * Michael David Adams) may be used to endorse or promote products derived
38 * from this software without specific prior written permission.
39 *
40 * D. User agrees that it shall not commence any action against Image Power,
41 * Inc., the University of British Columbia, Michael David Adams, or any
42 * other contributors (collectively "Licensors") for infringement of any
43 * intellectual property rights ("IPR") held by the User in respect of any
44 * technology that User owns or has a right to license or sublicense and
45 * which is an element required in order to claim compliance with ISO/IEC
46 * 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property
47 * rights worldwide arising under statutory or common law, and whether
48 * or not perfected, including, without limitation, all (i) patents and
49 * patent applications owned or licensable by User; (ii) rights associated
50 * with works of authorship including copyrights, copyright applications,
51 * copyright registrations, mask work rights, mask work applications,
52 * mask work registrations; (iii) rights relating to the protection of
53 * trade secrets and confidential information; (iv) any right analogous
54 * to those set forth in subsections (i), (ii), or (iii) and any other
55 * proprietary rights relating to intangible property (other than trademark,
56 * trade dress, or service mark rights); and (v) divisions, continuations,
57 * renewals, reissues and extensions of the foregoing (as and to the extent
58 * applicable) now existing, hereafter filed, issued or acquired.
59 *
60 * E. If User commences an infringement action against any Licensor(s) then
61 * such Licensor(s) shall have the right to terminate User's license and
62 * all sublicenses that have been granted hereunder by User to other parties.
63 *
64 * F. This software is for use only in hardware or software products that
65 * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license
66 * or right to this Software is granted for products that do not comply
67 * with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased
68 * from the ISO.
69 *
70 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
71 * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
72 * THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND
73 * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY
74 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
75 * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE,
76 * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING
77 * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE
78 * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING
79 * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS.
80 * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE
81 * IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE
82 * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY
83 * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY
84 * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING,
85 * REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY,
86 * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE
87 * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC.,
88 * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE
89 * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO
90 * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
91 * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION,
92 * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR
93 * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF
94 * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY
95 * OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT
96 * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR
97 * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING
98 * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES,
99 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT
100 * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
101 * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY
102 * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE
103 * ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS
104 * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT
105 * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING
106 * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS
107 * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE
108 * NOTICE SPECIFIED IN THIS SECTION.
109 *
110 * __END_OF_JASPER_LICENSE__
111 */
112
113 /*
114 * Fixed-Point Number Class
115 *
116 * $Id$
117 */
118
119 #ifndef JAS_FIX_H
120 #define JAS_FIX_H
121
122 /******************************************************************************\
123 * Includes.
124 \******************************************************************************/
125
126 #include <stdio.h>
127 #include <stdlib.h>
128 #include <math.h>
129
130 #include <jasper/jas_types.h>
131
132 #ifdef __cplusplus
133 extern "C" {
134 #endif
135
136 /******************************************************************************\
137 * Constants.
138 \******************************************************************************/
139
140 /* The representation of the value zero. */
141 #define JAS_FIX_ZERO(fix_t, fracbits) \
142 JAS_CAST(fix_t, 0)
143
144 /* The representation of the value one. */
145 #define JAS_FIX_ONE(fix_t, fracbits) \
146 (JAS_CAST(fix_t, 1) << (fracbits))
147
148 /* The representation of the value one half. */
149 #define JAS_FIX_HALF(fix_t, fracbits) \
150 (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
151
152 /******************************************************************************\
153 * Conversion operations.
154 \******************************************************************************/
155
156 /* Convert an int to a fixed-point number. */
157 #define JAS_INTTOFIX(fix_t, fracbits, x) \
158 JAS_CAST(fix_t, (x) << (fracbits))
159
160 /* Convert a fixed-point number to an int. */
161 #define JAS_FIXTOINT(fix_t, fracbits, x) \
162 JAS_CAST(int, (x) >> (fracbits))
163
164 /* Convert a fixed-point number to a double. */
165 #define JAS_FIXTODBL(fix_t, fracbits, x) \
166 (JAS_CAST(double, x) / (JAS_CAST(fix_t, 1) << (fracbits)))
167
168 /* Convert a double to a fixed-point number. */
169 #define JAS_DBLTOFIX(fix_t, fracbits, x) \
170 JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_CAST(fix_t, 1) << (fracbits))))
171
172 /******************************************************************************\
173 * Basic arithmetic operations.
174 * All other arithmetic operations are synthesized from these basic operations.
175 * There are three macros for each type of arithmetic operation.
176 * One macro always performs overflow/underflow checking, one never performs
177 * overflow/underflow checking, and one is generic with its behavior
178 * depending on compile-time flags.
179 * Only the generic macros should be invoked directly by application code.
180 \******************************************************************************/
181
182 /* Calculate the sum of two fixed-point numbers. */
183 #if !defined(DEBUG_OVERFLOW)
184 #define JAS_FIX_ADD JAS_FIX_ADD_FAST
185 #else
186 #define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
187 #endif
188
189 /* Calculate the sum of two fixed-point numbers without overflow checking. */
190 #define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
191
192 /* Calculate the sum of two fixed-point numbers with overflow checking. */
193 #define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
194 ((x) >= 0) ? \
195 (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
196 ((x) + (y))) : \
197 (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
198 (x) + (y)))
199
200 /* Calculate the product of two fixed-point numbers. */
201 #if !defined(DEBUG_OVERFLOW)
202 #define JAS_FIX_MUL JAS_FIX_MUL_FAST
203 #else
204 #define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
205 #endif
206
207 /* Calculate the product of two fixed-point numbers without overflow
208 checking. */
209 #define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \
210 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \
211 (fracbits))
212
213 /* Calculate the product of two fixed-point numbers with overflow
214 checking. */
215 #define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \
216 ((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \
217 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
218 (fracbits))) ? \
219 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
220 (fracbits))) : JAS_FIX_OFLOW())
221
222 /* Calculate the product of a fixed-point number and an int. */
223 #if !defined(DEBUG_OVERFLOW)
224 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
225 #else
226 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
227 #endif
228
229 /* Calculate the product of a fixed-point number and an int without overflow
230 checking. */
231 #define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
232 JAS_CAST(fix_t, ((x) * (y)))
233
234 /* Calculate the product of a fixed-point number and an int with overflow
235 checking. */
236 #define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
237 JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
238
239 /* Calculate the quotient of two fixed-point numbers. */
240 #if !defined(DEBUG_OVERFLOW)
241 #define JAS_FIX_DIV JAS_FIX_DIV_FAST
242 #else
243 #define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
244 #endif
245
246 /* Calculate the quotient of two fixed-point numbers without underflow
247 checking. */
248 #define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \
249 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))
250
251 /* Calculate the quotient of two fixed-point numbers with underflow
252 checking. */
253 #define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \
254 JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)
255
256 /* Negate a fixed-point number. */
257 #if !defined(DEBUG_OVERFLOW)
258 #define JAS_FIX_NEG JAS_FIX_NEG_FAST
259 #else
260 #define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
261 #endif
262
263 /* Negate a fixed-point number without overflow checking. */
264 #define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
265 (-(x))
266
267 /* Negate a fixed-point number with overflow checking. */
268 /* Yes, overflow is actually possible for two's complement representations,
269 although highly unlikely to occur. */
270 #define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
271 (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
272
273 /* Perform an arithmetic shift left of a fixed-point number. */
274 #if !defined(DEBUG_OVERFLOW)
275 #define JAS_FIX_ASL JAS_FIX_ASL_FAST
276 #else
277 #define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
278 #endif
279
280 /* Perform an arithmetic shift left of a fixed-point number without overflow
281 checking. */
282 #define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
283 ((x) << (n))
284
285 /* Perform an arithmetic shift left of a fixed-point number with overflow
286 checking. */
287 #define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
288 ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
289
290 /* Perform an arithmetic shift right of a fixed-point number. */
291 #if !defined(DEBUG_OVERFLOW)
292 #define JAS_FIX_ASR JAS_FIX_ASR_FAST
293 #else
294 #define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
295 #endif
296
297 /* Perform an arithmetic shift right of a fixed-point number without underflow
298 checking. */
299 #define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
300 ((x) >> (n))
301
302 /* Perform an arithmetic shift right of a fixed-point number with underflow
303 checking. */
304 #define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
305 JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
306
307 /******************************************************************************\
308 * Other basic arithmetic operations.
309 \******************************************************************************/
310
311 /* Calculate the difference between two fixed-point numbers. */
312 #define JAS_FIX_SUB(fix_t, fracbits, x, y) \
313 JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))
314
315 /* Add one fixed-point number to another. */
316 #define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
317 ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
318
319 /* Subtract one fixed-point number from another. */
320 #define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
321 ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
322
323 /* Multiply one fixed-point number by another. */
324 #define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
325 ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
326
327 /******************************************************************************\
328 * Miscellaneous operations.
329 \******************************************************************************/
330
331 /* Calculate the absolute value of a fixed-point number. */
332 #define JAS_FIX_ABS(fix_t, fracbits, x) \
333 (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
334
335 /* Is a fixed-point number an integer? */
336 #define JAS_FIX_ISINT(fix_t, fracbits, x) \
337 (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
338
339 /* Get the sign of a fixed-point number. */
340 #define JAS_FIX_SGN(fix_t, fracbits, x) \
341 ((x) >= 0 ? 1 : (-1))
342
343 /******************************************************************************\
344 * Relational operations.
345 \******************************************************************************/
346
347 /* Compare two fixed-point numbers. */
348 #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
349 ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
350
351 /* Less than. */
352 #define JAS_FIX_LT(fix_t, fracbits, x, y) \
353 ((x) < (y))
354
355 /* Less than or equal. */
356 #define JAS_FIX_LTE(fix_t, fracbits, x, y) \
357 ((x) <= (y))
358
359 /* Greater than. */
360 #define JAS_FIX_GT(fix_t, fracbits, x, y) \
361 ((x) > (y))
362
363 /* Greater than or equal. */
364 #define JAS_FIX_GTE(fix_t, fracbits, x, y) \
365 ((x) >= (y))
366
367 /******************************************************************************\
368 * Rounding functions.
369 \******************************************************************************/
370
371 /* Round a fixed-point number to the nearest integer. */
372 #define JAS_FIX_ROUND(fix_t, fracbits, x) \
373 (((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \
374 (x), JAS_FIX_HALF(fix_t, fracbits))) : \
375 JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \
376 JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))
377
378 /* Round a fixed-point number to the nearest integer in the direction of
379 negative infinity (i.e., the floor function). */
380 #define JAS_FIX_FLOOR(fix_t, fracbits, x) \
381 ((x) & (~((JAS_CAST(fix_t, 1) << (fracbits)) - 1)))
382
383 /* Round a fixed-point number to the nearest integer in the direction
384 of zero. */
385 #define JAS_FIX_TRUNC(fix_t, fracbits, x) \
386 (((x) >= 0) ? JAS_FIX_FLOOR(fix_t, fracbits, x) : \
387 JAS_FIX_CEIL(fix_t, fracbits, x))
388
389 /******************************************************************************\
390 * The below macros are for internal library use only. Do not invoke them
391 * directly in application code.
392 \******************************************************************************/
393
394 /* Handle overflow. */
395 #define JAS_FIX_OFLOW() \
396 fprintf(stderr, "overflow error: file %s, line %d\n", __FILE__, __LINE__)
397
398 /* Handle underflow. */
399 #define JAS_FIX_UFLOW() \
400 fprintf(stderr, "underflow error: file %s, line %d\n", __FILE__, __LINE__)
401
402 #ifdef __cplusplus
403 }
404 #endif
405
406 #endif