Iridescence 1.00
Embedded Graphic Framework
Loading...
Searching...
No Matches
ds_math.h
1/* Copyright (C) 2017 - 2024, Thornwave Labs Inc - All Rights Reserved.
2 * Unauthorized copying of this file, via any medium is strictly prohibited.
3 * Proprietary and confidential.
4 * Written by Razvan Turiac <razvan.turiac@thornwave.com>
5*/
6
7#ifndef _DS_MATH_H
8#define _DS_MATH_H
9
10
11#include <stdint.h>
12#include <stdlib.h>
13#include <math.h>
14
15
16
22template <typename T>
23static inline void swap(T &a, T &b)
24{
25 const T temp = a;
26 a = b;
27 b = temp;
28}
29
30
31template <typename T, typename Q>
32static inline T min(T a, Q b)
33{
34 return (a < b) ? a : b;
35}
36
37
38template <typename T, typename Q>
39static inline T max(T a, Q b)
40{
41 return (a > b) ? a : b;
42}
43
44
45template <typename T, typename Q, typename P>
46static inline T sat(T x, Q a, P b)
47{
48 if (x < a)
49 return a;
50 else if (x > b)
51 return b;
52 else
53 return x;
54}
55
56
57#ifdef DS_MATH_LIB
58
59
60static inline float frac(float x)
61{
62 return x - (int32_t)x;
63}
64
65
66static inline float fsat(float x, float min, float max)
67{
68 return fmin(fmax(min, x), max);
69}
70
71
72static inline int32_t round_closest_signed(int32_t n, int32_t d)
73{
74 return (((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d)) * d;
75}
76
77
78static inline uint32_t round_closest_unsigned(uint32_t n, uint32_t d)
79{
80 return ((n + d / 2) / d) * d;
81}
82
83
84#endif
85
86
87#endif