math.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 ML!PA Consulting GmbH
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
28 #define SIGNOF(a) _Generic(a, unsigned char: 1, \
29  unsigned short: 1, \
30  unsigned int: 1, \
31  unsigned long: 1, \
32  unsigned long long: 1, \
33  default: ((a) <= 0 ? ((a) == 0 ? 1L : -1L ): 1L))
37 #define DIV_ROUND(a, b) ((SIGNOF(a) * ((SIGNOF(a) * (a)) + (SIGNOF(b) * (b)) / 2) / (b)))
38 
42 #define DIV_ROUND_UP(a, b) ((SIGNOF(a)>0 && SIGNOF(b)>0) ? (((a) + (b) - 1) / (b)):\
43  (SIGNOF(a)<0 && SIGNOF(b)<0) ? (((a) + (b) + 1) / (b)): (a) / (b))
44 
48 #define DIV_ROUND_INF(a, b) (SIGNOF(a) * (((SIGNOF(a) * (a)) + (SIGNOF(b) * (b)) - 1) / (b)))
49 
53 #define MATH_ALIGN(num, chunk) ((chunk) * DIV_ROUND_INF(num, chunk))
54 
55 #ifdef __cplusplus
56 }
57 #endif
58