HandmadeHero/project/platform/intrinsics.hpp

139 lines
2.4 KiB
C++
Raw Normal View History

2023-10-06 23:33:39 -07:00
#pragma once
#if INTELLISENSE_DIRECTIVES
#include <math.h>
2023-10-20 23:09:44 -07:00
#include "grime.hpp"
2023-10-06 23:33:39 -07:00
#endif
// TODO(Ed) : Convert all of these to platform-efficient versions
inline
f32 abs( f32 value )
{
f32 result = fabsf(value);
return result;
}
2023-12-31 23:53:15 -08:00
inline
s32 sign( s32 value )
{
s32 result = value >= 0 ? 1 : -1;
return result;
}
inline
f32 sqrt( f32 value )
{
f32 result = sqrtf(value);
return result;
}
inline
s32 sqrt(s32 value) {
if (value < 0) {
return -1;
}
if (value == 0 || value == 1) {
return value;
}
// Initial guess for the square root
s32 current_estimate = value;
s32 reciprocal_estimate = 1;
// Second estimate based on the reciprocal of our current guess
// Tolerance level for convergence
const s32 tolerance = 1;
while (current_estimate - reciprocal_estimate > tolerance) {
current_estimate = (current_estimate + reciprocal_estimate) / 2;
reciprocal_estimate = value / current_estimate;
}
return current_estimate;
}
2023-10-10 21:56:16 -07:00
2023-10-06 23:33:39 -07:00
inline
2023-10-10 21:56:16 -07:00
s32 floor( f32 value )
2023-10-06 23:33:39 -07:00
{
s32 result = scast(s32, floorf( value ));
return result;
}
inline
2023-10-10 21:56:16 -07:00
s32 round( f32 value )
2023-10-06 23:33:39 -07:00
{
2023-10-11 14:52:13 -07:00
s32 result = scast(s32, roundf( value ));
2023-10-06 23:33:39 -07:00
return result;
}
inline
2023-10-10 21:56:16 -07:00
s32 truncate( f32 value )
2023-10-06 23:33:39 -07:00
{
s32 result = scast(s32, value);
return result;
}
2023-10-20 23:09:44 -07:00
inline
2023-10-06 23:33:39 -07:00
f32 sine( f32 angle )
{
f32 result = sinf( angle );
return result;
}
2023-10-20 23:09:44 -07:00
inline
2023-10-06 23:33:39 -07:00
f32 cosine( f32 angle )
{
f32 result = cosf( angle );
return result;
}
2023-10-20 23:09:44 -07:00
inline
2023-10-06 23:33:39 -07:00
f32 arc_tangent( f32 Y, f32 X )
{
f32 result = atan2f( Y, X );
return result;
}
2023-10-20 23:09:44 -07:00
inline
b32 bitscan_forward( u32* index, u32 value )
{
b32 found = false;
// TODO(Ed) : This file can technically be generated with a metaprogram or via the preprocessor..
// We can keep The definitions for each of these based on the platform and
// Choose which one gets added to te project that way (keeps the runtime definition clean)
#if Compiler_MSVC
found = _BitScanForward( rcast(unsigned long*, index), value );
#elif Compiler_Clang
*index = __builtin_ctz( value );
found = index;
#else
for ( u32 test = 0; test < 32; ++ test )
{
if ( value & (1 << test ))
{
*index = test;
found = true;
break;
}
}
#endif
return found;
}
inline
u32 rotate_left( u32 mask, s32 shift )
{
u32 result = _rotl( mask, shift);
return result;
}
inline
u32 rotate_right( u32 mask, s32 shift )
{
u32 result = _rotl( mask, shift );
return result;
}