KiCad PCB EDA Suite
3d_fastmath.h File Reference

Defines math related functions. More...

#include <string.h>
#include <stdint.h>
#include <cmath>

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

#define FASTMATH_USE

Definition at line 39 of file 3d_fastmath.h.

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

Definition at line 44 of file 3d_fastmath.h.

#define L1_CACHE_LINE_SIZE   64

Definition at line 41 of file 3d_fastmath.h.

Function Documentation

float BitsToFloat ( uint32_t  ui)
inline

Definition at line 106 of file 3d_fastmath.h.

Referenced by NextFloatDown(), and NextFloatUp().

107 {
108  float f;
109 
110  memcpy( &f, &ui, sizeof (uint32_t ) );
111 
112  return f;
113 }
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 }
int Fast_rand ( void  )

Definition at line 58 of file 3d_fastmath.cpp.

References s_nextRandSeed.

Referenced by CPOSTSHADER_SSAO::Shade().

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
float Fast_RandFloat ( )

Definition at line 45 of file 3d_fastmath.cpp.

References s_randSeed.

Referenced by CBOARDNORMAL::Generate(), CCOPPERNORMAL::Generate(), CMETALBRUSHEDNORMAL::Generate(), RAYPACKET::RAYPACKET(), RAYPACKET_InitRays_with2DDisplacement(), and UniformRandomHemisphereDirection().

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

Definition at line 65 of file 3d_fastmath.cpp.

References s_nextRandSeed.

66 {
67  s_nextRandSeed = seed;
68 }
static unsigned long int s_nextRandSeed
Definition: 3d_fastmath.cpp:56
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.

Referenced by NextFloatDown(), and NextFloatUp().

97 {
98  uint32_t ui;
99 
100  memcpy( &ui, &f, sizeof( float ) );
101 
102  return ui;
103 }
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 }
float NextFloatDown ( float  v)
inline

Definition at line 157 of file 3d_fastmath.h.

References BitsToFloat(), and FloatToBits().

Referenced by CLAYERITEM::Intersect(), CITEMLAYERCSG2D::Intersect(), CLAYERITEM::IntersectP(), RAYSEG2D::RAYSEG2D(), C3D_RENDER_RAYTRACING::reload(), CBBOX2D::ScaleNextDown(), CBBOX::ScaleNextDown(), CBBOX2D::ScaleNextUp(), and CBBOX::ScaleNextUp().

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

Definition at line 136 of file 3d_fastmath.h.

References BitsToFloat(), and FloatToBits().

Referenced by CLAYERITEM::Intersect(), CLAYERITEM::IntersectP(), C3D_RENDER_RAYTRACING::reload(), CBBOX2D::ScaleNextDown(), CBBOX::ScaleNextDown(), CBBOX2D::ScaleNextUp(), CBBOX::ScaleNextUp(), and C3D_RENDER_RAYTRACING::shadeHit().

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