JasPer  4.2.2
jas_math.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_MATH_H
70 #define JAS_MATH_H
71 
72 /******************************************************************************\
73 * Includes
74 \******************************************************************************/
75 
76 /* The configuration header file should be included first. */
77 #include <jasper/jas_config.h>
78 
79 #include <jasper/jas_compiler.h>
80 #include <jasper/jas_types.h>
81 
82 #include <assert.h>
83 #include <string.h>
84 #include <stdint.h>
85 #include <limits.h>
86 
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
91 /******************************************************************************\
92 * Macros
93 \******************************************************************************/
94 
95 #define JAS_KIBI JAS_CAST(size_t, 1024)
96 #define JAS_MEBI (JAS_KIBI * JAS_KIBI)
97 
98 /* Compute the absolute value. */
99 #define JAS_ABS(x) \
100  (((x) >= 0) ? (x) : (-(x)))
101 
102 /* Compute the minimum of two values. */
103 #define JAS_MIN(x, y) \
104  (((x) < (y)) ? (x) : (y))
105 
106 /* Compute the maximum of two values. */
107 #define JAS_MAX(x, y) \
108  (((x) > (y)) ? (x) : (y))
109 
110 /* Compute the remainder from division (where division is defined such
111  that the remainder is always nonnegative). */
112 #define JAS_MOD(x, y) \
113  (((x) < 0) ? (((-x) % (y)) ? ((y) - ((-(x)) % (y))) : (0)) : ((x) % (y)))
114 
115 /* Compute the integer with the specified number of least significant bits
116  set to one. */
117 #define JAS_ONES(n) \
118  ((1 << (n)) - 1)
119 #if 0
120 #define JAS_ONES_X(type, n) \
121  ((JAS_CAST(type, 1) << (n)) - 1)
122 #endif
123 #define JAS_POW2_X(type, n) \
124  (JAS_CAST(type, 1) << (n))
125 
126 /******************************************************************************\
127 *
128 \******************************************************************************/
129 
130 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
131 /* suppress clang warning "shifting a negative signed value is
132  undefined" in the assertions below */
133 #pragma GCC diagnostic push
134 #pragma GCC diagnostic ignored "-Wshift-negative-value"
135 #endif
136 
137 JAS_ATTRIBUTE_CONST
138 JAS_ATTRIBUTE_DISABLE_UBSAN
139 inline static int jas_int_asr(int x, unsigned n)
140 {
141  // Ensure that the shift of a negative value appears to behave as a
142  // signed arithmetic shift.
143  assert(((-1) >> 1) == -1);
144  // The behavior is undefined when x is negative.
145  // We tacitly assume the behavior is equivalent to a signed
146  // arithmetic right shift.
147  return x >> n;
148 }
149 
150 JAS_ATTRIBUTE_CONST
151 JAS_ATTRIBUTE_DISABLE_UBSAN
152 inline static int jas_int_asl(int x, unsigned n)
153 {
154  // Ensure that the shift of a negative value appears to behave as a
155  // signed arithmetic shift.
156  assert(((-1) << 1) == -2);
157  // The behavior is undefined when x is negative.
158  // We tacitly assume the behavior is equivalent to a signed
159  // arithmetic left shift.
160  return x << n;
161 }
162 
163 JAS_ATTRIBUTE_CONST
164 JAS_ATTRIBUTE_DISABLE_UBSAN
165 inline static int_least32_t jas_least32_asr(int_least32_t x, unsigned n)
166 {
167  // Ensure that the shift of a negative value appears to behave as a
168  // signed arithmetic shift.
169  assert(((JAS_CAST(int_least32_t, -1)) >> 1) == JAS_CAST(int_least32_t, -1));
170  // The behavior is undefined when x is negative.
171  // We tacitly assume the behavior is equivalent to a signed
172  // arithmetic right shift.
173  return x >> n;
174 }
175 
176 JAS_ATTRIBUTE_CONST
177 JAS_ATTRIBUTE_DISABLE_UBSAN
178 inline static int_least32_t jas_least32_asl(int_least32_t x, unsigned n)
179 {
180  // Ensure that the shift of a negative value appears to behave as a
181  // signed arithmetic shift.
182  assert(((JAS_CAST(int_least32_t, -1)) << 1) == JAS_CAST(int_least32_t, -2));
183  // The behavior is undefined when x is negative.
184  // We tacitly assume the behavior is equivalent to a signed
185  // arithmetic left shift.
186  return x << n;
187 }
188 
189 JAS_ATTRIBUTE_CONST
190 JAS_ATTRIBUTE_DISABLE_UBSAN
191 inline static int_fast32_t jas_fast32_asr(int_fast32_t x, unsigned n)
192 {
193  // Ensure that the shift of a negative value appears to behave as a
194  // signed arithmetic shift.
195  assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
196  // The behavior is undefined when x is negative.
197  // We tacitly assume the behavior is equivalent to a signed
198  // arithmetic right shift.
199  return x >> n;
200 }
201 
202 JAS_ATTRIBUTE_CONST
203 JAS_ATTRIBUTE_DISABLE_UBSAN
204 inline static int_fast32_t jas_fast32_asl(int_fast32_t x, unsigned n)
205 {
206  // Ensure that the shift of a negative value appears to behave as a
207  // signed arithmetic shift.
208  assert(((JAS_CAST(int_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
209  // The behavior is undefined when x is negative.
210  // We tacitly assume the behavior is equivalent to a signed
211  // arithmetic left shift.
212  return x << n;
213 }
214 
215 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
216 #pragma GCC diagnostic pop
217 #endif
218 
219 /******************************************************************************\
220 * Safe integer arithmetic (i.e., with overflow checking).
221 \******************************************************************************/
222 
223 /* Compute the product of two size_t integers with overflow checking. */
224 inline static bool jas_safe_size_mul(size_t x, size_t y, size_t *result)
225 {
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;
231  }
232  return valid;
233 #else
234  /* Check if overflow would occur */
235  if (x && y > SIZE_MAX / x) {
236  /* Overflow would occur. */
237  return false;
238  }
239  if (result) {
240  *result = x * y;
241  }
242  return true;
243 #endif
244 }
245 
246 /* Compute the product of three size_t integers with overflow checking. */
247 inline static bool jas_safe_size_mul3(size_t a, size_t b, size_t c,
248  size_t *result)
249 {
250  size_t tmp;
251  if (!jas_safe_size_mul(a, b, &tmp) ||
252  !jas_safe_size_mul(tmp, c, &tmp)) {
253  return false;
254  }
255  if (result) {
256  *result = tmp;
257  }
258  return true;
259 }
260 
261 /* Compute the sum of two size_t integers with overflow checking. */
262 inline static bool jas_safe_size_add(size_t x, size_t y, size_t *result)
263 {
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;
269  }
270  return valid;
271 #else
272  if (y > SIZE_MAX - x) {
273  return false;
274  }
275  if (result) {
276  *result = x + y;
277  }
278  return true;
279 #endif
280 }
281 
282 /* Compute the difference of two size_t integers with overflow checking. */
283 inline static bool jas_safe_size_sub(size_t x, size_t y, size_t *result)
284 {
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;
290  }
291  return valid;
292 #else
293  if (y > x) {
294  return false;
295  }
296  if (result) {
297  *result = x - y;
298  }
299  return true;
300 #endif
301 }
302 
303 /* Compute the product of two int_fast32_t integers with overflow checking. */
304 inline static bool jas_safe_intfast32_mul(int_fast32_t x, int_fast32_t y,
305  int_fast32_t *result)
306 {
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;
312  }
313  return valid;
314 #else
315  if (x > 0) {
316  /* x is positive */
317  if (y > 0) {
318  /* x and y are positive */
319  if (x > INT_FAST32_MAX / y) {
320  return false;
321  }
322  } else {
323  /* x positive, y nonpositive */
324  if (y < INT_FAST32_MIN / x) {
325  return false;
326  }
327  }
328  } else {
329  /* x is nonpositive */
330  if (y > 0) {
331  /* x is nonpositive, y is positive */
332  if (x < INT_FAST32_MIN / y) {
333  return false;
334  }
335  } else { /* x and y are nonpositive */
336  if (x != 0 && y < INT_FAST32_MAX / x) {
337  return false;
338  }
339  }
340  }
341 
342  if (result) {
343  *result = x * y;
344  }
345  return true;
346 #endif
347 }
348 
349 /* Compute the product of three int_fast32_t integers with overflow checking. */
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)
352 {
353  int_fast32_t tmp;
354  if (!jas_safe_intfast32_mul(a, b, &tmp) ||
355  !jas_safe_intfast32_mul(tmp, c, &tmp)) {
356  return false;
357  }
358  if (result) {
359  *result = tmp;
360  }
361  return true;
362 }
363 
364 /* Compute the sum of two int_fast32_t integers with overflow checking. */
365 inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
366  int_fast32_t *result)
367 {
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;
373  }
374  return valid;
375 #else
376  if ((y > 0 && x > INT_FAST32_MAX - y) ||
377  (y < 0 && x < INT_FAST32_MIN - y)) {
378  return false;
379  }
380  if (result) {
381  *result = x + y;
382  }
383  return true;
384 #endif
385 }
386 
387 #if 0
388 /*
389 This function is potentially useful but not currently used.
390 So, it is commented out.
391 */
392 inline static bool jas_safe_uint_mul(unsigned x, unsigned y, unsigned *result)
393 {
394  /* Check if overflow would occur */
395  if (x && y > UINT_MAX / x) {
396  /* Overflow would occur. */
397  return false;
398  }
399  if (result) {
400  *result = x * y;
401  }
402  return true;
403 }
404 #endif
405 
406 /******************************************************************************\
407 * Safe 32-bit unsigned integer arithmetic (i.e., with overflow checking).
408 \******************************************************************************/
409 
410 #define JAS_SAFEUI32_MAX (0xffffffffUL)
411 
412 typedef struct {
413  bool valid;
414  uint_least32_t value;
415 } jas_safeui32_t;
416 
417 JAS_ATTRIBUTE_CONST
418 static inline jas_safeui32_t jas_safeui32_from_ulong(unsigned long x)
419 {
420  jas_safeui32_t result;
421  if (x <= JAS_SAFEUI32_MAX) {
422  result.valid = 1;
423  result.value = JAS_CAST(uint_least32_t, x);
424  } else {
425  result.valid = 0;
426  result.value = 0;
427  }
428  return result;
429 }
430 
431 JAS_ATTRIBUTE_PURE
432 static inline bool jas_safeui32_to_intfast32(jas_safeui32_t x,
433  int_fast32_t* y)
434 {
435  const long I32_MAX = 0x7fffffffL;
436  if (x.value <= I32_MAX) {
437  *y = x.value;
438  return true;
439  } else {
440  return false;
441  }
442 }
443 
444 JAS_ATTRIBUTE_CONST
445 static inline jas_safeui32_t jas_safeui32_add(jas_safeui32_t x,
446  jas_safeui32_t y)
447 {
448  jas_safeui32_t result;
449  if (x.valid && y.valid && y.value <= UINT_LEAST32_MAX - x.value) {
450  result.valid = true;
451  result.value = x.value + y.value;
452  } else {
453  result.valid = false;
454  result.value = 0;
455  }
456  return result;
457 }
458 
459 JAS_ATTRIBUTE_CONST
460 static inline
461 jas_safeui32_t jas_safeui32_sub(jas_safeui32_t x, jas_safeui32_t y)
462 {
463  jas_safeui32_t result;
464  if (x.valid && y.valid && y.value <= x.value) {
465  result.valid = true;
466  result.value = x.value - y.value;
467  } else {
468  result.valid = false;
469  result.value = 0;
470  }
471  return result;
472 }
473 
474 JAS_ATTRIBUTE_CONST
475 static inline jas_safeui32_t jas_safeui32_mul(jas_safeui32_t x,
476  jas_safeui32_t y)
477 {
478  jas_safeui32_t result;
479  if (!x.valid || !y.valid || (x.value && y.value > UINT_LEAST32_MAX /
480  x.value)) {
481  result.valid = false;
482  result.value = 0;
483  } else {
484  result.valid = true;
485  result.value = x.value * y.value;
486  }
487  return result;
488 }
489 
490 /******************************************************************************\
491 * Safe 64-bit signed integer arithmetic (i.e., with overflow checking).
492 \******************************************************************************/
493 
494 typedef struct {
495  bool valid;
496  int_least64_t value;
497 } jas_safei64_t;
498 
499 JAS_ATTRIBUTE_CONST
500 static inline
501 jas_safei64_t jas_safei64_from_intmax(intmax_t x)
502 {
503  jas_safei64_t result;
504  if (x >= INT_LEAST64_MIN && x <= INT_LEAST64_MAX) {
505  result.valid = true;
506  result.value = JAS_CAST(int_least64_t, x);
507  } else {
508  result.valid = false;
509  result.value = 0;
510  }
511  return result;
512 }
513 
514 JAS_ATTRIBUTE_CONST
515 static inline
516 jas_safei64_t jas_safei64_add(jas_safei64_t x, jas_safei64_t y)
517 {
518  jas_safei64_t result;
519  if (((y.value > 0) && (x.value > (INT_LEAST64_MAX - y.value))) ||
520  ((y.value < 0) && (x.value < (INT_LEAST64_MIN - y.value)))) {
521  result.value = false;
522  result.value = 0;
523  } else {
524  result.valid = true;
525  result.value = x.value + y.value;
526  }
527  return result;
528 }
529 
530 JAS_ATTRIBUTE_CONST
531 static inline
532 jas_safei64_t jas_safei64_sub(jas_safei64_t x, jas_safei64_t y)
533 {
534  jas_safei64_t result;
535  if ((y.value > 0 && x.value < INT_LEAST64_MIN + y.value) ||
536  (y.value < 0 && x.value > INT_LEAST64_MAX + y.value)) {
537  result.valid = false;
538  result.value = 0;
539  } else {
540  result.valid = true;
541  result.value = x.value - y.value;
542  }
543  return result;
544 }
545 
546 JAS_ATTRIBUTE_CONST
547 static inline
548 jas_safei64_t jas_safei64_mul(jas_safei64_t x, jas_safei64_t y)
549 {
550  jas_safei64_t result;
551  if (x.value > 0) { /* x.value is positive */
552  if (y.value > 0) { /* x.value and y.value are positive */
553  if (x.value > (INT_LEAST64_MAX / y.value)) {
554  goto error;
555  }
556  } else { /* x.value positive, y.value nonpositive */
557  if (y.value < (INT_LEAST64_MIN / x.value)) {
558  goto error;
559  }
560  } /* x.value positive, y.value nonpositive */
561  } else { /* x.value is nonpositive */
562  if (y.value > 0) { /* x.value is nonpositive, y.value is positive */
563  if (x.value < (INT_LEAST64_MIN / y.value)) {
564  goto error;
565  }
566  } else { /* x.value and y.value are nonpositive */
567  if ( (x.value != 0) && (y.value < (INT_LEAST64_MAX / x.value))) {
568  goto error;
569  }
570  } /* End if x.value and y.value are nonpositive */
571  } /* End if x.value is nonpositive */
572  result.valid = true;
573  result.value = x.value * y.value;
574  return result;
575 error:
576  result.valid = false;
577  result.value = 0;
578  return result;
579 }
580 
581 #if 0
582 JAS_ATTRIBUTE_CONST
583 static inline
584 jas_safei64_t jas_safei64_div(jas_safei64_t x, jas_safei64_t y)
585 {
586  // TODO/FIXME: Not yet implemented.
587  jas_safei64_t result;
588  result.valid = false;
589  result.value = 0;
590  return result;
591 }
592 #endif
593 
594 JAS_ATTRIBUTE_CONST
595 static inline
596 jas_i32_t jas_safei64_to_i32(jas_safei64_t x, jas_i32_t invalid_value)
597 {
598  jas_i32_t result;
599  if (x.valid && x.value >= JAS_I32_MIN && x.value <= JAS_I32_MAX) {
600  result = JAS_CAST(jas_i32_t, x.value);
601  } else {
602  result = invalid_value;
603  }
604  return result;
605 }
606 
607 /******************************************************************************\
608 * Safe 64-bit unsigned integer arithmetic (i.e., with overflow checking).
609 \******************************************************************************/
610 
611 typedef struct {
612  bool valid;
613  uint_least64_t value;
614 } jas_safeui64_t;
615 
616 JAS_ATTRIBUTE_CONST
617 static inline
618 jas_safeui64_t jas_safeui64_from_intmax(intmax_t x)
619 {
620  jas_safeui64_t result;
621  if (x >= 0 && x <= UINT_LEAST64_MAX) {
622  result.valid = true;
623  result.value = JAS_CAST(uint_least64_t, x);
624  } else {
625  result.valid = false;
626  result.value = 0;
627  }
628  return result;
629 }
630 
631 JAS_ATTRIBUTE_CONST
632 static inline
633 jas_safeui64_t jas_safeui64_add(jas_safeui64_t x, jas_safeui64_t y)
634 {
635  jas_safeui64_t result;
636  if (x.valid && y.valid && y.value <= UINT_LEAST64_MAX - x.value) {
637  result.valid = true;
638  result.value = x.value + y.value;
639  } else {
640  result.valid = false;
641  result.value = 0;
642  }
643  return result;
644 }
645 
646 JAS_ATTRIBUTE_CONST
647 static inline
648 jas_safeui64_t jas_safeui64_sub(jas_safeui64_t x, jas_safeui64_t y)
649 {
650  jas_safeui64_t result;
651  if (x.valid && y.valid && y.value <= x.value) {
652  result.valid = true;
653  result.value = x.value - y.value;
654  } else {
655  result.valid = false;
656  result.value = 0;
657  }
658  return result;
659 }
660 
661 JAS_ATTRIBUTE_CONST
662 static inline
663 jas_safeui64_t jas_safeui64_mul(jas_safeui64_t x, jas_safeui64_t y)
664 {
665  jas_safeui64_t result;
666  if (!x.valid || !y.valid || (x.value && y.value > UINT_LEAST64_MAX /
667  x.value)) {
668  result.valid = false;
669  result.value = 0;
670  } else {
671  result.valid = true;
672  result.value = x.value * y.value;
673  }
674  return result;
675 }
676 
677 JAS_ATTRIBUTE_CONST
678 static inline
679 jas_safeui64_t jas_safeui64_div(jas_safeui64_t x, jas_safeui64_t y)
680 {
681  jas_safeui64_t result;
682  if (x.valid && y.valid && y.value) {
683  result.valid = true;
684  result.value = x.value / y.value;
685  } else {
686  result.valid = false;
687  result.value = 0;
688  }
689  return result;
690 }
691 
692 JAS_ATTRIBUTE_CONST
693 static inline
694 jas_safeui64_t jas_safeui64_pow2_intmax(intmax_t x)
695 {
696  jas_safeui64_t result;
697  if (x >= 0 && x < 64) {
698  result.valid = true;
699  result.value = JAS_CAST(uint_least64_t, 1) << x;
700  } else {
701  result.valid = false;
702  result.value = 0;
703  }
704  return result;
705 }
706 
707 JAS_ATTRIBUTE_CONST
708 static inline
709 int jas_safeui64_to_int(jas_safeui64_t x, int invalid_value)
710 {
711  int result;
712  if (x.valid && x.value <= INT_MAX) {
713  result = JAS_CAST(int, x.value);
714  } else {
715  result = invalid_value;
716  }
717  return result;
718 }
719 
720 JAS_ATTRIBUTE_CONST
721 static inline
722 jas_ui32_t jas_safeui64_to_ui32(jas_safeui64_t x, jas_ui32_t invalid_value)
723 {
724  jas_ui32_t result;
725  if (x.valid && x.value <= JAS_UI32_MAX) {
726  result = JAS_CAST(jas_ui32_t, x.value);
727  } else {
728  result = invalid_value;
729  }
730  return result;
731 }
732 
733 JAS_ATTRIBUTE_CONST
734 static inline
735 jas_i32_t jas_safeui64_to_i32(jas_safeui64_t x, jas_i32_t invalid_value)
736 {
737  jas_i32_t result;
738  if (x.valid && x.value >= JAS_I32_MIN && x.value <= JAS_I32_MAX) {
739  result = JAS_CAST(jas_i32_t, x.value);
740  } else {
741  result = invalid_value;
742  }
743  return result;
744 }
745 
746 /******************************************************************************\
747 \******************************************************************************/
748 
749 #ifdef __cplusplus
750 }
751 #endif
752 
753 #endif
Compiler-related macros.
Primitive Types.