JasPer  4.2.4
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 
90 /******************************************************************************\
91 * Types.
92 \******************************************************************************/
93 
97 #if defined(JAS_ENABLE_32BIT)
98 typedef int_least32_t jas_fix_t;
99 #define PRIjas_fix PRIiLEAST32
100 #else
101 typedef int_least64_t jas_fix_t;
102 #define PRIjas_fix PRIiLEAST64
103 #endif
104 
105 /*
106 Integral type used for double-precision fixed-point representations.
107 */
108 #if defined(JAS_ENABLE_32BIT)
109 typedef int_least64_t jas_fix_big_t;
110 #else
111 #if defined(JAS_HAVE_INT128_T)
112 typedef __int128_t jas_fix_big_t;
113 #else
114 typedef int_least64_t jas_fix_big_t;
115 #endif
116 #endif
117 
118 /******************************************************************************\
119 * Constants.
120 \******************************************************************************/
121 
123 #define JAS_FIX_ZERO(fix_t, fracbits) \
124  JAS_INTTOFIX(fix_t, fracbits, 0)
125 
127 #define JAS_FIX_ONE(fix_t, fracbits) \
128  JAS_INTTOFIX(fix_t, fracbits, 1)
129 
131 #define JAS_FIX_HALF(fix_t, fracbits) \
132  (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
133 
134 /******************************************************************************\
135 * Conversion operations.
136 \******************************************************************************/
137 
139 #define JAS_INTTOFIX(fix_t, fracbits, x) \
140  (JAS_CAST(fix_t, x) << (fracbits))
141 
143 #define JAS_FIXTOINT(fix_t, fracbits, x) \
144  JAS_CAST(int, (x) >> (fracbits))
145 
147 #define JAS_FIXTODBL(fix_t, fracbits, x) \
148  (JAS_CAST(double, x) / JAS_FIX_ONE(fix_t, fracbits))
149 
151 #define JAS_DBLTOFIX(fix_t, fracbits, x) \
152  JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_FIX_ONE(fix_t, fracbits))))
153 
154 /******************************************************************************\
155 * Basic arithmetic operations.
156 * All other arithmetic operations are synthesized from these basic operations.
157 * There are three macros for each type of arithmetic operation.
158 * One macro always performs overflow/underflow checking, one never performs
159 * overflow/underflow checking, and one is generic with its behavior
160 * depending on compile-time flags.
161 * Only the generic macros should be invoked directly by application code.
162 \******************************************************************************/
163 
165 #if !defined(DEBUG_OVERFLOW)
166 #define JAS_FIX_ADD JAS_FIX_ADD_FAST
167 #else
168 #define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
169 #endif
170 
172 #define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
173 
175 #define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
176  ((x) >= 0) ? \
177  (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
178  ((x) + (y))) : \
179  (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
180  (x) + (y)))
181 
183 #if !defined(DEBUG_OVERFLOW)
184 #define JAS_FIX_MUL JAS_FIX_MUL_FAST
185 #else
186 #define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
187 #endif
188 
191 #define JAS_FIX_MUL_FAST(fix_t, fracbits, bigfix_t, x, y) \
192  JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y)) >> \
193  (fracbits))
194 
197 #define JAS_FIX_MUL_OFLOW(fix_t, fracbits, bigfix_t, x, y) \
198  ((JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> (fracbits)) == \
199  JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
200  (fracbits))) ? \
201  JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
202  (fracbits))) : JAS_FIX_OFLOW())
203 
205 #if !defined(DEBUG_OVERFLOW)
206 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
207 #else
208 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
209 #endif
210 
213 #define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
214  JAS_CAST(fix_t, ((x) * (y)))
215 
218 #define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
219  JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
220 
222 #if !defined(DEBUG_OVERFLOW)
223 #define JAS_FIX_DIV JAS_FIX_DIV_FAST
224 #else
225 #define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
226 #endif
227 
230 #define JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y) \
231  JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) << (fracbits)) / (y))
232 
235 #define JAS_FIX_DIV_UFLOW(fix_t, fracbits, bigfix_t, x, y) \
236  JAS_FIX_DIV_FAST(fix_t, fracbits, bigfix_t, x, y)
237 
239 #if !defined(DEBUG_OVERFLOW)
240 #define JAS_FIX_NEG JAS_FIX_NEG_FAST
241 #else
242 #define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
243 #endif
244 
246 #define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
247  (-(x))
248 
250 /* Yes, overflow is actually possible for two's complement representations,
251  although highly unlikely to occur. */
252 #define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
253  (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
254 
256 #if !defined(DEBUG_OVERFLOW)
257 #define JAS_FIX_ASL JAS_FIX_ASL_FAST
258 #else
259 #define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
260 #endif
261 
264 #define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
265  ((x) << (n))
266 
269 #define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
270  ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
271 
273 #if !defined(DEBUG_OVERFLOW)
274 #define JAS_FIX_ASR JAS_FIX_ASR_FAST
275 #else
276 #define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
277 #endif
278 
281 #define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
282  ((x) >> (n))
283 
286 #define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
287  JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
288 
289 /******************************************************************************\
290 * Other basic arithmetic operations.
291 \******************************************************************************/
292 
294 #define JAS_FIX_SUB(fix_t, fracbits, x, y) \
295  JAS_FIX_ADD(fix_t, fracbits, x, JAS_FIX_NEG(fix_t, fracbits, y))
296 
298 #define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
299  ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
300 
302 #define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
303  ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
304 
306 #define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
307  ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
308 
309 /******************************************************************************\
310 * Miscellaneous operations.
311 \******************************************************************************/
312 
314 #define JAS_FIX_ABS(fix_t, fracbits, x) \
315  (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
316 
318 #define JAS_FIX_ISINT(fix_t, fracbits, x) \
319  (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
320 
322 #define JAS_FIX_SGN(fix_t, fracbits, x) \
323  ((x) >= 0 ? 1 : (-1))
324 
325 /******************************************************************************\
326 * Relational operations.
327 \******************************************************************************/
328 
330 #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
331  ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
332 
334 #define JAS_FIX_LT(fix_t, fracbits, x, y) \
335  ((x) < (y))
336 
338 #define JAS_FIX_LTE(fix_t, fracbits, x, y) \
339  ((x) <= (y))
340 
342 #define JAS_FIX_GT(fix_t, fracbits, x, y) \
343  ((x) > (y))
344 
346 #define JAS_FIX_GTE(fix_t, fracbits, x, y) \
347  ((x) >= (y))
348 
349 /******************************************************************************\
350 * Rounding functions.
351 \******************************************************************************/
352 
354 #define JAS_FIX_ROUND(fix_t, fracbits, x) \
355  (((x) < 0) ? JAS_FIX_FLOOR(fix_t, fracbits, JAS_FIX_ADD(fix_t, fracbits, \
356  (x), JAS_FIX_HALF(fix_t, fracbits))) : \
357  JAS_FIX_NEG(fix_t, fracbits, JAS_FIX_FLOOR(fix_t, fracbits, \
358  JAS_FIX_ADD(fix_t, fracbits, (-(x)), JAS_FIX_HALF(fix_t, fracbits)))))
359 
362 #define JAS_FIX_FLOOR(fix_t, fracbits, x) \
363  ((x) & (~(JAS_FIX_ONE(fix_t, fracbits) - 1)))
364 
365 /******************************************************************************\
366 * The below macros are for internal library use only. Do not invoke them
367 * directly in application code.
368 \******************************************************************************/
369 
370 /* Handle overflow. */
371 #define JAS_FIX_OFLOW() \
372  jas_logerrorf("overflow error: file %s, line %d\n", __FILE__, __LINE__)
373 
374 /* Handle underflow. */
375 #define JAS_FIX_UFLOW() \
376  jas_logerrorf("underflow error: file %s, line %d\n", __FILE__, __LINE__)
377 
378 /******************************************************************************\
379 *
380 \******************************************************************************/
381 
382 JAS_ATTRIBUTE_DISABLE_UBSAN
383 static inline jas_fix_t jas_fix_asl(jas_fix_t x, unsigned n)
384 {
385  return JAS_FIX_ASL(jas_fix_t, 0, x, n);
386 }
387 
388 JAS_ATTRIBUTE_DISABLE_UBSAN
389 static inline jas_fix_t jas_fix_asr(jas_fix_t x, unsigned n)
390 {
391  return JAS_FIX_ASR(jas_fix_t, 0, x, n);
392 }
393 
398 #ifdef __cplusplus
399 }
400 #endif
401 
402 #endif
#define JAS_FIX_ASL
Definition: jas_fix.h:257
int_least64_t jas_fix_t
Definition: jas_fix.h:101
#define JAS_FIX_ASR
Definition: jas_fix.h:274
Primitive Types.