Go to the documentation of this file.
77 #include <jasper/jas_config.h>
95 #define JAS_FIX_ZERO(fix_t, fracbits) \
96 JAS_INTTOFIX(fix_t, fracbits, 0)
99 #define JAS_FIX_ONE(fix_t, fracbits) \
100 JAS_INTTOFIX(fix_t, fracbits, 1)
103 #define JAS_FIX_HALF(fix_t, fracbits) \
104 (JAS_CAST(fix_t, 1) << ((fracbits) - 1))
111 #define JAS_INTTOFIX(fix_t, fracbits, x) \
112 (JAS_CAST(fix_t, x) << (fracbits))
115 #define JAS_FIXTOINT(fix_t, fracbits, x) \
116 JAS_CAST(int, (x) >> (fracbits))
119 #define JAS_FIXTODBL(fix_t, fracbits, x) \
120 (JAS_CAST(double, x) / JAS_FIX_ONE(fix_t, fracbits))
123 #define JAS_DBLTOFIX(fix_t, fracbits, x) \
124 JAS_CAST(fix_t, ((x) * JAS_CAST(double, JAS_FIX_ONE(fix_t, fracbits))))
137 #if !defined(DEBUG_OVERFLOW)
138 #define JAS_FIX_ADD JAS_FIX_ADD_FAST
140 #define JAS_FIX_ADD JAS_FIX_ADD_OFLOW
144 #define JAS_FIX_ADD_FAST(fix_t, fracbits, x, y) ((x) + (y))
147 #define JAS_FIX_ADD_OFLOW(fix_t, fracbits, x, y) \
149 (((y) >= 0) ? ((x) + (y) >= 0 || JAS_FIX_OFLOW(), (x) + (y)) : \
151 (((y) >= 0) ? ((x) + (y)) : ((x) + (y) < 0 || JAS_FIX_OFLOW(), \
155 #if !defined(DEBUG_OVERFLOW)
156 #define JAS_FIX_MUL JAS_FIX_MUL_FAST
158 #define JAS_FIX_MUL JAS_FIX_MUL_OFLOW
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)) >> \
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) >> \
173 JAS_CAST(fix_t, (JAS_CAST(bigfix_t, x) * JAS_CAST(bigfix_t, y) >> \
174 (fracbits))) : JAS_FIX_OFLOW())
177 #if !defined(DEBUG_OVERFLOW)
178 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_FAST
180 #define JAS_FIX_MULBYINT JAS_FIX_MULBYINT_OFLOW
185 #define JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y) \
186 JAS_CAST(fix_t, ((x) * (y)))
190 #define JAS_FIX_MULBYINT_OFLOW(fix_t, fracbits, x, y) \
191 JAS_FIX_MULBYINT_FAST(fix_t, fracbits, x, y)
194 #if !defined(DEBUG_OVERFLOW)
195 #define JAS_FIX_DIV JAS_FIX_DIV_FAST
197 #define JAS_FIX_DIV JAS_FIX_DIV_UFLOW
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))
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)
211 #if !defined(DEBUG_OVERFLOW)
212 #define JAS_FIX_NEG JAS_FIX_NEG_FAST
214 #define JAS_FIX_NEG JAS_FIX_NEG_OFLOW
218 #define JAS_FIX_NEG_FAST(fix_t, fracbits, x) \
224 #define JAS_FIX_NEG_OFLOW(fix_t, fracbits, x) \
225 (((x) < 0) ? (-(x) > 0 || JAS_FIX_OFLOW(), -(x)) : (-(x)))
228 #if !defined(DEBUG_OVERFLOW)
229 #define JAS_FIX_ASL JAS_FIX_ASL_FAST
231 #define JAS_FIX_ASL JAS_FIX_ASL_OFLOW
236 #define JAS_FIX_ASL_FAST(fix_t, fracbits, x, n) \
241 #define JAS_FIX_ASL_OFLOW(fix_t, fracbits, x, n) \
242 ((((x) << (n)) >> (n)) == (x) || JAS_FIX_OFLOW(), (x) << (n))
245 #if !defined(DEBUG_OVERFLOW)
246 #define JAS_FIX_ASR JAS_FIX_ASR_FAST
248 #define JAS_FIX_ASR JAS_FIX_ASR_UFLOW
253 #define JAS_FIX_ASR_FAST(fix_t, fracbits, x, n) \
258 #define JAS_FIX_ASR_UFLOW(fix_t, fracbits, x, n) \
259 JAS_FIX_ASR_FAST(fix_t, fracbits, x, n)
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))
270 #define JAS_FIX_PLUSEQ(fix_t, fracbits, x, y) \
271 ((x) = JAS_FIX_ADD(fix_t, fracbits, x, y))
274 #define JAS_FIX_MINUSEQ(fix_t, fracbits, x, y) \
275 ((x) = JAS_FIX_SUB(fix_t, fracbits, x, y))
278 #define JAS_FIX_MULEQ(fix_t, fracbits, bigfix_t, x, y) \
279 ((x) = JAS_FIX_MUL(fix_t, fracbits, bigfix_t, x, y))
286 #define JAS_FIX_ABS(fix_t, fracbits, x) \
287 (((x) >= 0) ? (x) : (JAS_FIX_NEG(fix_t, fracbits, x)))
290 #define JAS_FIX_ISINT(fix_t, fracbits, x) \
291 (JAS_FIX_FLOOR(fix_t, fracbits, x) == (x))
294 #define JAS_FIX_SGN(fix_t, fracbits, x) \
295 ((x) >= 0 ? 1 : (-1))
302 #define JAS_FIX_CMP(fix_t, fracbits, x, y) \
303 ((x) > (y) ? 1 : (((x) == (y)) ? 0 : (-1)))
306 #define JAS_FIX_LT(fix_t, fracbits, x, y) \
310 #define JAS_FIX_LTE(fix_t, fracbits, x, y) \
314 #define JAS_FIX_GT(fix_t, fracbits, x, y) \
318 #define JAS_FIX_GTE(fix_t, fracbits, x, y) \
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)))))
334 #define JAS_FIX_FLOOR(fix_t, fracbits, x) \
335 ((x) & (~(JAS_FIX_ONE(fix_t, fracbits) - 1)))
343 #define JAS_FIX_OFLOW() \
344 jas_logerrorf("overflow error: file %s, line %d\n", __FILE__, __LINE__)
347 #define JAS_FIX_UFLOW() \
348 jas_logerrorf("underflow error: file %s, line %d\n", __FILE__, __LINE__)