KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
intmath.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 kos/intmath.h
4 Copyright (C) 2026 Paul Cercueil
5
6 Integer math helper functions
7*/
8
9/** \file kos/intmath.h
10 \brief Functions to help with integer math.
11 \ingroup intmath
12
13 \author Paul Cercueil
14*/
15
16#ifndef __KOS_INTMATH_H
17#define __KOS_INTMATH_H
18
19#include <kos/cdefs.h>
20__BEGIN_DECLS
21
22#include <stdbool.h>
23
24static inline bool is_power_of_two(unsigned int val)
25{
26 return (val & (val - 1)) == 0;
27}
28
29static inline unsigned int log2_rdown(unsigned int val)
30{
31 return 31 - __builtin_clz(val);
32}
33
34static inline unsigned int log2_rup(unsigned int val)
35{
36 return log2_rdown(val) + !is_power_of_two(val);
37}
38
39__END_DECLS
40#endif /* __KOS_INTMATH_H */
Various common macros used throughout the codebase.
static bool is_power_of_two(unsigned int val)
Definition intmath.h:24
static unsigned int log2_rup(unsigned int val)
Definition intmath.h:34
static unsigned int log2_rdown(unsigned int val)
Definition intmath.h:29