JasPer 2.0.33
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
88extern "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#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
125/* suppress clang warning "shifting a negative signed value is
126 undefined" in the assertions below */
127#pragma GCC diagnostic push
128#pragma GCC diagnostic ignored "-Wshift-negative-value"
129#endif
130
131JAS_ATTRIBUTE_CONST
132JAS_ATTRIBUTE_DISABLE_USAN
133inline static int jas_int_asr(int x, unsigned n)
134{
135 // Ensure that the shift of a negative value appears to behave as a
136 // signed arithmetic shift.
137 assert(((-1) >> 1) == -1);
138 // The behavior is undefined when x is negative. */
139 // We tacitly assume the behavior is equivalent to a signed
140 // arithmetic right shift.
141 return x >> n;
142}
143
144JAS_ATTRIBUTE_CONST
145JAS_ATTRIBUTE_DISABLE_USAN
146inline static int jas_int_asl(int x, unsigned n)
147{
148 // Ensure that the shift of a negative value appears to behave as a
149 // signed arithmetic shift.
150 assert(((-1) << 1) == -2);
151 // The behavior is undefined when x is negative. */
152 // We tacitly assume the behavior is equivalent to a signed
153 // arithmetic left shift.
154 return x << n;
155}
156
157JAS_ATTRIBUTE_CONST
158JAS_ATTRIBUTE_DISABLE_USAN
159inline static int_least32_t jas_least32_asr(int_least32_t x, unsigned n)
160{
161 // Ensure that the shift of a negative value appears to behave as a
162 // signed arithmetic shift.
163 assert(((JAS_CAST(int_least32_t, -1)) >> 1) == JAS_CAST(int_least32_t, -1));
164 // The behavior is undefined when x is negative. */
165 // We tacitly assume the behavior is equivalent to a signed
166 // arithmetic right shift.
167 return x >> n;
168}
169
170JAS_ATTRIBUTE_CONST
171JAS_ATTRIBUTE_DISABLE_USAN
172inline static int_least32_t jas_least32_asl(int_least32_t x, unsigned n)
173{
174 // Ensure that the shift of a negative value appears to behave as a
175 // signed arithmetic shift.
176 assert(((JAS_CAST(int_least32_t, -1)) << 1) == JAS_CAST(int_least32_t, -2));
177 // The behavior is undefined when x is negative. */
178 // We tacitly assume the behavior is equivalent to a signed
179 // arithmetic left shift.
180 return x << n;
181}
182
183JAS_ATTRIBUTE_CONST
184JAS_ATTRIBUTE_DISABLE_USAN
185inline static int_fast32_t jas_fast32_asr(int_fast32_t x, unsigned n)
186{
187 // Ensure that the shift of a negative value appears to behave as a
188 // signed arithmetic shift.
189 assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
190 // The behavior is undefined when x is negative. */
191 // We tacitly assume the behavior is equivalent to a signed
192 // arithmetic right shift.
193 return x >> n;
194}
195
196JAS_ATTRIBUTE_CONST
197JAS_ATTRIBUTE_DISABLE_USAN
198inline static int_fast32_t jas_fast32_asl(int_fast32_t x, unsigned n)
199{
200 // Ensure that the shift of a negative value appears to behave as a
201 // signed arithmetic shift.
202 assert(((JAS_CAST(int_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
203 // The behavior is undefined when x is negative. */
204 // We tacitly assume the behavior is equivalent to a signed
205 // arithmetic left shift.
206 return x << n;
207}
208
209#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
210#pragma GCC diagnostic pop
211#endif
212
213/******************************************************************************\
214* Safe integer arithmetic (i.e., with overflow checking).
215\******************************************************************************/
216
217/* Compute the product of two size_t integers with overflow checking. */
218inline static bool jas_safe_size_mul(size_t x, size_t y, size_t *result)
219{
220#if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
221 size_t result_buffer;
222 if (!result)
223 result = &result_buffer;
224 return !__builtin_mul_overflow(x, y, result);
225#else
226 /* Check if overflow would occur */
227 if (x && y > SIZE_MAX / x) {
228 /* Overflow would occur. */
229 return false;
230 }
231 if (result) {
232 *result = x * y;
233 }
234 return true;
235#endif
236}
237
238/* Compute the product of three size_t integers with overflow checking. */
239inline static bool jas_safe_size_mul3(size_t a, size_t b, size_t c,
240 size_t *result)
241{
242 size_t tmp;
243 if (!jas_safe_size_mul(a, b, &tmp) ||
244 !jas_safe_size_mul(tmp, c, &tmp)) {
245 return false;
246 }
247 if (result) {
248 *result = tmp;
249 }
250 return true;
251}
252
253/* Compute the sum of two size_t integers with overflow checking. */
254inline static bool jas_safe_size_add(size_t x, size_t y, size_t *result)
255{
256#if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
257 size_t result_buffer;
258 if (!result)
259 result = &result_buffer;
260 return !__builtin_add_overflow(x, y, result);
261#else
262 if (y > SIZE_MAX - x) {
263 return false;
264 }
265 if (result) {
266 *result = x + y;
267 }
268 return true;
269#endif
270}
271
272/* Compute the difference of two size_t integers with overflow checking. */
273inline static bool jas_safe_size_sub(size_t x, size_t y, size_t *result)
274{
275#if jas_has_builtin(__builtin_sub_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
276 size_t result_buffer;
277 if (!result)
278 result = &result_buffer;
279 return !__builtin_sub_overflow(x, y, result);
280#else
281 if (y > x) {
282 return false;
283 }
284 if (result) {
285 *result = x - y;
286 }
287 return true;
288#endif
289}
290
291/* Compute the product of two int_fast32_t integers with overflow checking. */
292inline static bool jas_safe_intfast32_mul(int_fast32_t x, int_fast32_t y,
293 int_fast32_t *result)
294{
295#if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
296 int_fast32_t result_buffer;
297 if (!result)
298 result = &result_buffer;
299 return !__builtin_mul_overflow(x, y, result);
300#else
301 if (x > 0) {
302 /* x is positive */
303 if (y > 0) {
304 /* x and y are positive */
305 if (x > INT_FAST32_MAX / y) {
306 return false;
307 }
308 } else {
309 /* x positive, y nonpositive */
310 if (y < INT_FAST32_MIN / x) {
311 return false;
312 }
313 }
314 } else {
315 /* x is nonpositive */
316 if (y > 0) {
317 /* x is nonpositive, y is positive */
318 if (x < INT_FAST32_MIN / y) {
319 return false;
320 }
321 } else { /* x and y are nonpositive */
322 if (x != 0 && y < INT_FAST32_MAX / x) {
323 return false;
324 }
325 }
326 }
327
328 if (result) {
329 *result = x * y;
330 }
331 return true;
332#endif
333}
334
335/* Compute the product of three int_fast32_t integers with overflow checking. */
336inline static bool jas_safe_intfast32_mul3(int_fast32_t a, int_fast32_t b,
337 int_fast32_t c, int_fast32_t *result)
338{
339 int_fast32_t tmp;
340 if (!jas_safe_intfast32_mul(a, b, &tmp) ||
341 !jas_safe_intfast32_mul(tmp, c, &tmp)) {
342 return false;
343 }
344 if (result) {
345 *result = tmp;
346 }
347 return true;
348}
349
350/* Compute the sum of two int_fast32_t integers with overflow checking. */
351inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
352 int_fast32_t *result)
353{
354#if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
355 int_fast32_t result_buffer;
356 if (!result)
357 result = &result_buffer;
358 return !__builtin_add_overflow(x, y, result);
359#else
360 if ((y > 0 && x > INT_FAST32_MAX - y) ||
361 (y < 0 && x < INT_FAST32_MIN - y)) {
362 return false;
363 }
364 if (result) {
365 *result = x + y;
366 }
367 return true;
368#endif
369}
370
371#if 0
372/*
373This function is potentially useful but not currently used.
374So, it is commented out.
375*/
376inline static bool jas_safe_uint_mul(unsigned x, unsigned y, unsigned *result)
377{
378 /* Check if overflow would occur */
379 if (x && y > UINT_MAX / x) {
380 /* Overflow would occur. */
381 return false;
382 }
383 if (result) {
384 *result = x * y;
385 }
386 return true;
387}
388#endif
389
390#ifdef __cplusplus
391}
392#endif
393
394#endif
Compiler-related macros.
Primitive Types.