aboutsummaryrefslogtreecommitdiff
path: root/code/handmade_intrinsics.h
blob: 2a3a69ef852a11c84f7de76edfb10f13aa73fe2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* date = May 13th 2025 1:41 pm */

#ifndef HANDMADE_INTRINSICS_H
#define HANDMADE_INTRINSICS_H

//
// TODO(casey): Remove math.h
//
#include <math.h>

inline s32
RoundReal32ToInt32(r32 Real32)
{
    s32 Result = (s32)(roundf(Real32));
    return Result;
}

inline u32
RoundReal32ToUInt32(r32 Real32)
{
    u32 Result = (u32)(roundf(Real32));
    return Result;
}

inline u32
TruncateReal32ToUInt32(r32 Real32)
{
    u32 Result = (u32)Real32;
    return Result;
}

inline s32
TruncateReal32ToInt32(r32 Real32)
{
    s32 Result = (s32)Real32;
    return Result;
}

inline s32
FloorReal32ToInt32(r32 Real32)
{
    s32 Result = (s32)floorf(Real32);
    return Result;
}

inline r32
Sin(r32 Angle)
{
    r32 Result = sinf(Angle);
    return Result;
}

inline r32
Cos(r32 Angle)
{
    r32 Result = cosf(Angle);
    return Result;
}

inline r32
Atan2(r32 Y, r32 X)
{
    r32 Result = atan2f(Y, X);
    return Result;
}

struct bit_scan_result
{
    b32 Found;
    u32 Index;
};

internal inline bit_scan_result
FindLeastSignificantSetBit(u32 Value)
{
    bit_scan_result Result = {};
    
#if 0
#elif COMPILER_GNU
    Result.Index = __builtin_ffs(Value);
    if(Result.Index)
    {
        Result.Found = true;
        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 //HANDMADE_INTRINSICS_H