ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1#pragma once
2
23
24#include <stdbool.h>
25#include <stddef.h>
26#include <stdint.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/* ============================================================================
33 * Mathematical Macros
34 * ============================================================================
35 */
36
57#define ROUND(xfloat) (int)(0.5f + (xfloat))
58
59/* ============================================================================
60 * Clamping Functions
61 * ============================================================================
62 */
63
85static inline uint8_t clamp_rgb(int value) {
86 if (value < 0)
87 return 0;
88 if (value > 255)
89 return 255;
90 return (uint8_t)value;
91}
92
93/* ============================================================================
94 * Power-of-Two Utilities
95 * ============================================================================
96 */
97
121static inline bool math_is_power_of_two(size_t n) {
122 return n && !(n & (n - 1));
123}
124
151static inline size_t math_next_power_of_two(size_t n) {
152 if (n == 0)
153 return 1;
154 n--;
155 n |= n >> 1;
156 n |= n >> 2;
157 n |= n >> 4;
158 n |= n >> 8;
159 n |= n >> 16;
160#if SIZE_MAX > UINT32_MAX
161 n |= n >> 32;
162#endif
163 return n + 1;
164}
165
166#ifdef __cplusplus
167}
168#endif
unsigned char uint8_t