Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOFastArithmetic.h
Go to the documentation of this file.
1/*
2
3OOFastArithmetic.h
4
5Mathematical framework for Oolite.
6
7Oolite
8Copyright (C) 2004-2013 Giles C Williams and contributors
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 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. */
34OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC;
35OOINLINE float OOClamp_n1_1_f(float value) INLINE_CONST_FUNC;
36OOINLINE double OOClamp_0_1_d(double value) INLINE_CONST_FUNC;
37OOINLINE float OOClamp_0_max_f(float value, float max) INLINE_CONST_FUNC;
38OOINLINE double OOClamp_0_max_d(double value, double max) INLINE_CONST_FUNC;
39
40/* Linear interpolation. */
41OOINLINE float OOLerp(float v0, float v1, float fraction) INLINE_CONST_FUNC;
42OOINLINE 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. */
46OOINLINE INLINE_CONST_FUNC uint32_t OORoundUpToPowerOf2_32(uint32_t value)
47{
48 return 0x80000000U >> (__builtin_clz(value - 1) - 1);
49}
50
51
52OOINLINE 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
60OOINLINE INLINE_CONST_FUNC NSUInteger OORoundUpToPowerOf2_NS(NSUInteger value)
61{
62 return OORoundUpToPowerOf2_64(value);
63}
64#else
65OOINLINE INLINE_CONST_FUNC NSUInteger OORoundUpToPowerOf2_NS(NSUInteger value)
66{
67 return OORoundUpToPowerOf2_32(value);
68}
69#endif
70#endif
71
72
73OOINLINE float OOClamp_0_1_f(float value)
74{
75 return fmax(0.0f, fmin(value, 1.0f));
76}
77
78OOINLINE float OOClamp_n1_1_f(float value)
79{
80 return fmax(-1.0f, fmin(value, 1.0f));
81}
82
83OOINLINE double OOClamp_0_1_d(double value)
84{
85 return fmax(0.0f, fmin(value, 1.0f));
86}
87
88OOINLINE float OOClamp_0_max_f(float value, float max)
89{
90 return fmax(0.0f, fmin(value, max));
91}
92
93OOINLINE double OOClamp_0_max_d(double value, double max)
94{
95 return fmax(0.0, fmin(value, max));
96}
97
98
99OOINLINE 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
105OOINLINE 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 */
#define INLINE_CONST_FUNC
#define OOINLINE