jas_math.h
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 
64 /*
65  * Math-Related Code
66  *
67  * $Id$
68  */
69 
70 #ifndef JAS_MATH_H
71 #define JAS_MATH_H
72 
73 /******************************************************************************\
74 * Includes
75 \******************************************************************************/
76 
77 /* The configuration header file should be included first. */
78 #include <jasper/jas_config.h>
79 
80 #include <jasper/jas_types.h>
81 
82 #include <assert.h>
83 #include <stdio.h>
84 #include <string.h>
85 #include <stdint.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 
120 /******************************************************************************\
121 *
122 \******************************************************************************/
123 
124 JAS_ATTRIBUTE_DISABLE_USAN
125 inline static int jas_int_asr(int x, int n)
126 {
127  // Ensure that the shift of a negative value appears to behave as a
128  // signed arithmetic shift.
129  assert(((-1) >> 1) == -1);
130  assert(n >= 0);
131  // The behavior is undefined when x is negative. */
132  // We tacitly assume the behavior is equivalent to a signed
133  // arithmetic right shift.
134  return x >> n;
135 }
136 
137 JAS_ATTRIBUTE_DISABLE_USAN
138 inline static int jas_int_asl(int x, int n)
139 {
140  // Ensure that the shift of a negative value appears to behave as a
141  // signed arithmetic shift.
142  assert(((-1) << 1) == -2);
143  assert(n >= 0);
144  // The behavior is undefined when x is negative. */
145  // We tacitly assume the behavior is equivalent to a signed
146  // arithmetic left shift.
147  return x << n;
148 }
149 
150 JAS_ATTRIBUTE_DISABLE_USAN
151 inline static int jas_fast32_asr(int_fast32_t x, int n)
152 {
153  // Ensure that the shift of a negative value appears to behave as a
154  // signed arithmetic shift.
155  assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
156  assert(n >= 0);
157  // The behavior is undefined when x is negative. */
158  // We tacitly assume the behavior is equivalent to a signed
159  // arithmetic right shift.
160  return x >> n;
161 }
162 
163 JAS_ATTRIBUTE_DISABLE_USAN
164 inline static int jas_fast32_asl(int_fast32_t x, int n)
165 {
166  // Ensure that the shift of a negative value appears to behave as a
167  // signed arithmetic shift.
168  assert(((JAS_CAST(int_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
169  assert(n >= 0);
170  // The behavior is undefined when x is negative. */
171  // We tacitly assume the behavior is equivalent to a signed
172  // arithmetic left shift.
173  return x << n;
174 }
175 
176 /******************************************************************************\
177 * Safe integer arithmetic (i.e., with overflow checking).
178 \******************************************************************************/
179 
180 /* Compute the product of two size_t integers with overflow checking. */
181 inline static bool jas_safe_size_mul(size_t x, size_t y, size_t *result)
182 {
183  /* Check if overflow would occur */
184  if (x && y > SIZE_MAX / x) {
185  /* Overflow would occur. */
186  return false;
187  }
188  if (result) {
189  *result = x * y;
190  }
191  return true;
192 }
193 
194 /* Compute the product of three size_t integers with overflow checking. */
195 inline static bool jas_safe_size_mul3(size_t a, size_t b, size_t c,
196  size_t *result)
197 {
198  size_t tmp;
199  if (!jas_safe_size_mul(a, b, &tmp) ||
200  !jas_safe_size_mul(tmp, c, &tmp)) {
201  return false;
202  }
203  if (result) {
204  *result = tmp;
205  }
206  return true;
207 }
208 
209 /* Compute the sum of two size_t integers with overflow checking. */
210 inline static bool jas_safe_size_add(size_t x, size_t y, size_t *result)
211 {
212  if (y > SIZE_MAX - x) {
213  return false;
214  }
215  if (result) {
216  *result = x + y;
217  }
218  return true;
219 }
220 
221 /* Compute the difference of two size_t integers with overflow checking. */
222 inline static bool jas_safe_size_sub(size_t x, size_t y, size_t *result)
223 {
224  if (y > x) {
225  return false;
226  }
227  if (result) {
228  *result = x - y;
229  }
230  return true;
231 }
232 
233 /* Compute the product of two int_fast32_t integers with overflow checking. */
234 inline static bool jas_safe_intfast32_mul(int_fast32_t x, int_fast32_t y,
235  int_fast32_t *result)
236 {
237  if (x > 0) {
238  /* x is positive */
239  if (y > 0) {
240  /* x and y are positive */
241  if (x > INT_FAST32_MAX / y) {
242  return false;
243  }
244  } else {
245  /* x positive, y nonpositive */
246  if (y < INT_FAST32_MIN / x) {
247  return false;
248  }
249  }
250  } else {
251  /* x is nonpositive */
252  if (y > 0) {
253  /* x is nonpositive, y is positive */
254  if (x < INT_FAST32_MIN / y) {
255  return false;
256  }
257  } else { /* x and y are nonpositive */
258  if (x != 0 && y < INT_FAST32_MAX / x) {
259  return false;
260  }
261  }
262  }
263 
264  if (result) {
265  *result = x * y;
266  }
267  return true;
268 }
269 
270 /* Compute the product of three int_fast32_t integers with overflow checking. */
271 inline static bool jas_safe_intfast32_mul3(int_fast32_t a, int_fast32_t b,
272  int_fast32_t c, int_fast32_t *result)
273 {
274  int_fast32_t tmp;
275  if (!jas_safe_intfast32_mul(a, b, &tmp) ||
276  !jas_safe_intfast32_mul(tmp, c, &tmp)) {
277  return false;
278  }
279  if (result) {
280  *result = tmp;
281  }
282  return true;
283 }
284 
285 /* Compute the sum of two int_fast32_t integers with overflow checking. */
286 inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
287  int_fast32_t *result)
288 {
289  if ((y > 0 && x > INT_FAST32_MAX - y) ||
290  (y < 0 && x < INT_FAST32_MIN - y)) {
291  return false;
292  }
293  if (result) {
294  *result = x + y;
295  }
296  return true;
297 }
298 
299 #ifdef __cplusplus
300 }
301 #endif
302 
303 #endif