77 #include <jasper/jas_config.h>
97 #if defined(JAS_ENABLE_32BIT)
99 #define PRIjas_fix PRIiLEAST32
102 #define PRIjas_fix PRIiLEAST64
108 #if defined(JAS_ENABLE_32BIT)
109 typedef int_least64_t jas_fix_big_t;
111 #if defined(JAS_HAVE_INT128_T)
112 typedef __int128_t jas_fix_big_t;
114 typedef int_least64_t jas_fix_big_t;
123 #define JAS_FIX_ZERO(fix_t, fracbits) \
124 JAS_INTTOFIX(fix_t, fracbits, 0)
127 #define JAS_FIX_ONE(fix_t, fracbits) \
128 JAS_INTTOFIX(fix_t, fracbits, 1)
131 #define JAS_FIX_HALF(fix_t, fracbits) \
132 (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
139 #define JAS_INTTOFIX(fix_t, fracbits, x) \
140 (JAS_CAST(fix_t, x) << (fracbits))
143 #define JAS_FIXTOINT(fix_t, fracbits, x) \
144 JAS_CAST(int, (x) >> (fracbits))
147 #define JAS_FIXTODBL(fix_t, fracbits, x) \
148 (JAS_CAST(double, x) / JAS_FIX_ONE(fix_t, fracbits))
151 #define JAS_DBLTOFIX(fix_t, fracbits, x) \
152 JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_FIX_ONE(fix_t, fracbits))))
165 #if !defined(DEBUG_OVERFLOW)
166 #define JAS_FIX_ADD JAS_FIX_ADD_FAST
168 #define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
172 #define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
175 #define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
177 (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
179 (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
183 #if !defined(DEBUG_OVERFLOW)
184 #define JAS_FIX_MUL JAS_FIX_MUL_FAST
186 #define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
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)) >> \
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) >> \
201 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
202 (fracbits))) : JAS_FIX_OFLOW())
205 #if !defined(DEBUG_OVERFLOW)
206 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
208 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
213 #define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
214 JAS_CAST(fix_t, ((x) * (y)))
218 #define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
219 JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
222 #if !defined(DEBUG_OVERFLOW)
223 #define JAS_FIX_DIV JAS_FIX_DIV_FAST
225 #define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
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))
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)
239 #if !defined(DEBUG_OVERFLOW)
240 #define JAS_FIX_NEG JAS_FIX_NEG_FAST
242 #define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
246 #define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
252 #define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
253 (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
256 #if !defined(DEBUG_OVERFLOW)
257 #define JAS_FIX_ASL JAS_FIX_ASL_FAST
259 #define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
264 #define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
269 #define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
270 ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
273 #if !defined(DEBUG_OVERFLOW)
274 #define JAS_FIX_ASR JAS_FIX_ASR_FAST
276 #define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
281 #define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
286 #define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
287 JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
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))
298 #define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
299 ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
302 #define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
303 ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
306 #define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
307 ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
314 #define JAS_FIX_ABS(fix_t, fracbits, x) \
315 (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
318 #define JAS_FIX_ISINT(fix_t, fracbits, x) \
319 (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
322 #define JAS_FIX_SGN(fix_t, fracbits, x) \
323 ((x) >= 0 ? 1 : (-1))
330 #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
331 ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
334 #define JAS_FIX_LT(fix_t, fracbits, x, y) \
338 #define JAS_FIX_LTE(fix_t, fracbits, x, y) \
342 #define JAS_FIX_GT(fix_t, fracbits, x, y) \
346 #define JAS_FIX_GTE(fix_t, fracbits, x, y) \
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)))))
362 #define JAS_FIX_FLOOR(fix_t, fracbits, x) \
363 ((x) & (~(JAS_FIX_ONE(fix_t, fracbits) - 1)))
371 #define JAS_FIX_OFLOW() \
372 jas_logerrorf("overflow error: file %s, line %d\n", __FILE__, __LINE__)
375 #define JAS_FIX_UFLOW() \
376 jas_logerrorf("underflow error: file %s, line %d\n", __FILE__, __LINE__)
382 JAS_ATTRIBUTE_DISABLE_UBSAN
388 JAS_ATTRIBUTE_DISABLE_UBSAN