1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _BS_UTIL_UTILS_H
8 #define _BS_UTIL_UTILS_H
9 
10 #include "bs_types.h"
11 
12 #ifndef STR
13 #define __STR_2(a) #a
14 #define STR(a) __STR_2(a)
15 #endif
16 
17 //MIN and MAX macros similar to the definition in sys/param.h
18 //see for example http://stackoverflow.com/questions/3437404/min-and-max-in-c for a long discussion about a trivial piece of code
19 //Note that the solution with the compound statement is not necessarily portable to other compilers
20 #ifndef BS_MAX
21 #define BS_MAX( x, y ) ( ( (x) > (y) ) ? (x) : (y) ) //Safe'ish MAX macro (be careful that a and b are evaluated twice)
22 #endif
23 
24 #ifndef BS_MIN
25 #define BS_MIN( x, y ) ( ( (x) < (y) ) ? (x) : (y) ) //Safe'ish MIN macro (be careful that a and b are evaluated twice)
26 #endif
27 
28 #ifndef BS_MOD
29 #define BS_MOD( x ,y ) ( ( ( (x) % (y) ) + (y) ) % (y) ) //Use this instead of just % to avoid problems with rem( % in C99 ) != mod
30 #endif
31 
32 #if !defined(BS_STATIC_ASSERT)
33 
34 #if defined(__cplusplus) && (__cplusplus >= 201103L)
35 #define BS_STATIC_ASSERT(...) static_assert(__VA_ARGS__)
36 /*
37  * GCC 4.6 and higher have the C11 _Static_assert built
38  */
39 #elif !defined(__cplusplus) && \
40   ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) ||  \
41    (__STDC_VERSION__) >= 201100)
42 #define BS_STATIC_ASSERT(...) _Static_assert(__VA_ARGS__)
43 #else
44 #define BS_STATIC_ASSERT(...)
45 #endif
46 
47 #endif /* !defined(BS_STATIC_ASSERT) */
48 
49 #ifndef BS_UNREACHABLE
50 #define BS_UNREACHABLE __builtin_unreachable()
51 #endif
52 
53 #endif
54