JasPer 3.0.4
 
Loading...
Searching...
No Matches
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
82extern "C" {
83#endif
84
90/******************************************************************************\
91* Constants.
92\******************************************************************************/
93
95#define JAS_FIX_ZERO(fix_t, fracbits) \
96 JAS_INTTOFIX(fix_t, fracbits, 0)
97
99#define JAS_FIX_ONE(fix_t, fracbits) \
100 JAS_INTTOFIX(fix_t, fracbits, 1)
101
103#define JAS_FIX_HALF(fix_t, fracbits) \
104 (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
105
106/******************************************************************************\
107* Conversion operations.
108\******************************************************************************/
109
111#define JAS_INTTOFIX(fix_t, fracbits, x) \
112 (JAS_CAST(fix_t, x) << (fracbits))
113
115#define JAS_FIXTOINT(fix_t, fracbits, x) \
116 JAS_CAST(int, (x) >> (fracbits))
117
119#define JAS_FIXTODBL(fix_t, fracbits, x) \
120 (JAS_CAST(double, x) / JAS_FIX_ONE(fix_t, fracbits))
121
123#define JAS_DBLTOFIX(fix_t, fracbits, x) \
124 JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_FIX_ONE(fix_t, fracbits))))
125
126/******************************************************************************\
127* Basic arithmetic operations.
128* All other arithmetic operations are synthesized from these basic operations.
129* There are three macros for each type of arithmetic operation.
130* One macro always performs overflow/underflow checking, one never performs
131* overflow/underflow checking, and one is generic with its behavior
132* depending on compile-time flags.
133* Only the generic macros should be invoked directly by application code.
134\******************************************************************************/
135
137#if !defined(DEBUG_OVERFLOW)
138#define JAS_FIX_ADD JAS_FIX_ADD_FAST
139#else
140#define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
141#endif
142
144#define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
145
147#define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
148 ((x) >= 0) ? \
149 (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
150 ((x) + (y))) : \
151 (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
152 (x) + (y)))
153
155#if !defined(DEBUG_OVERFLOW)
156#define JAS_FIX_MUL JAS_FIX_MUL_FAST
157#else
158#define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
159#endif
160
163#define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \
164 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \
165 (fracbits))
166
169#define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \
170 ((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \
171 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
172 (fracbits))) ? \
173 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
174 (fracbits))) : JAS_FIX_OFLOW())
175
177#if !defined(DEBUG_OVERFLOW)
178#define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
179#else
180#define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
181#endif
182
185#define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
186 JAS_CAST(fix_t, ((x) * (y)))
187
190#define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
191 JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
192
194#if !defined(DEBUG_OVERFLOW)
195#define JAS_FIX_DIV JAS_FIX_DIV_FAST
196#else
197#define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
198#endif
199
202#define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \
203 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))
204
207#define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \
208 JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)
209
211#if !defined(DEBUG_OVERFLOW)
212#define JAS_FIX_NEG JAS_FIX_NEG_FAST
213#else
214#define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
215#endif
216
218#define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
219 (-(x))
220
222/* Yes, overflow is actually possible for two's complement representations,
223 although highly unlikely to occur. */
224#define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
225 (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
226
228#if !defined(DEBUG_OVERFLOW)
229#define JAS_FIX_ASL JAS_FIX_ASL_FAST
230#else
231#define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
232#endif
233
236#define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
237 ((x) << (n))
238
241#define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
242 ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
243
245#if !defined(DEBUG_OVERFLOW)
246#define JAS_FIX_ASR JAS_FIX_ASR_FAST
247#else
248#define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
249#endif
250
253#define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
254 ((x) >> (n))
255
258#define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
259 JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
260
261/******************************************************************************\
262* Other basic arithmetic operations.
263\******************************************************************************/
264
266#define JAS_FIX_SUB(fix_t, fracbits, x, y) \
267 JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))
268
270#define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
271 ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
272
274#define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
275 ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
276
278#define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
279 ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
280
281/******************************************************************************\
282* Miscellaneous operations.
283\******************************************************************************/
284
286#define JAS_FIX_ABS(fix_t, fracbits, x) \
287 (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
288
290#define JAS_FIX_ISINT(fix_t, fracbits, x) \
291 (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
292
294#define JAS_FIX_SGN(fix_t, fracbits, x) \
295 ((x) >= 0 ? 1 : (-1))
296
297/******************************************************************************\
298* Relational operations.
299\******************************************************************************/
300
302#define JAS_FIX_CMP(fix_t, fracbits, x, y) \
303 ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
304
306#define JAS_FIX_LT(fix_t, fracbits, x, y) \
307 ((x) < (y))
308
310#define JAS_FIX_LTE(fix_t, fracbits, x, y) \
311 ((x) <= (y))
312
314#define JAS_FIX_GT(fix_t, fracbits, x, y) \
315 ((x) > (y))
316
318#define JAS_FIX_GTE(fix_t, fracbits, x, y) \
319 ((x) >= (y))
320
321/******************************************************************************\
322* Rounding functions.
323\******************************************************************************/
324
326#define JAS_FIX_ROUND(fix_t, fracbits, x) \
327 (((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \
328 (x), JAS_FIX_HALF(fix_t, fracbits))) : \
329 JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \
330 JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))
331
334#define JAS_FIX_FLOOR(fix_t, fracbits, x) \
335 ((x) & (~(JAS_FIX_ONE(fix_t, fracbits) - 1)))
336
337/******************************************************************************\
338* The below macros are for internal library use only. Do not invoke them
339* directly in application code.
340\******************************************************************************/
341
342/* Handle overflow. */
343#define JAS_FIX_OFLOW() \
344 jas_logerrorf("overflow error: file %s, line %d\n", __FILE__, __LINE__)
345
346/* Handle underflow. */
347#define JAS_FIX_UFLOW() \
348 jas_logerrorf("underflow error: file %s, line %d\n", __FILE__, __LINE__)
349
354#ifdef __cplusplus
355}
356#endif
357
358#endif
Primitive Types.