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) \
124 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
127 #pragma GCC diagnostic push
128 #pragma GCC diagnostic ignored "-Wshift-negative-value"
132 JAS_ATTRIBUTE_DISABLE_UBSAN
133 inline static int jas_int_asr(
int x,
unsigned n)
137 assert(((-1) >> 1) == -1);
145 JAS_ATTRIBUTE_DISABLE_UBSAN
146 inline static int jas_int_asl(
int x,
unsigned n)
150 assert(((-1) << 1) == -2);
158 JAS_ATTRIBUTE_DISABLE_UBSAN
159 inline static int_least32_t jas_least32_asr(int_least32_t x,
unsigned n)
163 assert(((JAS_CAST(int_least32_t, -1)) >> 1) == JAS_CAST(int_least32_t, -1));
171 JAS_ATTRIBUTE_DISABLE_UBSAN
172 inline static int_least32_t jas_least32_asl(int_least32_t x,
unsigned n)
176 assert(((JAS_CAST(int_least32_t, -1)) << 1) == JAS_CAST(int_least32_t, -2));
184 JAS_ATTRIBUTE_DISABLE_UBSAN
185 inline static int_fast32_t jas_fast32_asr(int_fast32_t x,
unsigned n)
189 assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
197 JAS_ATTRIBUTE_DISABLE_UBSAN
198 inline static int_fast32_t jas_fast32_asl(int_fast32_t x,
unsigned n)
202 assert(((JAS_CAST(int_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
209 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
210 #pragma GCC diagnostic pop
218 inline static bool jas_safe_size_mul(
size_t x,
size_t y,
size_t *result)
220 #if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
221 size_t result_buffer;
222 bool valid = !__builtin_mul_overflow(x, y, &result_buffer);
223 if (valid && result) {
224 *result = result_buffer;
229 if (x && y > SIZE_MAX / x) {
241 inline static bool jas_safe_size_mul3(
size_t a,
size_t b,
size_t c,
245 if (!jas_safe_size_mul(a, b, &tmp) ||
246 !jas_safe_size_mul(tmp, c, &tmp)) {
256 inline static bool jas_safe_size_add(
size_t x,
size_t y,
size_t *result)
258 #if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
259 size_t result_buffer;
260 bool valid = !__builtin_add_overflow(x, y, &result_buffer);
261 if (valid && result) {
262 *result = result_buffer;
266 if (y > SIZE_MAX - x) {
277 inline static bool jas_safe_size_sub(
size_t x,
size_t y,
size_t *result)
279 #if jas_has_builtin(__builtin_sub_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
280 size_t result_buffer;
281 bool valid = !__builtin_sub_overflow(x, y, &result_buffer);
282 if (valid && result) {
283 *result = result_buffer;
298 inline static bool jas_safe_intfast32_mul(int_fast32_t x, int_fast32_t y,
299 int_fast32_t *result)
301 #if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
302 int_fast32_t result_buffer;
303 bool valid = !__builtin_mul_overflow(x, y, &result_buffer);
304 if (valid && result) {
305 *result = result_buffer;
313 if (x > INT_FAST32_MAX / y) {
318 if (y < INT_FAST32_MIN / x) {
326 if (x < INT_FAST32_MIN / y) {
330 if (x != 0 && y < INT_FAST32_MAX / x) {
344 inline static bool jas_safe_intfast32_mul3(int_fast32_t a, int_fast32_t b,
345 int_fast32_t c, int_fast32_t *result)
348 if (!jas_safe_intfast32_mul(a, b, &tmp) ||
349 !jas_safe_intfast32_mul(tmp, c, &tmp)) {
359 inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
360 int_fast32_t *result)
362 #if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
363 int_fast32_t result_buffer;
364 bool valid = !__builtin_add_overflow(x, y, &result_buffer);
365 if (valid && result) {
366 *result = result_buffer;
370 if ((y > 0 && x > INT_FAST32_MAX - y) ||
371 (y < 0 && x < INT_FAST32_MIN - y)) {
386 inline static bool jas_safe_uint_mul(
unsigned x,
unsigned y,
unsigned *result)
389 if (x && y > UINT_MAX / x) {
406 uint_least64_t value;
410 jas_safeui64_t jas_safeui64_from_intmax(intmax_t x)
412 jas_safeui64_t result;
413 if (x >= 0 && x <= UINT_LEAST64_MAX) {
415 result.value = JAS_CAST(uint_least64_t, x);
417 result.valid =
false;
424 jas_safeui64_t jas_safeui64_add(jas_safeui64_t x, jas_safeui64_t y)
426 jas_safeui64_t result;
427 if (x.valid && y.valid && y.value <= UINT_LEAST64_MAX - x.value) {
429 result.value = x.value + y.value;
431 result.valid =
false;
438 jas_safeui64_t jas_safeui64_sub(jas_safeui64_t x, jas_safeui64_t y)
440 jas_safeui64_t result;
441 if (x.valid && y.valid && y.value <= x.value) {
443 result.value = x.value - y.value;
445 result.valid =
false;
452 jas_safeui64_t jas_safeui64_mul(jas_safeui64_t x, jas_safeui64_t y)
454 jas_safeui64_t result;
455 if (!x.valid || !y.valid || (x.value && y.value > UINT_LEAST64_MAX /
457 result.valid =
false;
461 result.value = x.value * y.value;
467 jas_safeui64_t jas_safeui64_div(jas_safeui64_t x, jas_safeui64_t y)
469 jas_safeui64_t result;
470 if (x.valid && y.valid && y.value) {
472 result.value = x.value / y.value;
474 result.valid =
false;
481 jas_safeui64_t jas_safeui64_pow2_intmax(intmax_t x)
483 jas_safeui64_t result;
484 if (x >= 0 && x < 64) {
486 result.value = JAS_CAST(uint_least64_t, 1) << x;
488 result.valid =
false;
495 int jas_safeui64_to_int(jas_safeui64_t x,
int invalid_value)
498 if (x.valid && x.value <= INT_MAX) {
499 result = JAS_CAST(
int, x.value);
501 result = invalid_value;