Line data Source code
1 0 : /* 2 : 3 : OOFastArithmetic.h 4 : 5 : Mathematical framework for Oolite. 6 : 7 : Oolite 8 : Copyright (C) 2004-2013 Giles C Williams and contributors 9 : 10 : This program is free software; you can redistribute it and/or 11 : modify it under the terms of the GNU General Public License 12 : as published by the Free Software Foundation; either version 2 13 : of the License, or (at your option) any later version. 14 : 15 : This program is distributed in the hope that it will be useful, 16 : but WITHOUT ANY WARRANTY; without even the implied warranty of 17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 : GNU General Public License for more details. 19 : 20 : You should have received a copy of the GNU General Public License 21 : along with this program; if not, write to the Free Software 22 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 23 : MA 02110-1301, USA. 24 : 25 : */ 26 : 27 : 28 : #ifndef INCLUDED_OOMATHS_h 29 : #error Do not include OOFastArithmetic.h directly; include OOMaths.h. 30 : #else 31 : 32 : 33 : /* Clamp to range. */ 34 : OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC; 35 : OOINLINE float OOClamp_n1_1_f(float value) INLINE_CONST_FUNC; 36 : OOINLINE double OOClamp_0_1_d(double value) INLINE_CONST_FUNC; 37 : OOINLINE float OOClamp_0_max_f(float value, float max) INLINE_CONST_FUNC; 38 : OOINLINE double OOClamp_0_max_d(double value, double max) INLINE_CONST_FUNC; 39 : 40 : /* Linear interpolation. */ 41 : OOINLINE float OOLerp(float v0, float v1, float fraction) INLINE_CONST_FUNC; 42 : OOINLINE double OOLerpd(double v0, double v1, double fraction) INLINE_CONST_FUNC; 43 : 44 : 45 : /* Round integer up to nearest power of 2. NOTE: these return 0 if the high bit of value is set. */ 46 : OOINLINE INLINE_CONST_FUNC uint32_t OORoundUpToPowerOf2_32(uint32_t value) 47 : { 48 : return 0x80000000U >> (__builtin_clz(value - 1) - 1); 49 : } 50 : 51 : 52 : OOINLINE INLINE_CONST_FUNC uint64_t OORoundUpToPowerOf2_64(uint64_t value) 53 : { 54 : return 0x8000000000000000ULL >> (__builtin_clzll(value - 1) - 1); 55 : } 56 : 57 : 58 : #if __OBJC__ 59 : #if OOLITE_64_BIT 60 : OOINLINE INLINE_CONST_FUNC NSUInteger OORoundUpToPowerOf2_NS(NSUInteger value) 61 : { 62 : return OORoundUpToPowerOf2_64(value); 63 : } 64 : #else 65 : OOINLINE INLINE_CONST_FUNC NSUInteger OORoundUpToPowerOf2_NS(NSUInteger value) 66 : { 67 : return OORoundUpToPowerOf2_32(value); 68 : } 69 : #endif 70 : #endif 71 : 72 : 73 : OOINLINE float OOClamp_0_1_f(float value) 74 : { 75 : return fmax(0.0f, fmin(value, 1.0f)); 76 : } 77 : 78 : OOINLINE float OOClamp_n1_1_f(float value) 79 : { 80 : return fmax(-1.0f, fmin(value, 1.0f)); 81 : } 82 : 83 : OOINLINE double OOClamp_0_1_d(double value) 84 : { 85 : return fmax(0.0f, fmin(value, 1.0f)); 86 : } 87 : 88 : OOINLINE float OOClamp_0_max_f(float value, float max) 89 : { 90 : return fmax(0.0f, fmin(value, max)); 91 : } 92 : 93 : OOINLINE double OOClamp_0_max_d(double value, double max) 94 : { 95 : return fmax(0.0, fmin(value, max)); 96 : } 97 : 98 : 99 : OOINLINE float OOLerp(float v0, float v1, float fraction) 100 : { 101 : // Linear interpolation - equivalent to v0 * (1.0f - fraction) + v1 * fraction. 102 : return v0 + fraction * (v1 - v0); 103 : } 104 : 105 : OOINLINE double OOLerpd(double v0, double v1, double fraction) 106 : { 107 : // Linear interpolation - equivalent to v0 * (1.0 - fraction) + v1 * fraction. 108 : return v0 + fraction * (v1 - v0); 109 : } 110 : 111 : 112 : 113 : #endif /* INCLUDED_OOMATHS_h */