77 #include <jasper/jas_config.h>
95 #define JAS_KIBI JAS_CAST(size_t, 1024)
96 #define JAS_MEBI (JAS_KIBI * JAS_KIBI)
100 (((x) >= 0) ? (x) : (-(x)))
103 #define JAS_MIN(x, y) \
104 (((x) < (y)) ? (x) : (y))
107 #define JAS_MAX(x, y) \
108 (((x) > (y)) ? (x) : (y))
112 #define JAS_MOD(x, y) \
113 (((x) < 0) ? (((-x) % (y)) ? ((y) - ((-(x)) % (y))) : (0)) : ((x) % (y)))
117 #define JAS_ONES(n) \
120 #define JAS_ONES_X(type, n) \
121 ((JAS_CAST(type, 1) << (n)) - 1)
123 #define JAS_POW2_X(type, n) \
124 (JAS_CAST(type, 1) << (n))
130 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
133 #pragma GCC diagnostic push
134 #pragma GCC diagnostic ignored "-Wshift-negative-value"
138 JAS_ATTRIBUTE_DISABLE_UBSAN
139 inline static int jas_int_asr(
int x,
unsigned n)
143 assert(((-1) >> 1) == -1);
151 JAS_ATTRIBUTE_DISABLE_UBSAN
152 inline static int jas_int_asl(
int x,
unsigned n)
156 assert(((-1) << 1) == -2);
164 JAS_ATTRIBUTE_DISABLE_UBSAN
165 inline static int_least32_t jas_least32_asr(int_least32_t x,
unsigned n)
169 assert(((JAS_CAST(int_least32_t, -1)) >> 1) == JAS_CAST(int_least32_t, -1));
177 JAS_ATTRIBUTE_DISABLE_UBSAN
178 inline static int_least32_t jas_least32_asl(int_least32_t x,
unsigned n)
182 assert(((JAS_CAST(int_least32_t, -1)) << 1) == JAS_CAST(int_least32_t, -2));
190 JAS_ATTRIBUTE_DISABLE_UBSAN
191 inline static int_fast32_t jas_fast32_asr(int_fast32_t x,
unsigned n)
195 assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
203 JAS_ATTRIBUTE_DISABLE_UBSAN
204 inline static int_fast32_t jas_fast32_asl(int_fast32_t x,
unsigned n)
208 assert(((JAS_CAST(int_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
215 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
216 #pragma GCC diagnostic pop
224 inline static bool jas_safe_size_mul(
size_t x,
size_t y,
size_t *result)
226 #if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
227 size_t result_buffer;
228 bool valid = !__builtin_mul_overflow(x, y, &result_buffer);
229 if (valid && result) {
230 *result = result_buffer;
235 if (x && y > SIZE_MAX / x) {
247 inline static bool jas_safe_size_mul3(
size_t a,
size_t b,
size_t c,
251 if (!jas_safe_size_mul(a, b, &tmp) ||
252 !jas_safe_size_mul(tmp, c, &tmp)) {
262 inline static bool jas_safe_size_add(
size_t x,
size_t y,
size_t *result)
264 #if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
265 size_t result_buffer;
266 bool valid = !__builtin_add_overflow(x, y, &result_buffer);
267 if (valid && result) {
268 *result = result_buffer;
272 if (y > SIZE_MAX - x) {
283 inline static bool jas_safe_size_sub(
size_t x,
size_t y,
size_t *result)
285 #if jas_has_builtin(__builtin_sub_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
286 size_t result_buffer;
287 bool valid = !__builtin_sub_overflow(x, y, &result_buffer);
288 if (valid && result) {
289 *result = result_buffer;
304 inline static bool jas_safe_intfast32_mul(int_fast32_t x, int_fast32_t y,
305 int_fast32_t *result)
307 #if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
308 int_fast32_t result_buffer;
309 bool valid = !__builtin_mul_overflow(x, y, &result_buffer);
310 if (valid && result) {
311 *result = result_buffer;
319 if (x > INT_FAST32_MAX / y) {
324 if (y < INT_FAST32_MIN / x) {
332 if (x < INT_FAST32_MIN / y) {
336 if (x != 0 && y < INT_FAST32_MAX / x) {
350 inline static bool jas_safe_intfast32_mul3(int_fast32_t a, int_fast32_t b,
351 int_fast32_t c, int_fast32_t *result)
354 if (!jas_safe_intfast32_mul(a, b, &tmp) ||
355 !jas_safe_intfast32_mul(tmp, c, &tmp)) {
365 inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
366 int_fast32_t *result)
368 #if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
369 int_fast32_t result_buffer;
370 bool valid = !__builtin_add_overflow(x, y, &result_buffer);
371 if (valid && result) {
372 *result = result_buffer;
376 if ((y > 0 && x > INT_FAST32_MAX - y) ||
377 (y < 0 && x < INT_FAST32_MIN - y)) {
392 inline static bool jas_safe_uint_mul(
unsigned x,
unsigned y,
unsigned *result)
395 if (x && y > UINT_MAX / x) {
410 #define JAS_SAFEUI32_MAX (0xffffffffU)
414 uint_least32_t value;
418 static inline jas_safeui32_t jas_safeui32_from_ulong(
unsigned long x)
420 jas_safeui32_t result;
421 if (x <= JAS_SAFEUI32_MAX) {
423 result.value = JAS_CAST(uint_least32_t, x);
432 static inline bool jas_safeui32_to_intfast32(jas_safeui32_t x,
435 if (x.value <= INT_FAST32_MAX) {
444 static inline jas_safeui32_t jas_safeui32_add(jas_safeui32_t x,
447 jas_safeui32_t result;
448 if (x.valid && y.valid && y.value <= UINT_LEAST32_MAX - x.value) {
450 result.value = x.value + y.value;
452 result.valid =
false;
460 jas_safeui32_t jas_safeui32_sub(jas_safeui32_t x, jas_safeui32_t y)
462 jas_safeui32_t result;
463 if (x.valid && y.valid && y.value <= x.value) {
465 result.value = x.value - y.value;
467 result.valid =
false;
474 static inline jas_safeui32_t jas_safeui32_mul(jas_safeui32_t x,
477 jas_safeui32_t result;
478 if (!x.valid || !y.valid || (x.value && y.value > UINT_LEAST32_MAX /
480 result.valid =
false;
484 result.value = x.value * y.value;
495 uint_least64_t value;
500 jas_safeui64_t jas_safeui64_from_intmax(intmax_t x)
502 jas_safeui64_t result;
503 if (x >= 0 && x <= UINT_LEAST64_MAX) {
505 result.value = JAS_CAST(uint_least64_t, x);
507 result.valid =
false;
515 jas_safeui64_t jas_safeui64_add(jas_safeui64_t x, jas_safeui64_t y)
517 jas_safeui64_t result;
518 if (x.valid && y.valid && y.value <= UINT_LEAST64_MAX - x.value) {
520 result.value = x.value + y.value;
522 result.valid =
false;
530 jas_safeui64_t jas_safeui64_sub(jas_safeui64_t x, jas_safeui64_t y)
532 jas_safeui64_t result;
533 if (x.valid && y.valid && y.value <= x.value) {
535 result.value = x.value - y.value;
537 result.valid =
false;
545 jas_safeui64_t jas_safeui64_mul(jas_safeui64_t x, jas_safeui64_t y)
547 jas_safeui64_t result;
548 if (!x.valid || !y.valid || (x.value && y.value > UINT_LEAST64_MAX /
550 result.valid =
false;
554 result.value = x.value * y.value;
561 jas_safeui64_t jas_safeui64_div(jas_safeui64_t x, jas_safeui64_t y)
563 jas_safeui64_t result;
564 if (x.valid && y.valid && y.value) {
566 result.value = x.value / y.value;
568 result.valid =
false;
576 jas_safeui64_t jas_safeui64_pow2_intmax(intmax_t x)
578 jas_safeui64_t result;
579 if (x >= 0 && x < 64) {
581 result.value = JAS_CAST(uint_least64_t, 1) << x;
583 result.valid =
false;
591 int jas_safeui64_to_int(jas_safeui64_t x,
int invalid_value)
594 if (x.valid && x.value <= INT_MAX) {
595 result = JAS_CAST(
int, x.value);
597 result = invalid_value;