Oolite
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#pragma once
28
29/* Clamp to range. */
32OOINLINE double OOClamp_0_1_d(double value) INLINE_CONST_FUNC;
33OOINLINE float OOClamp_0_max_f(float value, float max) INLINE_CONST_FUNC;
34OOINLINE double OOClamp_0_max_d(double value, double max) INLINE_CONST_FUNC;
35
36/* Linear interpolation. */
37OOINLINE float OOLerp(float v0, float v1, float fraction) INLINE_CONST_FUNC;
38OOINLINE double OOLerpd(double v0, double v1, double fraction) INLINE_CONST_FUNC;
39
40/* Round integer up to nearest power of 2. NOTE: these return 0 if the high bit of value is set. */
42{
43 return 0x80000000U >> (__builtin_clz(value - 1) - 1);
44}
45
47{
48 return 0x8000000000000000ULL >> (__builtin_clzll(value - 1) - 1);
49}
50
51#if __OBJC__
52#if OOLITE_64_BIT
53OOINLINE INLINE_CONST_FUNC NSUInteger OORoundUpToPowerOf2_NS(NSUInteger value)
54{
55 return OORoundUpToPowerOf2_64(value);
56}
57#else
58OOINLINE INLINE_CONST_FUNC NSUInteger OORoundUpToPowerOf2_NS(NSUInteger value)
59{
60 return OORoundUpToPowerOf2_32(value);
61}
62#endif
63#endif
64
65OOINLINE float OOClamp_0_1_f(float value)
66{
67 return fmax(0.0f, fmin(value, 1.0f));
68}
69
70OOINLINE float OOClamp_n1_1_f(float value)
71{
72 return fmax(-1.0f, fmin(value, 1.0f));
73}
74
75OOINLINE double OOClamp_0_1_d(double value)
76{
77 return fmax(0.0f, fmin(value, 1.0f));
78}
79
80OOINLINE float OOClamp_0_max_f(float value, float max)
81{
82 return fmax(0.0f, fmin(value, max));
83}
84
85OOINLINE double OOClamp_0_max_d(double value, double max)
86{
87 return fmax(0.0, fmin(value, max));
88}
89
90OOINLINE float OOLerp(float v0, float v1, float fraction)
91{
92 // Linear interpolation - equivalent to v0 * (1.0f - fraction) + v1 * fraction.
93 return v0 + fraction * (v1 - v0);
94}
95
96OOINLINE double OOLerpd(double v0, double v1, double fraction)
97{
98 // Linear interpolation - equivalent to v0 * (1.0 - fraction) + v1 * fraction.
99 return v0 + fraction * (v1 - v0);
100}
OOINLINE float OOClamp_n1_1_f(float value) INLINE_CONST_FUNC
OOINLINE double OOClamp_0_max_d(double value, double max) INLINE_CONST_FUNC
OOINLINE INLINE_CONST_FUNC uint64_t OORoundUpToPowerOf2_64(uint64_t value)
OOINLINE float OOLerp(float v0, float v1, float fraction) INLINE_CONST_FUNC
OOINLINE float OOClamp_0_max_f(float value, float max) INLINE_CONST_FUNC
OOINLINE double OOClamp_0_1_d(double value) INLINE_CONST_FUNC
OOINLINE INLINE_CONST_FUNC uint32_t OORoundUpToPowerOf2_32(uint32_t value)
OOINLINE float OOClamp_0_1_f(float value) INLINE_CONST_FUNC
OOINLINE double OOLerpd(double v0, double v1, double fraction) INLINE_CONST_FUNC
#define INLINE_CONST_FUNC
#define OOINLINE