Files
handmadehero/handmade_intrinsics.h
T

77 lines
1.5 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
S32 RoundF32toS32(F32 Real32)
{
S32 Result = (S32)roundf(Real32);
return Result;
}
S32 FloorF32toS32(F32 Real32)
{
S32 Result = (S32)floorf(Real32);
return Result;
}
F32 Sin(F32 Angle)
{
F32 Result = sinf(Angle);
return Result;
}
F32 Cos(F32 Angle)
{
F32 Result = cosf(Angle);
return Result;
}
F32 ATan2(F32 Y, F32 X)
{
F32 Result = atan2f(Y, X);
return Result;
}
typedef struct bit_scan_result {
B32 Found;
U32 Index;
} bit_scan_result;
static bit_scan_result FindLeastSignificantSetBit(U32 Value)
{
bit_scan_result Result;
memset(&Result, 0, sizeof(Result));
#if COMPILER_MSVC
unsigned long Index;
Result.Found = _BitScanForward(&Index, Value);
Result.Index = (U32)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 = TRUE;
if(Result.Index > 0)
Result.Index--;
}
#else
for(U32 Test = 0; Test < 32; Test++) {
if(Value & (1 << Test)) {
Result.Index = Test;
Result.Found = TRUE;
break;
}
}
#endif
return Result;
}
#endif