JasPer: jas_math.h Source File

JasPer 2.0.24 |

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_types.h>
80
81 #include <assert.h>
82 #include <string.h>
83 #include <stdint.h>
84 #include <limits.h>
85
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89
90 /******************************************************************************\
91 * Macros
92 \******************************************************************************/
93
94 #define JAS_KIBI JAS_CAST(size_t, 1024)
95 #define JAS_MEBI (JAS_KIBI * JAS_KIBI)
96
97 /* Compute the absolute value. */
98 #define JAS_ABS(x) \
99 (((x) >= 0) ? (x) : (-(x)))
100
101 /* Compute the minimum of two values. */
102 #define JAS_MIN(x, y) \
103 (((x) < (y)) ? (x) : (y))
104
105 /* Compute the maximum of two values. */
106 #define JAS_MAX(x, y) \
107 (((x) > (y)) ? (x) : (y))
108
109 /* Compute the remainder from division (where division is defined such
110 that the remainder is always nonnegative). */
111 #define JAS_MOD(x, y) \
112 (((x) < 0) ? (((-x) % (y)) ? ((y) - ((-(x)) % (y))) : (0)) : ((x) % (y)))
113
114 /* Compute the integer with the specified number of least significant bits
115 set to one. */
116 #define JAS_ONES(n) \
117 ((1 << (n)) - 1)
118
119 /******************************************************************************\
120 *
121 \******************************************************************************/
122
123 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
124 /* suppress clang warning "shifting a negative signed value is
125 undefined" in the assertions below */
126 #pragma GCC diagnostic push
127 #pragma GCC diagnostic ignored "-Wshift-negative-value"
128 #endif
129
130 JAS_ATTRIBUTE_CONST
131 JAS_ATTRIBUTE_DISABLE_USAN
132 inline static int jas_int_asr(int x, unsigned n)
133 {
134 // Ensure that the shift of a negative value appears to behave as a
135 // signed arithmetic shift.
136 assert(((-1) >> 1) == -1);
137 // The behavior is undefined when x is negative. */
138 // We tacitly assume the behavior is equivalent to a signed
139 // arithmetic right shift.
140 return x >> n;
141 }
142
143 JAS_ATTRIBUTE_CONST
144 JAS_ATTRIBUTE_DISABLE_USAN
145 inline static int jas_int_asl(int x, unsigned n)
146 {
147 // Ensure that the shift of a negative value appears to behave as a
148 // signed arithmetic shift.
149 assert(((-1) << 1) == -2);
150 // The behavior is undefined when x is negative. */
151 // We tacitly assume the behavior is equivalent to a signed
152 // arithmetic left shift.
153 return x << n;
154 }
155
156 JAS_ATTRIBUTE_CONST
157 JAS_ATTRIBUTE_DISABLE_USAN
158 inline static int_least32_t jas_least32_asr(int_least32_t x, unsigned n)
159 {
160 // Ensure that the shift of a negative value appears to behave as a
161 // signed arithmetic shift.
162 assert(((JAS_CAST(int_least32_t, -1)) >> 1) == JAS_CAST(int_least32_t, -1));
163 // The behavior is undefined when x is negative. */
164 // We tacitly assume the behavior is equivalent to a signed
165 // arithmetic right shift.
166 return x >> n;
167 }
168
169 JAS_ATTRIBUTE_CONST
170 JAS_ATTRIBUTE_DISABLE_USAN
171 inline static int_least32_t jas_least32_asl(int_least32_t x, unsigned n)
172 {
173 // Ensure that the shift of a negative value appears to behave as a
174 // signed arithmetic shift.
175 assert(((JAS_CAST(int_least32_t, -1)) << 1) == JAS_CAST(int_least32_t, -2));
176 // The behavior is undefined when x is negative. */
177 // We tacitly assume the behavior is equivalent to a signed
178 // arithmetic left shift.
179 return x << n;
180 }
181
182 JAS_ATTRIBUTE_CONST
183 JAS_ATTRIBUTE_DISABLE_USAN
184 inline static int_fast32_t jas_fast32_asr(int_fast32_t x, unsigned n)
185 {
186 // Ensure that the shift of a negative value appears to behave as a
187 // signed arithmetic shift.
188 assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
189 // The behavior is undefined when x is negative. */
190 // We tacitly assume the behavior is equivalent to a signed
191 // arithmetic right shift.
192 return x >> n;
193 }
194
195 JAS_ATTRIBUTE_CONST
196 JAS_ATTRIBUTE_DISABLE_USAN
197 inline static int_fast32_t jas_fast32_asl(int_fast32_t x, unsigned n)
198 {
199 // Ensure that the shift of a negative value appears to behave as a
200 // signed arithmetic shift.
201 assert(((JAS_CAST(int_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
202 // The behavior is undefined when x is negative. */
203 // We tacitly assume the behavior is equivalent to a signed
204 // arithmetic left shift.
205 return x << n;
206 }
207
208 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
209 #pragma GCC diagnostic pop
210 #endif
211
212 /******************************************************************************\
213 * Safe integer arithmetic (i.e., with overflow checking).
214 \******************************************************************************/
215
216 /* Compute the product of two size_t integers with overflow checking. */
217 inline static bool jas_safe_size_mul(size_t x, size_t y, size_t *result)
218 {
219 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 5)
220 size_t result_buffer;
221 if (!result)
222 result = &result_buffer;
223 return !__builtin_mul_overflow(x, y, result);
224 #else
225 /* Check if overflow would occur */
226 if (x && y > SIZE_MAX / x) {
227 /* Overflow would occur. */
228 return false;
229 }
230 if (result) {
231 *result = x * y;
232 }
233 return true;
234 #endif
235 }
236
237 /* Compute the product of three size_t integers with overflow checking. */
238 inline static bool jas_safe_size_mul3(size_t a, size_t b, size_t c,
239 size_t *result)
240 {
241 size_t tmp;
242 if (!jas_safe_size_mul(a, b, &tmp) ||
243 !jas_safe_size_mul(tmp, c, &tmp)) {
244 return false;
245 }
246 if (result) {
247 *result = tmp;
248 }
249 return true;
250 }
251
252 /* Compute the sum of two size_t integers with overflow checking. */
253 inline static bool jas_safe_size_add(size_t x, size_t y, size_t *result)
254 {
255 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 5)
256 size_t result_buffer;
257 if (!result)
258 result = &result_buffer;
259 return !__builtin_add_overflow(x, y, result);
260 #else
261 if (y > SIZE_MAX - x) {
262 return false;
263 }
264 if (result) {
265 *result = x + y;
266 }
267 return true;
268 #endif
269 }
270
271 /* Compute the difference of two size_t integers with overflow checking. */
272 inline static bool jas_safe_size_sub(size_t x, size_t y, size_t *result)
273 {
274 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 5)
275 size_t result_buffer;
276 if (!result)
277 result = &result_buffer;
278 return !__builtin_sub_overflow(x, y, result);
279 #else
280 if (y > x) {
281 return false;
282 }
283 if (result) {
284 *result = x - y;
285 }
286 return true;
287 #endif
288 }
289
290 /* Compute the product of two int_fast32_t integers with overflow checking. */
291 inline static bool jas_safe_intfast32_mul(int_fast32_t x, int_fast32_t y,
292 int_fast32_t *result)
293 {
294 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 5)
295 int_fast32_t result_buffer;
296 if (!result)
297 result = &result_buffer;
298 return !__builtin_mul_overflow(x, y, result);
299 #else
300 if (x > 0) {
301 /* x is positive */
302 if (y > 0) {
303 /* x and y are positive */
304 if (x > INT_FAST32_MAX / y) {
305 return false;
306 }
307 } else {
308 /* x positive, y nonpositive */
309 if (y < INT_FAST32_MIN / x) {
310 return false;
311 }
312 }
313 } else {
314 /* x is nonpositive */
315 if (y > 0) {
316 /* x is nonpositive, y is positive */
317 if (x < INT_FAST32_MIN / y) {
318 return false;
319 }
320 } else { /* x and y are nonpositive */
321 if (x != 0 && y < INT_FAST32_MAX / x) {
322 return false;
323 }
324 }
325 }
326
327 if (result) {
328 *result = x * y;
329 }
330 return true;
331 #endif
332 }
333
334 /* Compute the product of three int_fast32_t integers with overflow checking. */
335 inline static bool jas_safe_intfast32_mul3(int_fast32_t a, int_fast32_t b,
336 int_fast32_t c, int_fast32_t *result)
337 {
338 int_fast32_t tmp;
339 if (!jas_safe_intfast32_mul(a, b, &tmp) ||
340 !jas_safe_intfast32_mul(tmp, c, &tmp)) {
341 return false;
342 }
343 if (result) {
344 *result = tmp;
345 }
346 return true;
347 }
348
349 /* Compute the sum of two int_fast32_t integers with overflow checking. */
350 inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
351 int_fast32_t *result)
352 {
353 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 5)
354 int_fast32_t result_buffer;
355 if (!result)
356 result = &result_buffer;
357 return !__builtin_add_overflow(x, y, result);
358 #else
359 if ((y > 0 && x > INT_FAST32_MAX - y) ||
360 (y < 0 && x < INT_FAST32_MIN - y)) {
361 return false;
362 }
363 if (result) {
364 *result = x + y;
365 }
366 return true;
367 #endif
368 }
369
370 #ifdef __cplusplus
371 }
372 #endif
373
374 #endif
jas_types.h
Primitive Types.