KiCad PCB EDA Suite
Loading...
Searching...
No Matches
3d_fastmath.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015-2016 Mario Luzeiro <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
25
26#ifndef _3D_FASTMATH_H
27#define _3D_FASTMATH_H
28
29#include <cmath>
30#include <cstdint>
31#include <cstring>
32
33
34// Define this flag to use fast math optimizations
35#define FASTMATH_USE
36
37#define L1_CACHE_LINE_SIZE 64
38
39#ifdef FASTMATH_USE
40#define INTFLOORF(s) (lrintf( (s) - (0.5f - FLT_EPSILON) ))
41#else
42#define INTFLOORF(s) ((int)( floor(s) ))
43#endif
44
45
46// Fast Float Random Numbers
47// a small and fast implementation for random float numbers in the range [-1,1]
48// This is not thread safe
49float Fast_RandFloat();
50
51int Fast_rand( void );
52void Fast_srand( unsigned int seed );
53
58
59/*
60 pbrt source code is Copyright(c) 1998-2015
61 Matt Pharr, Greg Humphreys, and Wenzel Jakob.
62
63 This file is part of pbrt.
64
65 Redistribution and use in source and binary forms, with or without
66 modification, are permitted provided that the following conditions are
67 met:
68
69 - Redistributions of source code must retain the above copyright
70 notice, this list of conditions and the following disclaimer.
71
72 - Redistributions in binary form must reproduce the above copyright
73 notice, this list of conditions and the following disclaimer in the
74 documentation and/or other materials provided with the distribution.
75
76 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
77 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
78 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
79 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
80 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
81 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
82 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
83 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
84 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
85 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
86 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87
88 */
89
90
91// Global Inline Functions
92inline uint32_t FloatToBits( float f )
93{
94 uint32_t ui;
95
96 memcpy( &ui, &f, sizeof( float ) );
97
98 return ui;
99}
100
101
102inline float BitsToFloat( uint32_t ui )
103{
104 float f;
105
106 memcpy( &f, &ui, sizeof (uint32_t ) );
107
108 return f;
109}
110
111
112inline uint64_t FloatToBits( double f )
113{
114 uint64_t ui;
115
116 memcpy( &ui, &f, sizeof( double ) );
117
118 return ui;
119}
120
121
122inline double BitsToFloat( uint64_t ui )
123{
124 double f;
125
126 memcpy( &f, &ui, sizeof( uint64_t ) );
127
128 return f;
129}
130
131
132inline float NextFloatUp( float v )
133{
134 // Handle infinity and negative zero for _NextFloatUp()_
135 if( std::isinf( v ) && (v > 0.) )
136 return v;
137
138 if( v == -0.f )
139 v = 0.f;
140
141 // Advance _v_ to next higher float
142 uint32_t ui = FloatToBits( v );
143
144 if( v >= 0. )
145 ++ui;
146 else
147 --ui;
148
149 return BitsToFloat( ui );
150}
151
152
153inline float NextFloatDown( float v )
154{
155 // Handle infinity and positive zero for _NextFloatDown()_
156 if( std::isinf( v ) && (v < 0.) )
157 return v;
158
159 if( v == 0.f )
160 v = -0.f;
161
162 uint32_t ui = FloatToBits( v );
163
164 if( v > 0. )
165 --ui;
166 else
167 ++ui;
168
169 return BitsToFloat( ui );
170}
171
172#endif // 3D_FASTMATH_H
float Fast_RandFloat()
int Fast_rand(void)
uint32_t FloatToBits(float f)
This part contains some functions from the PBRT 3 source code.
Definition 3d_fastmath.h:92
float NextFloatDown(float v)
float NextFloatUp(float v)
float BitsToFloat(uint32_t ui)
void Fast_srand(unsigned int seed)
const uint32_t seed