Loading...
Searching...
No Matches
math.h File Reference

Math helper macros. More...

Detailed Description

Math helper macros.

Author
Benjamin Valentin benja.nosp@m.min..nosp@m.valen.nosp@m.tin@.nosp@m.ml-pa.nosp@m..com

Definition in file math.h.

+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

#define SIGNOF(a)
 Returns the sign of a, either -1 or 1.
 
#define DIV_ROUND(a, b)   ((SIGNOF(a) * ((SIGNOF(a) * (a)) + (SIGNOF(b) * (b)) / 2) / (b)))
 Calculates a/ b with arithmetic rounding (.5 away from zero)
 
#define DIV_ROUND_UP(a, b)
 Calculates a/ b, always rounding up (towards positive infinity)
 
#define DIV_ROUND_INF(a, b)   (SIGNOF(a) * (((SIGNOF(a) * (a)) + (SIGNOF(b) * (b)) - 1) / (b)))
 Calculates a/ b, always rounding away from zero (towards positive and negative inf)
 
#define MATH_ALIGN(num, chunk)   ((chunk) * DIV_ROUND_INF(num, chunk))
 Align num with the next multiple of chunk.
 

Macro Definition Documentation

◆ DIV_ROUND

#define DIV_ROUND (   a,
 
)    ((SIGNOF(a) * ((SIGNOF(a) * (a)) + (SIGNOF(b) * (b)) / 2) / (b)))

Calculates a/ b with arithmetic rounding (.5 away from zero)

Definition at line 38 of file math.h.

◆ DIV_ROUND_INF

#define DIV_ROUND_INF (   a,
 
)    (SIGNOF(a) * (((SIGNOF(a) * (a)) + (SIGNOF(b) * (b)) - 1) / (b)))

Calculates a/ b, always rounding away from zero (towards positive and negative inf)

Definition at line 49 of file math.h.

◆ DIV_ROUND_UP

#define DIV_ROUND_UP (   a,
 
)
Value:
((SIGNOF(a)>0 && SIGNOF(b)>0) ? (((a) + (b) - 1) / (b)):\
(SIGNOF(a)<0 && SIGNOF(b)<0) ? (((a) + (b) + 1) / (b)): (a) / (b))
#define SIGNOF(a)
Returns the sign of a, either -1 or 1.
Definition math.h:29

Calculates a/ b, always rounding up (towards positive infinity)

Definition at line 43 of file math.h.

◆ MATH_ALIGN

#define MATH_ALIGN (   num,
  chunk 
)    ((chunk) * DIV_ROUND_INF(num, chunk))

Align num with the next multiple of chunk.

Definition at line 54 of file math.h.

◆ SIGNOF

#define SIGNOF (   a)
Value:
_Generic(a, unsigned char: 1, \
unsigned short: 1, \
unsigned int: 1, \
unsigned long: 1, \
unsigned long long: 1, \
default: ((a) <= 0 ? ((a) == 0 ? 1L : -1L ): 1L))

Returns the sign of a, either -1 or 1.

Definition at line 29 of file math.h.