KiCad PCB EDA Suite
3d_fastmath.h File Reference

Defines math related functions. More...

#include <cmath>
#include <cstdint>
#include <cstring>

Go to the source code of this file.

Macros

#define FASTMATH_USE
 
#define L1_CACHE_LINE_SIZE   64
 
#define INTFLOORF(s)   (lrintf( (s) - (0.5f - FLT_EPSILON) ))
 

Functions

float Fast_RandFloat ()
 
int Fast_rand (void)
 
void Fast_srand (unsigned int seed)
 
uint32_t FloatToBits (float f)
 This part contains some functions from the PBRT 3 source code. More...
 
float BitsToFloat (uint32_t ui)
 
uint64_t FloatToBits (double f)
 
double BitsToFloat (uint64_t ui)
 
float NextFloatUp (float v)
 
float NextFloatDown (float v)
 

Detailed Description

Defines math related functions.

Definition in file 3d_fastmath.h.

Macro Definition Documentation

◆ FASTMATH_USE

#define FASTMATH_USE

Definition at line 39 of file 3d_fastmath.h.

◆ INTFLOORF

#define INTFLOORF (   s)    (lrintf( (s) - (0.5f - FLT_EPSILON) ))

Definition at line 44 of file 3d_fastmath.h.

◆ L1_CACHE_LINE_SIZE

#define L1_CACHE_LINE_SIZE   64

Definition at line 41 of file 3d_fastmath.h.

Function Documentation

◆ BitsToFloat() [1/2]

float BitsToFloat ( uint32_t  ui)
inline

Definition at line 106 of file 3d_fastmath.h.

107{
108 float f;
109
110 memcpy( &f, &ui, sizeof (uint32_t ) );
111
112 return f;
113}

Referenced by NextFloatDown(), and NextFloatUp().

◆ BitsToFloat() [2/2]

double BitsToFloat ( uint64_t  ui)
inline

Definition at line 126 of file 3d_fastmath.h.

127{
128 double f;
129
130 memcpy( &f, &ui, sizeof( uint64_t ) );
131
132 return f;
133}

◆ Fast_rand()

int Fast_rand ( void  )

Definition at line 58 of file 3d_fastmath.cpp.

59{
60 s_nextRandSeed = s_nextRandSeed * 1103515245 + 12345;
61
62 return (unsigned int)(s_nextRandSeed >> 16) & 0x7FFF;
63}
static unsigned long int s_nextRandSeed
Definition: 3d_fastmath.cpp:56

References s_nextRandSeed.

Referenced by POST_SHADER_SSAO::Shade().

◆ Fast_RandFloat()

float Fast_RandFloat ( )

Definition at line 45 of file 3d_fastmath.cpp.

46{
47 s_randSeed *= 16807;
48
49 return (float)s_randSeed * 4.6566129e-010f;
50}
static int s_randSeed
Definition: 3d_fastmath.cpp:41

References s_randSeed.

Referenced by RAYPACKET::RAYPACKET(), RAYPACKET_InitRays_with2DDisplacement(), and UniformRandomHemisphereDirection().

◆ Fast_srand()

void Fast_srand ( unsigned int  seed)

Definition at line 65 of file 3d_fastmath.cpp.

66{
67 s_nextRandSeed = seed;
68}

References s_nextRandSeed.

◆ FloatToBits() [1/2]

uint64_t FloatToBits ( double  f)
inline

Definition at line 116 of file 3d_fastmath.h.

117{
118 uint64_t ui;
119
120 memcpy( &ui, &f, sizeof( double ) );
121
122 return ui;
123}

◆ FloatToBits() [2/2]

uint32_t FloatToBits ( float  f)
inline

This part contains some functions from the PBRT 3 source code.

https://github.com/mmp/pbrt-v3/blob/master/src/core/pbrt.h

Definition at line 96 of file 3d_fastmath.h.

97{
98 uint32_t ui;
99
100 memcpy( &ui, &f, sizeof( float ) );
101
102 return ui;
103}

Referenced by NextFloatDown(), and NextFloatUp().

◆ NextFloatDown()

float NextFloatDown ( float  v)
inline

Definition at line 157 of file 3d_fastmath.h.

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}
uint32_t FloatToBits(float f)
This part contains some functions from the PBRT 3 source code.
Definition: 3d_fastmath.h:96
float BitsToFloat(uint32_t ui)
Definition: 3d_fastmath.h:106

References BitsToFloat(), and FloatToBits().

Referenced by LAYER_ITEM::Intersect(), LAYER_ITEM::IntersectP(), RAYSEG2D::RAYSEG2D(), RENDER_3D_RAYTRACE::Reload(), BBOX_2D::ScaleNextDown(), BBOX_3D::ScaleNextDown(), BBOX_2D::ScaleNextUp(), and BBOX_3D::ScaleNextUp().

◆ NextFloatUp()

float NextFloatUp ( float  v)
inline

Definition at line 136 of file 3d_fastmath.h.

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}

References BitsToFloat(), and FloatToBits().

Referenced by LAYER_ITEM::Intersect(), LAYER_ITEM::IntersectP(), RENDER_3D_RAYTRACE::Reload(), BBOX_2D::ScaleNextDown(), BBOX_3D::ScaleNextDown(), BBOX_2D::ScaleNextUp(), and BBOX_3D::ScaleNextUp().