33 |
33 |
34 #ifndef _MAIN_HH |
34 #ifndef _MAIN_HH |
35 #define _MAIN_HH |
35 #define _MAIN_HH |
36 |
36 |
37 |
37 |
38 /* Get the definition of INT16_MAX, INT16_MIN, UINT64_MAX, INT64_MAX, INT64_MIN, ... */ |
|
39 |
|
40 #define __STDC_LIMIT_MACROS /* required when including from C++ source code. */ |
|
41 #include <stdint.h> |
|
42 |
|
43 #ifndef UINT64_MAX |
|
44 #define UINT64_MAX (std::numeric_limits< uint64_t >::max()) |
|
45 #endif |
|
46 #ifndef INT64_MAX |
|
47 #define INT64_MAX (std::numeric_limits< int64_t >::max()) |
|
48 #endif |
|
49 #ifndef INT64_MIN |
|
50 #define INT64_MIN (std::numeric_limits< int64_t >::min()) |
|
51 #endif |
|
52 |
|
53 #if (real64_t == float) |
|
54 #define HUGE_VAL64 HUGE_VALF |
|
55 #elif (real64_t == double) |
|
56 #define HUGE_VAL64 HUGE_VAL |
|
57 #elif (real64_t == long_double) |
|
58 #define HUGE_VAL64 HUGE_VALL |
|
59 #else |
|
60 #error Could not determine which data type is being used for real64_t (defined in absyntax.hh). Aborting! |
|
61 #endif |
|
62 |
|
63 |
|
64 |
|
65 |
38 |
66 /* Function used throughout the code --> used to report failed assertions (i.e. internal compiler errors)! */ |
39 /* Function used throughout the code --> used to report failed assertions (i.e. internal compiler errors)! */ |
67 #include <stddef.h> /* required for NULL */ |
40 #include <stddef.h> /* required for NULL */ |
68 |
41 |
69 #define ERROR error_exit(__FILE__,__LINE__) |
42 #define ERROR error_exit(__FILE__,__LINE__) |
73 extern void error_exit(const char *file_name, int line_no, const char *errmsg = NULL, ...); |
46 extern void error_exit(const char *file_name, int line_no, const char *errmsg = NULL, ...); |
74 |
47 |
75 |
48 |
76 |
49 |
77 |
50 |
|
51 |
|
52 |
|
53 /* Get the definition of INT16_MAX, INT16_MIN, UINT64_MAX, INT64_MAX, INT64_MIN, ... */ |
|
54 |
|
55 #define __STDC_LIMIT_MACROS /* required to have UINTxx_MAX defined when including stdint.h from C++ source code. */ |
|
56 #include <stdint.h> |
|
57 #include <limits> |
|
58 |
|
59 #ifndef UINT64_MAX |
|
60 #define UINT64_MAX (std::numeric_limits< uint64_t >::max()) |
|
61 #endif |
|
62 #ifndef INT64_MAX |
|
63 #define INT64_MAX (std::numeric_limits< int64_t >::max()) |
|
64 #endif |
|
65 #ifndef INT64_MIN |
|
66 #define INT64_MIN (std::numeric_limits< int64_t >::min()) |
|
67 #endif |
|
68 |
|
69 |
|
70 |
|
71 /* Determine, for the current platform, which datas types (float, double or long double) use 64 and 32 bits. */ |
|
72 /* NOTE: We cant use sizeof() in pre-processor directives, so we have to do it another way... */ |
|
73 /* CURIOSITY: We can use sizeof() and offsetof() inside static_assert() but: |
|
74 * - this only allows us to make assertions, and not #define new macros |
|
75 * - is only available in the C standard [ISO/IEC 9899:2011] and the C++ 0X draft standard [Becker 2008]. It is not available in C99. |
|
76 * https://www.securecoding.cert.org/confluence/display/seccode/DCL03-C.+Use+a+static+assertion+to+test+the+value+of+a+constant+expression |
|
77 * struct {int a, b, c, d} header_t; |
|
78 * e.g.: static_assert(offsetof(struct header_t, c) == 8, "Compile time error message."); |
|
79 */ |
|
80 |
|
81 #include <float.h> |
|
82 #if (LDBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */ |
|
83 #define long_double long double |
|
84 #define real64_t long_double /* so we can later use #if (real64_t == long_double) directives in the code! */ |
|
85 #define REAL64_MAX LDBL_MAX |
|
86 #elif ( DBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */ |
|
87 #define real64_t double |
|
88 #define REAL64_MAX DBL_MAX |
|
89 #elif ( FLT_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */ |
|
90 #define real64_t float |
|
91 #define REAL64_MAX FLT_MAX |
|
92 #else |
|
93 #error Could not find a 64 bit floating point data type on this platform. Aborting... |
|
94 #endif |
|
95 |
|
96 |
|
97 #if (LDBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */ |
|
98 #ifndef long_double |
|
99 #define long_double long double |
|
100 #endif |
|
101 #define real32_t long_double /* so we can later use #if (real32_t == long_double) directives in the code! */ |
|
102 #define REAL32_MAX LDBL_MAX |
|
103 #elif ( DBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */ |
|
104 #define real32_t double |
|
105 #define REAL32_MAX DBL_MAX |
|
106 #elif ( FLT_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */ |
|
107 #define real32_t float |
|
108 #define REAL32_MAX FLT_MAX |
|
109 #else |
|
110 #error Could not find a 32 bit floating point data type on this platform. Aborting... |
|
111 #endif |
|
112 |
|
113 |
|
114 |
|
115 #include <math.h> |
|
116 #ifndef INFINITY |
|
117 #error Could not find the macro that defines the value for INFINITY in the current platform. |
|
118 #endif |
|
119 #ifndef NAN |
|
120 #error Could not find the macro that defines the value for NAN in the current platform. |
|
121 #endif |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
78 #endif // #ifndef _MAIN_HH |
127 #endif // #ifndef _MAIN_HH |