This library contains code that extends and simplifies different operations
for C language based programs.
Revisión | f9db305d50cc612d0d567bd824a0f0bf57192665 (tree) |
---|---|
Tiempo | 2022-10-07 10:42:15 |
Autor | Sergey Gusarov <laborer2008@gmai...> |
Commiter | Sergey Gusarov |
Added config option CT_NO_64. Enabled for sdcc port
@@ -1,4 +1,4 @@ | ||
1 | -e17570dd4d5b2716ad6fe1f3b65c4052d5adfd82 cmake/cmake_tools | |
1 | +a799a417ac6770fd96892416bc7148144ea600f6 cmake/cmake_tools | |
2 | 2 | 5013d507802becd2c434d4dbdafad45c3da5ef23 include/ctools/std/msinttypes |
3 | 3 | 0556a9e3eb8cef3262acbd7be988447b41b14215 pyrepo |
4 | 4 | 0b899aec14d3a9abb2bf260ac355f0f28630a6a3 tests/unity |
@@ -57,6 +57,10 @@ | ||
57 | 57 | if (CT_RETARGET_PUTCHAR) |
58 | 58 | add_definitions(-DCT_RETARGET_PUTCHAR=1) |
59 | 59 | endif() |
60 | + | |
61 | + if (CT_NO_64) | |
62 | + add_definitions(-DCT_NO_64=1) | |
63 | + endif() | |
60 | 64 | else() |
61 | 65 | # Definitions from above-mentioned files should provide top level script |
62 | 66 | endif() |
@@ -129,7 +129,8 @@ | ||
129 | 129 | * CT_NO_DYNAMIC_MEMORY - library will lack of dynamic memory support |
130 | 130 | and some other inherent things; |
131 | 131 | * CT_RETARGET_PUTCHAR - provide to the linker code of standard putchar() function. It fixes linking errors |
132 | - for standalone builds. Any project-user have to implement this function if necessary. | |
132 | + for standalone builds. Any project-user have to implement this function if necessary; | |
133 | +* CT_NO_64 - disable 64 bit data type support | |
133 | 134 | |
134 | 135 | Library initialization |
135 | 136 | ---------------------- |
@@ -122,7 +122,8 @@ | ||
122 | 122 | динамической памяти и некоторых возможностей неотрывно зависящих от неё; |
123 | 123 | * CT_RETARGET_PUTCHAR - включить код стандартной функции putchar(), чтобы исправить ошибки линковки |
124 | 124 | библиотеки при автономной сборке. Там где это необходимо, проекты-пользователи должны предоставлять |
125 | - линковщику свои реализации этой функции. | |
125 | + линковщику свои реализации этой функции; | |
126 | +* CT_NO_64 - выключить поддержку 64 битных типов данных. | |
126 | 127 | |
127 | 128 | Инициализация библиотеки |
128 | 129 | ------------------------ |
@@ -51,14 +51,16 @@ | ||
51 | 51 | #define CT_B32(b3, b2, b1, b0) (((uint32_t)CT_B8(b3) << 24) | ((uint32_t)CT_B8(b2) << 16) |\ |
52 | 52 | ((uint32_t)CT_B8(b1) << 8) | CT_B8(b0)) |
53 | 53 | |
54 | +#ifndef CT_NO_64 | |
54 | 55 | /* |
55 | 56 | * For up to 64-bit binary constants, MSB first |
56 | 57 | */ |
57 | -#define CT_B64(b7, b6, b5, b4, b3, b2, b1, b0)\ | |
58 | - ( ((uint64_t)CT_B8(b7) << 56) | ((uint64_t)CT_B8(b6) << 48)\ | |
59 | - | ((uint64_t)CT_B8(b5) << 40) | ((uint64_t)CT_B8(b4) << 32)\ | |
60 | - | ((uint64_t)CT_B8(b3) << 24) | ((uint64_t)CT_B8(b2) << 16)\ | |
61 | - | ((uint64_t)CT_B8(b1) << 8) | CT_B8(b0)) | |
58 | +# define CT_B64(b7, b6, b5, b4, b3, b2, b1, b0)\ | |
59 | + ( ((uint64_t)CT_B8(b7) << 56) | ((uint64_t)CT_B8(b6) << 48)\ | |
60 | + | ((uint64_t)CT_B8(b5) << 40) | ((uint64_t)CT_B8(b4) << 32)\ | |
61 | + | ((uint64_t)CT_B8(b3) << 24) | ((uint64_t)CT_B8(b2) << 16)\ | |
62 | + | ((uint64_t)CT_B8(b1) << 8) | CT_B8(b0)) | |
63 | +#endif | |
62 | 64 | |
63 | 65 | #ifdef CT_HEX__ |
64 | 66 | # error CT_HEX__ must be undefined |
@@ -149,8 +149,10 @@ | ||
149 | 149 | #define CT_SIMPLE_ROUNDF32(x) CT_SIMPLE_ROUND__(float, int32_t, x) |
150 | 150 | #define CT_SIMPLE_UROUNDF32(x) CT_SIMPLE_ROUND__(float, uint32_t, x) |
151 | 151 | |
152 | -#define CT_SIMPLE_ROUNDF64(x) CT_SIMPLE_ROUND__(float, int64_t, x) | |
153 | -#define CT_SIMPLE_UROUNDF64(x) CT_SIMPLE_ROUND__(float, uint64_t, x) | |
152 | +#ifndef CT_NO_64 | |
153 | +# define CT_SIMPLE_ROUNDF64(x) CT_SIMPLE_ROUND__(float, int64_t, x) | |
154 | +# define CT_SIMPLE_UROUNDF64(x) CT_SIMPLE_ROUND__(float, uint64_t, x) | |
155 | +#endif | |
154 | 156 | |
155 | 157 | #if defined (CT_COMPL_SDCC) |
156 | 158 | # define CT_SIMPLE_ROUND8(x) CT_SIMPLE_ROUND__(float, int8_t, x) |
@@ -162,8 +164,10 @@ | ||
162 | 164 | # define CT_SIMPLE_ROUND32(x) CT_SIMPLE_ROUND__(float, int32_t, x) |
163 | 165 | # define CT_SIMPLE_UROUND32(x) CT_SIMPLE_ROUND__(float, uint32_t, x) |
164 | 166 | |
165 | -# define CT_SIMPLE_ROUND64(x) CT_SIMPLE_ROUND__(float, int64_t, x) | |
166 | -# define CT_SIMPLE_UROUND64(x) CT_SIMPLE_ROUND__(float, uint64_t, x) | |
167 | +# ifndef CT_NO_64 | |
168 | +# define CT_SIMPLE_ROUND64(x) CT_SIMPLE_ROUND__(float, int64_t, x) | |
169 | +# define CT_SIMPLE_UROUND64(x) CT_SIMPLE_ROUND__(float, uint64_t, x) | |
170 | +# endif | |
167 | 171 | #else |
168 | 172 | # define CT_SIMPLE_ROUND8(x) CT_SIMPLE_ROUND__(double, int8_t, x) |
169 | 173 | # define CT_SIMPLE_UROUND8(x) CT_SIMPLE_ROUND__(double, uint8_t, x) |
@@ -174,6 +178,8 @@ | ||
174 | 178 | # define CT_SIMPLE_ROUND32(x) CT_SIMPLE_ROUND__(double, int32_t, x) |
175 | 179 | # define CT_SIMPLE_UROUND32(x) CT_SIMPLE_ROUND__(double, uint32_t, x) |
176 | 180 | |
177 | -# define CT_SIMPLE_ROUND64(x) CT_SIMPLE_ROUND__(double, int64_t, x) | |
178 | -# define CT_SIMPLE_UROUND64(x) CT_SIMPLE_ROUND__(double, uint64_t, x) | |
181 | +# ifndef CT_NO_64 | |
182 | +# define CT_SIMPLE_ROUND64(x) CT_SIMPLE_ROUND__(double, int64_t, x) | |
183 | +# define CT_SIMPLE_UROUND64(x) CT_SIMPLE_ROUND__(double, uint64_t, x) | |
184 | +# endif | |
179 | 185 | #endif |
@@ -127,6 +127,7 @@ | ||
127 | 127 | return CT_SIMPLE_UROUNDF32(x); |
128 | 128 | } |
129 | 129 | |
130 | +#ifndef CT_NO_64 | |
130 | 131 | static CT_FORCEINLINE int64_t roundf64(const float x) CT_NOEXCEPT |
131 | 132 | { |
132 | 133 | return CT_SIMPLE_ROUNDF64(x); |
@@ -136,6 +137,7 @@ | ||
136 | 137 | { |
137 | 138 | return CT_SIMPLE_UROUNDF64(x); |
138 | 139 | } |
140 | +#endif | |
139 | 141 | |
140 | 142 | #ifdef CT_COMPL_SDCC |
141 | 143 |
@@ -169,6 +171,7 @@ | ||
169 | 171 | return uroundf32(x); |
170 | 172 | } |
171 | 173 | |
174 | +# ifndef CT_NO_64 | |
172 | 175 | static inline int64_t round64(const float x) CT_NOEXCEPT |
173 | 176 | { |
174 | 177 | return roundf64(x); |
@@ -178,6 +181,7 @@ | ||
178 | 181 | { |
179 | 182 | return uroundf64(x); |
180 | 183 | } |
184 | +# endif | |
181 | 185 | |
182 | 186 | #else |
183 | 187 |
@@ -211,6 +215,7 @@ | ||
211 | 215 | return CT_SIMPLE_UROUND32(x); |
212 | 216 | } |
213 | 217 | |
218 | +# ifndef CT_NO_64 | |
214 | 219 | static CT_FORCEINLINE int64_t round64(const double x) CT_NOEXCEPT |
215 | 220 | { |
216 | 221 | return CT_SIMPLE_ROUND64(x); |
@@ -220,6 +225,7 @@ | ||
220 | 225 | { |
221 | 226 | return CT_SIMPLE_UROUND64(x); |
222 | 227 | } |
228 | +# endif | |
223 | 229 | |
224 | 230 | #endif |
225 | 231 |
@@ -59,8 +59,29 @@ | ||
59 | 59 | #if defined (CT_PACK_STRING_TO_UINT_AVAILABLE) |
60 | 60 | |
61 | 61 | # define CT_PACK_STRING_TO_UINT_EMPTY_STRING 0 |
62 | -# define CT_PACK_STRING_TO_UINT_TOO_LONG_STRING UINT64_MAX | |
63 | 62 | |
63 | +# ifdef CT_NO_64 | |
64 | +# define CT_PACK_STRING_TO_UINT_TOO_LONG_STRING UINT32_MAX | |
65 | + | |
66 | +/** | |
67 | + * @brief Store up to 4 characters in the one unsigned integer value | |
68 | + * CT_STRLEN(str) == 2U is strange because of gcc warning "signed and unsigned type in conditional expression". | |
69 | + * Assert couldn't be used here - it will bring nested macro. | |
70 | + * | |
71 | + * @result 4 bytes long(Reason is unknown) for C compilers and 1/2/4 bytes long for C++ | |
72 | + */ | |
73 | +# define CT_PACK_STRING_TO_UINT(str)\ | |
74 | + (\ | |
75 | + CT_STRLEN(str) == 0 ? ((uint8_t)CT_PACK_STRING_TO_UINT_EMPTY_STRING) :\ | |
76 | + CT_STRLEN(str) == 1 ? ((uint8_t)str[0]) :\ | |
77 | + CT_STRLEN(str) == 2 ? ((uint16_t)(((uint16_t)str[1] << 8) | ((uint16_t)str[0]))) :\ | |
78 | + CT_STRLEN(str) == 3 ? (((uint32_t)str[2] << 16) | ((uint32_t)str[1] << 8) | ((uint32_t)str[0])) :\ | |
79 | + CT_STRLEN(str) == 4 ? (((uint32_t)str[3] << 24) | ((uint32_t)str[2] << 16) | ((uint32_t)str[1] << 8)\ | |
80 | + | ((uint32_t)str[0])) :\ | |
81 | + ((uint32_t)CT_PACK_STRING_TO_UINT_TOO_LONG_STRING)\ | |
82 | + ) | |
83 | +# else | |
84 | +# define CT_PACK_STRING_TO_UINT_TOO_LONG_STRING UINT64_MAX | |
64 | 85 | /** |
65 | 86 | * @brief Store up to 8 characters in the one unsigned integer value |
66 | 87 | * CT_STRLEN(str) == 2U is strange because of gcc warning "signed and unsigned type in conditional expression". |
@@ -68,7 +89,7 @@ | ||
68 | 89 | * |
69 | 90 | * @result 8 bytes long(Reason is unknown) for C compilers and 1/2/4/8 bytes long for C++ |
70 | 91 | */ |
71 | -# define CT_PACK_STRING_TO_UINT(str)\ | |
92 | +# define CT_PACK_STRING_TO_UINT(str)\ | |
72 | 93 | (\ |
73 | 94 | CT_STRLEN(str) == 0 ? ((uint8_t)CT_PACK_STRING_TO_UINT_EMPTY_STRING) :\ |
74 | 95 | CT_STRLEN(str) == 1 ? ((uint8_t)str[0]) :\ |
@@ -88,5 +109,6 @@ | ||
88 | 109 | | ((uint64_t)str[1] << 8) | ((uint64_t)str[0])) :\ |
89 | 110 | ((uint64_t)CT_PACK_STRING_TO_UINT_TOO_LONG_STRING)\ |
90 | 111 | ) |
112 | +# endif | |
91 | 113 | |
92 | 114 | #endif // CT_PACK_STRING_TO_UINT_AVAILABLE |
@@ -65,9 +65,11 @@ | ||
65 | 65 | TEST_ASSERT_EQUAL_INT32(roundf32(1.5f), 2); |
66 | 66 | TEST_ASSERT_EQUAL_UINT32(uroundf32(1.5f), 2U); |
67 | 67 | |
68 | -#if defined (UNITY_INCLUDE_64) || (UNITY_LONG_WIDTH == 64) | |
68 | +#ifndef CT_NO_64 | |
69 | +# if defined (UNITY_INCLUDE_64) || (UNITY_LONG_WIDTH == 64) | |
69 | 70 | TEST_ASSERT_EQUAL_INT64(roundf64(1.5f), 2); |
70 | 71 | TEST_ASSERT_EQUAL_UINT64(uroundf64(1.5f), 2U); |
72 | +# endif | |
71 | 73 | #endif |
72 | 74 | |
73 | 75 |
@@ -80,8 +82,10 @@ | ||
80 | 82 | TEST_ASSERT_EQUAL_INT32(round32(1.5), 2); |
81 | 83 | TEST_ASSERT_EQUAL_UINT32(uround32(1.5), 2U); |
82 | 84 | |
83 | -#if defined (UNITY_INCLUDE_64) || (UNITY_LONG_WIDTH == 64) | |
85 | +#ifndef CT_NO_64 | |
86 | +# if defined (UNITY_INCLUDE_64) || (UNITY_LONG_WIDTH == 64) | |
84 | 87 | TEST_ASSERT_EQUAL_INT64(round64(1.5), 2); |
85 | 88 | TEST_ASSERT_EQUAL_UINT64(uround64(1.5), 2U); |
89 | +# endif | |
86 | 90 | #endif |
87 | 91 | } |
@@ -28,10 +28,15 @@ | ||
28 | 28 | const uint8_t k8 = CT_B8(01010101); |
29 | 29 | const uint16_t k16 = CT_B16(10101010, 01010101); |
30 | 30 | const uint32_t k32 = CT_B32(10000000, 11111111, 10101010, 01010101); |
31 | +#ifndef CT_NO_64 | |
31 | 32 | const uint64_t k64 = CT_B64(10001000, 10000000, 00000000, 10000000, 10000000, 11111111, 10101010, 01010101); |
33 | +#endif | |
32 | 34 | |
33 | 35 | TEST_ASSERT_TRUE(k8 == 85); |
34 | 36 | TEST_ASSERT_TRUE(k16 == 43605); |
35 | 37 | TEST_ASSERT_TRUE(k32 == 2164238933); |
38 | + | |
39 | +#ifndef CT_NO_64 | |
36 | 40 | TEST_ASSERT_TRUE(k64 == 0x8880008080FFAA55); |
41 | +#endif | |
37 | 42 | } |
@@ -42,8 +42,10 @@ | ||
42 | 42 | uint32_t a32; |
43 | 43 | uint32_t b32; |
44 | 44 | |
45 | +#ifndef CT_NO_64 | |
45 | 46 | uint64_t a64; |
46 | 47 | uint64_t b64; |
48 | +#endif | |
47 | 49 | |
48 | 50 | /* |
49 | 51 | * CT_PACK_STRING_TO_UINT for C returns 8 byte long value. |
@@ -70,6 +72,7 @@ | ||
70 | 72 | b32 = 0x34333231; |
71 | 73 | TEST_ASSERT_EQUAL_UINT(a32, b32); |
72 | 74 | |
75 | +#ifndef CT_NO_64 | |
73 | 76 | a64 = CT_PACK_STRING_TO_UINT("12345"); |
74 | 77 | b64 = 0x3534333231; |
75 | 78 | TEST_ASSERT_EQUAL_UINT(a64, b64); |
@@ -89,6 +92,7 @@ | ||
89 | 92 | a64 = CT_PACK_STRING_TO_UINT("1234567890"); |
90 | 93 | b64 = CT_PACK_STRING_TO_UINT_TOO_LONG_STRING; |
91 | 94 | TEST_ASSERT_EQUAL_UINT(a64, b64); |
95 | +#endif | |
92 | 96 | } |
93 | 97 | |
94 | 98 | #endif // CT_PACK_STRING_TO_UINT_AVAILABLE |