Files
handmadehero/handmade_intrinsics.h
T
2026-07-14 13:21:04 -07:00

85 lines
1.7 KiB
C

#ifndef HANDMADE_INTRINSICS_H_SENTRY
#define HANDMADE_INTRINSICS_H_SENTRY
/* TODO: Re-implement these functions so we do not have to rely on
math.h. */
#include <math.h>
#include <string.h> // memset
float AbsoluteValue(float Real32)
{
float Result = fabs(Real32);
return Result;
}
int RoundFloatToInt(float Real32)
{
int Result = (int)roundf(Real32);
return Result;
}
int FloorFloatToInt(float Real32)
{
int Result = (int)floorf(Real32);
return Result;
}
float Sin(float Angle)
{
float Result = sinf(Angle);
return Result;
}
float Cos(float Angle)
{
float Result = cosf(Angle);
return Result;
}
float ATan2(float Y, float X)
{
float Result = atan2f(Y, X);
return Result;
}
struct bit_scan_result {
int Found;
unsigned int Index;
};
static struct bit_scan_result FindLeastSignificantSetBit(unsigned int Value)
{
struct bit_scan_result Result;
memset(&Result, 0, sizeof(Result));
#if COMPILER_MSVC
unsigned long Index;
Result.Found = _BitScanForward(&Index, Value);
Result.Index = (unsigned int)Index;
#elif COMPILER_CLANG
Result.Index = __builtin_ffsll(Value); /* NOTE: It is a GCC function,
but is supported by clang
compiler.
https://gcc.gnu.org/onlinedocs/gcc/Bit-Operation-Builtins.html*/
if(Result.Index) {
Result.Found = 1;
if(Result.Index > 0)
Result.Index--;
}
#else
for(unsigned int Test = 0; Test < 32; Test++) {
if(Value & (1 << Test)) {
Result.Index = Test;
Result.Found = 1;
break;
}
}
#endif
return Result;
}
#endif