|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Side Effects Software Inc. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + * |
| 22 | + * COMMENTS: |
| 23 | + * Miscellaneous math functions. |
| 24 | + */ |
| 25 | + |
| 26 | +#pragma once |
| 27 | + |
| 28 | +#ifndef __SYS_Math__ |
| 29 | +#define __SYS_Math__ |
| 30 | + |
| 31 | +#include "SYS_Types.h" |
| 32 | + |
| 33 | +#include <float.h> |
| 34 | +#include <limits> |
| 35 | +#include <math.h> |
| 36 | + |
| 37 | +// NOTE: |
| 38 | +// These have been carefully written so that in the case of equality |
| 39 | +// we always return the first parameter. This is so that NANs in |
| 40 | +// in the second parameter are suppressed. |
| 41 | +#define h_min(a, b) (((a) > (b)) ? (b) : (a)) |
| 42 | +#define h_max(a, b) (((a) < (b)) ? (b) : (a)) |
| 43 | +// DO NOT CHANGE THE ABOVE WITHOUT READING THE COMMENT |
| 44 | +#define h_abs(a) (((a) > 0) ? (a) : -(a)) |
| 45 | + |
| 46 | +static constexpr inline int16 SYSmin(int16 a, int16 b) { return h_min(a,b); } |
| 47 | +static constexpr inline int16 SYSmax(int16 a, int16 b) { return h_max(a,b); } |
| 48 | +static constexpr inline int16 SYSabs(int16 a) { return h_abs(a); } |
| 49 | +static constexpr inline int32 SYSmin(int32 a, int32 b) { return h_min(a,b); } |
| 50 | +static constexpr inline int32 SYSmax(int32 a, int32 b) { return h_max(a,b); } |
| 51 | +static constexpr inline int32 SYSabs(int32 a) { return h_abs(a); } |
| 52 | +static constexpr inline int64 SYSmin(int64 a, int64 b) { return h_min(a,b); } |
| 53 | +static constexpr inline int64 SYSmax(int64 a, int64 b) { return h_max(a,b); } |
| 54 | +static constexpr inline int64 SYSmin(int32 a, int64 b) { return h_min(a,b); } |
| 55 | +static constexpr inline int64 SYSmax(int32 a, int64 b) { return h_max(a,b); } |
| 56 | +static constexpr inline int64 SYSmin(int64 a, int32 b) { return h_min(a,b); } |
| 57 | +static constexpr inline int64 SYSmax(int64 a, int32 b) { return h_max(a,b); } |
| 58 | +static constexpr inline int64 SYSabs(int64 a) { return h_abs(a); } |
| 59 | +static constexpr inline uint16 SYSmin(uint16 a, uint16 b) { return h_min(a,b); } |
| 60 | +static constexpr inline uint16 SYSmax(uint16 a, uint16 b) { return h_max(a,b); } |
| 61 | +static constexpr inline uint32 SYSmin(uint32 a, uint32 b) { return h_min(a,b); } |
| 62 | +static constexpr inline uint32 SYSmax(uint32 a, uint32 b) { return h_max(a,b); } |
| 63 | +static constexpr inline uint64 SYSmin(uint64 a, uint64 b) { return h_min(a,b); } |
| 64 | +static constexpr inline uint64 SYSmax(uint64 a, uint64 b) { return h_max(a,b); } |
| 65 | +static constexpr inline fpreal32 SYSmin(fpreal32 a, fpreal32 b) { return h_min(a,b); } |
| 66 | +static constexpr inline fpreal32 SYSmax(fpreal32 a, fpreal32 b) { return h_max(a,b); } |
| 67 | +static constexpr inline fpreal64 SYSmin(fpreal64 a, fpreal64 b) { return h_min(a,b); } |
| 68 | +static constexpr inline fpreal64 SYSmax(fpreal64 a, fpreal64 b) { return h_max(a,b); } |
| 69 | + |
| 70 | +// Some systems have size_t as a seperate type from uint. Some don't. |
| 71 | +#if (defined(LINUX) && defined(IA64)) || defined(MBSD) |
| 72 | +static constexpr inline size_t SYSmin(size_t a, size_t b) { return h_min(a,b); } |
| 73 | +static constexpr inline size_t SYSmax(size_t a, size_t b) { return h_max(a,b); } |
| 74 | +#endif |
| 75 | + |
| 76 | +#undef h_min |
| 77 | +#undef h_max |
| 78 | +#undef h_abs |
| 79 | + |
| 80 | +#define h_clamp(val, min, max, tol) \ |
| 81 | + ((val <= min+tol) ? min : ((val >= max-tol) ? max : val)) |
| 82 | + |
| 83 | + static constexpr inline int |
| 84 | + SYSclamp(int v, int min, int max) |
| 85 | + { return h_clamp(v, min, max, 0); } |
| 86 | + |
| 87 | + static constexpr inline uint |
| 88 | + SYSclamp(uint v, uint min, uint max) |
| 89 | + { return h_clamp(v, min, max, 0); } |
| 90 | + |
| 91 | + static constexpr inline int64 |
| 92 | + SYSclamp(int64 v, int64 min, int64 max) |
| 93 | + { return h_clamp(v, min, max, int64(0)); } |
| 94 | + |
| 95 | + static constexpr inline uint64 |
| 96 | + SYSclamp(uint64 v, uint64 min, uint64 max) |
| 97 | + { return h_clamp(v, min, max, uint64(0)); } |
| 98 | + |
| 99 | + static constexpr inline fpreal32 |
| 100 | + SYSclamp(fpreal32 v, fpreal32 min, fpreal32 max, fpreal32 tol=(fpreal32)0) |
| 101 | + { return h_clamp(v, min, max, tol); } |
| 102 | + |
| 103 | + static constexpr inline fpreal64 |
| 104 | + SYSclamp(fpreal64 v, fpreal64 min, fpreal64 max, fpreal64 tol=(fpreal64)0) |
| 105 | + { return h_clamp(v, min, max, tol); } |
| 106 | + |
| 107 | +#undef h_clamp |
| 108 | + |
| 109 | +static inline fpreal64 SYSsqrt(fpreal64 arg) |
| 110 | +{ return ::sqrt(arg); } |
| 111 | +static inline fpreal32 SYSsqrt(fpreal32 arg) |
| 112 | +{ return ::sqrtf(arg); } |
| 113 | +static inline fpreal64 SYSatan2(fpreal64 a, fpreal64 b) |
| 114 | +{ return ::atan2(a, b); } |
| 115 | +static inline fpreal32 SYSatan2(fpreal32 a, fpreal32 b) |
| 116 | +{ return ::atan2(a, b); } |
| 117 | + |
| 118 | +static inline fpreal32 SYSabs(fpreal32 a) { return ::fabsf(a); } |
| 119 | +static inline fpreal64 SYSabs(fpreal64 a) { return ::fabs(a); } |
| 120 | + |
| 121 | +#endif |
0 commit comments