jas_math.h Source File

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 <stdbool.h>
86 #include <stdint.h>
87
88 #ifdef __cplusplus
89 extern "C" {
90 #endif
91
92 /******************************************************************************\
93 * Macros
94 \******************************************************************************/
95
96 #define JAS_KIBI JAS_CAST(size_t, 1024)
97 #define JAS_MEBI (JAS_KIBI * JAS_KIBI)
98
99 /* Compute the absolute value. */
100 #define JAS_ABS(x) \
101 (((x) >= 0) ? (x) : (-(x)))
102
103 /* Compute the minimum of two values. */
104 #define JAS_MIN(x, y) \
105 (((x) < (y)) ? (x) : (y))
106
107 /* Compute the maximum of two values. */
108 #define JAS_MAX(x, y) \
109 (((x) > (y)) ? (x) : (y))
110
111 /* Compute the remainder from division (where division is defined such
112 that the remainder is always nonnegative). */
113 #define JAS_MOD(x, y) \
114 (((x) < 0) ? (((-x) % (y)) ? ((y) - ((-(x)) % (y))) : (0)) : ((x) % (y)))
115
116 /* Compute the integer with the specified number of least significant bits
117 set to one. */
118 #define JAS_ONES(n) \
119 ((1 << (n)) - 1)
120
121 /******************************************************************************\
122 *
123 \******************************************************************************/
124
125 JAS_ATTRIBUTE_DISABLE_USAN
126 inline static int jas_int_asr(int x, int n)
127 {
128 // Ensure that the shift of a negative value appears to behave as a
129 // signed arithmetic shift.
130 assert(((-1) >> 1) == -1);
131 assert(n >= 0);
132 // The behavior is undefined when x is negative. */
133 // We tacitly assume the behavior is equivalent to a signed
134 // arithmetic right shift.
135 return x >> n;
136 }
137
138 JAS_ATTRIBUTE_DISABLE_USAN
139 inline static int jas_int_asl(int x, int n)
140 {
141 // Ensure that the shift of a negative value appears to behave as a
142 // signed arithmetic shift.
143 assert(((-1) << 1) == -2);
144 assert(n >= 0);
145 // The behavior is undefined when x is negative. */
146 // We tacitly assume the behavior is equivalent to a signed
147 // arithmetic left shift.
148 return x << n;
149 }
150
151 JAS_ATTRIBUTE_DISABLE_USAN
152 inline static int jas_fast32_asr(int_fast32_t x, int n)
153 {
154 // Ensure that the shift of a negative value appears to behave as a
155 // signed arithmetic shift.
156 assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
157 assert(n >= 0);
158 // The behavior is undefined when x is negative. */
159 // We tacitly assume the behavior is equivalent to a signed
160 // arithmetic right shift.
161 return x >> n;
162 }
163
164 JAS_ATTRIBUTE_DISABLE_USAN
165 inline static int jas_fast32_asl(int_fast32_t x, int 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_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
170 assert(n >= 0);
171 // The behavior is undefined when x is negative. */
172 // We tacitly assume the behavior is equivalent to a signed
173 // arithmetic left shift.
174 return x << n;
175 }
176
177 /******************************************************************************\
178 * Safe integer arithmetic (i.e., with overflow checking).
179 \******************************************************************************/
180
181 /* Compute the product of two size_t integers with overflow checking. */
182 inline static bool jas_safe_size_mul(size_t x, size_t y, size_t *result)
183 {
184 /* Check if overflow would occur */
185 if (x && y > SIZE_MAX / x) {
186 /* Overflow would occur. */
187 return false;
188 }
189 if (result) {
190 *result = x * y;
191 }
192 return true;
193 }
194
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 integer 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 integer 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 sum of two size_t integer 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 inline static bool jas_safe_intfast32_mul3(int_fast32_t a, int_fast32_t b,
271 int_fast32_t c, int_fast32_t *result)
272 {
273 int_fast32_t tmp;
274 if (!jas_safe_intfast32_mul(a, b, &tmp) ||
275 !jas_safe_intfast32_mul(tmp, c, &tmp)) {
276 return false;
277 }
278 if (result) {
279 *result = tmp;
280 }
281 return true;
282 }
283
284 /* Compute the sum of two size_t integer with overflow checking. */
285 inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
286 int_fast32_t *result)
287 {
288 if ((y > 0 && x > INT_FAST32_MAX - y) ||
289 (y < 0 && x < INT_FAST32_MIN - y)) {
290 return false;
291 }
292 if (result) {
293 *result = x + y;
294 }
295 return true;
296 }
297
298 #ifdef __cplusplus
299 }
300 #endif
301
302 #endif