CLibs
Loading...
Searching...
No Matches
simple_math.h
Go to the documentation of this file.
1/*
2 * Functions and macros for simple math operations like min/max of two numbers.
3 *
4 *
5 * Created by Michal Pitner on 13.06.2025.
6 */
7
8#ifndef SIMPLE_MATH_H
9#define SIMPLE_MATH_H
10
11#include "attributes.h"
12#include "extra_types.h"
13
14
16typedef enum {
19} sign_t;
20
21
26#define sign_flipped( SIGN ) ( -( SIGN ) )
27
28
32LibraryDefined Mathematical inline signed sgn_64( const int64_t n )
33{
34 return n == 0 ? 0 : n < 0 ? -1 : 1;
35}
36
49#define min_m( NUM_A, NUM_B ) ( ( NUM_A ) < ( NUM_B ) ? ( NUM_A ) : ( NUM_B ) )
50
63#define max_m( NUM_A, NUM_B ) ( ( NUM_A ) > ( NUM_B ) ? ( NUM_A ) : ( NUM_B ) )
64
65
66LibraryDefined Mathematical inline int64_t min_i64( const int64_t a, const int64_t b )
67{
68 return min_m( a, b );
69}
70LibraryDefined Mathematical inline uint64_t min_u64( const uint64_t a, const uint64_t b )
71{
72 return min_m( a, b );
73}
74
75LibraryDefined Mathematical inline int64_t max_i64( const int64_t a, const int64_t b )
76{
77 return max_m( a, b );
78}
79LibraryDefined Mathematical inline uint64_t max_u64( const uint64_t a, const uint64_t b )
80{
81 return max_m( a, b );
82}
83
84
95LibraryDefined Mathematical inline bool is_within( const int64_t low, const int64_t num,
96 const int64_t high )
97{
98 return num >= low && num <= high;
99}
100
102LibraryDefined Mathematical inline bool is_power_of_two( const uint64_t num )
103{
104 return num != 0 && !( num & ( num - 1 ) );
105}
106
107
108#endif //SIMPLE_MATH_H
Function and variable attributes.
#define LibraryDefined
Definition attributes.h:107
#define Mathematical
Definition attributes.h:113
LibraryDefined Mathematical signed sgn_64(const int64_t n)
Definition simple_math.h:32
LibraryDefined Mathematical bool is_power_of_two(const uint64_t num)
Returns true if num is a power of two.
Definition simple_math.h:102
LibraryDefined Mathematical uint64_t min_u64(const uint64_t a, const uint64_t b)
Definition simple_math.h:70
#define max_m(NUM_A, NUM_B)
Definition simple_math.h:63
sign_t
The signs are equal to their supposed value.
Definition simple_math.h:16
@ SIGN_NEG
Definition simple_math.h:17
@ SIGN_POS
Definition simple_math.h:18
LibraryDefined Mathematical int64_t min_i64(const int64_t a, const int64_t b)
Definition simple_math.h:66
LibraryDefined Mathematical uint64_t max_u64(const uint64_t a, const uint64_t b)
Definition simple_math.h:79
#define min_m(NUM_A, NUM_B)
Definition simple_math.h:49
LibraryDefined Mathematical bool is_within(const int64_t low, const int64_t num, const int64_t high)
Definition simple_math.h:95
LibraryDefined Mathematical int64_t max_i64(const int64_t a, const int64_t b)
Definition simple_math.h:75