KiCad PCB EDA Suite
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 (C) 1992-2016 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
30#ifndef _3D_FASTMATH_H
31#define _3D_FASTMATH_H
32
33#include <cmath>
34#include <cstdint>
35#include <cstring>
36
37
38// Define this flag to use fast math optimizations
39#define FASTMATH_USE
40
41#define L1_CACHE_LINE_SIZE 64
42
43#ifdef FASTMATH_USE
44#define INTFLOORF(s) (lrintf( (s) - (0.5f - FLT_EPSILON) ))
45#else
46#define INTFLOORF(s) ((int)( floor(s) ))
47#endif
48
49
50// Fast Float Random Numbers
51// a small and fast implementation for random float numbers in the range [-1,1]
52// This is not thread safe
53float Fast_RandFloat();
54
55int Fast_rand( void );
56void Fast_srand( unsigned int seed );
57
63/*
64 pbrt source code is Copyright(c) 1998-2015
65 Matt Pharr, Greg Humphreys, and Wenzel Jakob.
66
67 This file is part of pbrt.
68
69 Redistribution and use in source and binary forms, with or without
70 modification, are permitted provided that the following conditions are
71 met:
72
73 - Redistributions of source code must retain the above copyright
74 notice, this list of conditions and the following disclaimer.
75
76 - Redistributions in binary form must reproduce the above copyright
77 notice, this list of conditions and the following disclaimer in the
78 documentation and/or other materials provided with the distribution.
79
80 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
81 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
82 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
83 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
84 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
85 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
86 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
87 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
88 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
89 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
90 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91
92 */
93
94
95// Global Inline Functions
96inline uint32_t FloatToBits( float f )
97{
98 uint32_t ui;
99
100 memcpy( &ui, &f, sizeof( float ) );
101
102 return ui;
103}
104
105
106inline float BitsToFloat( uint32_t ui )
107{
108 float f;
109
110 memcpy( &f, &ui, sizeof (uint32_t ) );
111
112 return f;
113}
114
115
116inline uint64_t FloatToBits( double f )
117{
118 uint64_t ui;
119
120 memcpy( &ui, &f, sizeof( double ) );
121
122 return ui;
123}
124
125
126inline double BitsToFloat( uint64_t ui )
127{
128 double f;
129
130 memcpy( &f, &ui, sizeof( uint64_t ) );
131
132 return f;
133}
134
135
136inline float NextFloatUp( float v )
137{
138 // Handle infinity and negative zero for _NextFloatUp()_
139 if( std::isinf( v ) && (v > 0.) )
140 return v;
141
142 if( v == -0.f )
143 v = 0.f;
144
145 // Advance _v_ to next higher float
146 uint32_t ui = FloatToBits( v );
147
148 if( v >= 0. )
149 ++ui;
150 else
151 --ui;
152
153 return BitsToFloat( ui );
154}
155
156
157inline float NextFloatDown( float v )
158{
159 // Handle infinity and positive zero for _NextFloatDown()_
160 if( std::isinf( v ) && (v < 0.) )
161 return v;
162
163 if( v == 0.f )
164 v = -0.f;
165
166 uint32_t ui = FloatToBits( v );
167
168 if( v > 0. )
169 --ui;
170 else
171 ++ui;
172
173 return BitsToFloat( ui );
174}
175
176#endif // 3D_FASTMATH_H
float Fast_RandFloat()
Definition: 3d_fastmath.cpp:45
int Fast_rand(void)
Definition: 3d_fastmath.cpp:58
uint32_t FloatToBits(float f)
This part contains some functions from the PBRT 3 source code.
Definition: 3d_fastmath.h:96
float NextFloatDown(float v)
Definition: 3d_fastmath.h:157
float NextFloatUp(float v)
Definition: 3d_fastmath.h:136
float BitsToFloat(uint32_t ui)
Definition: 3d_fastmath.h:106
void Fast_srand(unsigned int seed)
Definition: 3d_fastmath.cpp:65