87 lines
1.7 KiB
C
87 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 absolute_value(float real32)
|
|
{
|
|
float result = fabs(real32);
|
|
return result;
|
|
}
|
|
|
|
int round_float_to_int(float real32)
|
|
{
|
|
int result = (int)roundf(real32);
|
|
return result;
|
|
}
|
|
|
|
int floor_float_to_int(float real32)
|
|
{
|
|
int result = (int)floorf(real32);
|
|
return result;
|
|
}
|
|
|
|
float hm_sin(float angle)
|
|
{
|
|
float result = sinf(angle);
|
|
return result;
|
|
}
|
|
|
|
float hm_cos(float angle)
|
|
{
|
|
float result = cosf(angle);
|
|
return result;
|
|
}
|
|
|
|
float hm_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
|
|
find_least_significant_set_bit(unsigned int value)
|
|
{
|
|
/* TODO: */
|
|
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
|
|
|