This library contains code that extends and simplifies different operations
for C language based programs.
Revisión | 5ab5f6fd4751ee0364450d191b1b9e7a951ec117 (tree) |
---|---|
Tiempo | 2017-01-30 07:49:48 |
Autor | s.gusarov |
Commiter | s.gusarov |
Module minmax renamed to min_max
@@ -0,0 +1,67 @@ | ||
1 | +/* | |
2 | + * @author Sergey Gusarov <laborer2008 (at) gmail.com> | |
3 | + * @section LICENSE | |
4 | + * This Source Code Form is subject to the terms of the Mozilla Public | |
5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | + * | |
8 | + * @section DESCRIPTION | |
9 | + * Calculation of minimum/maximum value from 2 arguments. | |
10 | + * See http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax | |
11 | + * for jumpless macros. But it seems that modern compilers can do it: | |
12 | + * http://stackoverflow.com/a/7475388 | |
13 | + * So we shouldn't care | |
14 | + */ | |
15 | + | |
16 | +#pragma once | |
17 | + | |
18 | +#include "ctools/predef/compiler.h" | |
19 | + | |
20 | + | |
21 | +#ifdef CT_MAX2 | |
22 | +# error CT_MAX2 must be undefined | |
23 | +#endif | |
24 | + | |
25 | +#ifdef CT_MIN2 | |
26 | +# error CT_MIN2 must be undefined | |
27 | +#endif | |
28 | + | |
29 | + | |
30 | +#if defined (CT_COMPL_GCC_EMULATION) && !defined (CT_COMPL_ARMCC) | |
31 | + | |
32 | +/* | |
33 | + * See: | |
34 | + * https://gcc.gnu.org/onlinedocs/gcc/Typeof.html | |
35 | + * https://gcc.gnu.org/onlinedocs/cpp/Duplication-of-Side-Effects.html | |
36 | + */ | |
37 | + | |
38 | +# ifdef _a | |
39 | +# error _a must be undefined | |
40 | +# endif | |
41 | + | |
42 | +# ifdef _b | |
43 | +# error _b must be undefined | |
44 | +# endif | |
45 | + | |
46 | +/* *INDENT-OFF* */ | |
47 | +# define CT_MAX2(a, b)\ | |
48 | + ({\ | |
49 | + __typeof__(a) _a = (a);\ | |
50 | + __typeof__(b) _b = (b);\ | |
51 | + _a >= _b ? _a : _b;\ | |
52 | + }) | |
53 | + | |
54 | +# define CT_MIN2(a, b)\ | |
55 | + ({\ | |
56 | + __typeof__(a) _a = (a);\ | |
57 | + __typeof__(b) _b = (b);\ | |
58 | + _a < _b ? _a : _b;\ | |
59 | + }) | |
60 | +/* *INDENT-ON* */ | |
61 | + | |
62 | +#else | |
63 | + | |
64 | +# define CT_MAX2(a, b) ((a) >= (b) ? (a) : (b)) | |
65 | +# define CT_MIN2(a, b) ((a) < (b) ? (a) : (b)) | |
66 | + | |
67 | +#endif // defined (CT_COMPL_GCC_EMULATION) |
@@ -1,67 +0,0 @@ | ||
1 | -/* | |
2 | - * @author Sergey Gusarov <laborer2008 (at) gmail.com> | |
3 | - * @section LICENSE | |
4 | - * This Source Code Form is subject to the terms of the Mozilla Public | |
5 | - * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 | - * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | - * | |
8 | - * @section DESCRIPTION | |
9 | - * Calculation of minimum/maximum value from 2 arguments. | |
10 | - * See http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax | |
11 | - * for jumpless macros. But it seems that modern compilers can do it: | |
12 | - * http://stackoverflow.com/a/7475388 | |
13 | - * So we shouldn't care | |
14 | - */ | |
15 | - | |
16 | -#pragma once | |
17 | - | |
18 | -#include "ctools/predef/compiler.h" | |
19 | - | |
20 | - | |
21 | -#ifdef CT_MAX2 | |
22 | -# error CT_MAX2 must be undefined | |
23 | -#endif | |
24 | - | |
25 | -#ifdef CT_MIN2 | |
26 | -# error CT_MIN2 must be undefined | |
27 | -#endif | |
28 | - | |
29 | - | |
30 | -#if defined (CT_COMPL_GCC_EMULATION) && !defined (CT_COMPL_ARMCC) | |
31 | - | |
32 | -/* | |
33 | - * See: | |
34 | - * https://gcc.gnu.org/onlinedocs/gcc/Typeof.html | |
35 | - * https://gcc.gnu.org/onlinedocs/cpp/Duplication-of-Side-Effects.html | |
36 | - */ | |
37 | - | |
38 | -# ifdef _a | |
39 | -# error _a must be undefined | |
40 | -# endif | |
41 | - | |
42 | -# ifdef _b | |
43 | -# error _b must be undefined | |
44 | -# endif | |
45 | - | |
46 | -/* *INDENT-OFF* */ | |
47 | -# define CT_MAX2(a, b)\ | |
48 | - ({\ | |
49 | - __typeof__(a) _a = (a);\ | |
50 | - __typeof__(b) _b = (b);\ | |
51 | - _a >= _b ? _a : _b;\ | |
52 | - }) | |
53 | - | |
54 | -# define CT_MIN2(a, b)\ | |
55 | - ({\ | |
56 | - __typeof__(a) _a = (a);\ | |
57 | - __typeof__(b) _b = (b);\ | |
58 | - _a < _b ? _a : _b;\ | |
59 | - }) | |
60 | -/* *INDENT-ON* */ | |
61 | - | |
62 | -#else | |
63 | - | |
64 | -# define CT_MAX2(a, b) ((a) >= (b) ? (a) : (b)) | |
65 | -# define CT_MIN2(a, b) ((a) < (b) ? (a) : (b)) | |
66 | - | |
67 | -#endif // defined (CT_COMPL_GCC_EMULATION) |
@@ -31,7 +31,7 @@ | ||
31 | 31 | # include <stdlib.h> |
32 | 32 | CT_END_EXTERNAL_HEADERS |
33 | 33 | |
34 | -#include "ctools/minmax.h" | |
34 | +#include "ctools/min_max.h" | |
35 | 35 | #include "ctools/pointer.h" |
36 | 36 | #include "ctools/trace.h" |
37 | 37 |
@@ -0,0 +1,40 @@ | ||
1 | +/* | |
2 | + * @author Sergey Gusarov <laborer2008 (at) gmail.com> | |
3 | + * @section LICENSE | |
4 | + * This Source Code Form is subject to the terms of the Mozilla Public | |
5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | + * | |
8 | + * @section DESCRIPTION | |
9 | + * | |
10 | + */ | |
11 | + | |
12 | +#include "tests/unity_headers.h" | |
13 | + | |
14 | +#include <ctools/min_max.h> | |
15 | + | |
16 | + | |
17 | +void setUp(void) | |
18 | +{ | |
19 | +} | |
20 | + | |
21 | +void tearDown(void) | |
22 | +{ | |
23 | +} | |
24 | + | |
25 | +static const int a = -10; | |
26 | +static const int b = 3; | |
27 | + | |
28 | +void testMin(void) | |
29 | +{ | |
30 | + const int c = CT_MIN2(a, b); | |
31 | + | |
32 | + TEST_ASSERT_EQUAL_INT(c, a); | |
33 | +} | |
34 | + | |
35 | +void testMax(void) | |
36 | +{ | |
37 | + const int d = CT_MAX2(a, b); | |
38 | + | |
39 | + TEST_ASSERT_EQUAL_INT(d, b); | |
40 | +} |
@@ -1,40 +0,0 @@ | ||
1 | -/* | |
2 | - * @author Sergey Gusarov <laborer2008 (at) gmail.com> | |
3 | - * @section LICENSE | |
4 | - * This Source Code Form is subject to the terms of the Mozilla Public | |
5 | - * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 | - * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | - * | |
8 | - * @section DESCRIPTION | |
9 | - * | |
10 | - */ | |
11 | - | |
12 | -#include "tests/unity_headers.h" | |
13 | - | |
14 | -#include <ctools/minmax.h> | |
15 | - | |
16 | - | |
17 | -void setUp(void) | |
18 | -{ | |
19 | -} | |
20 | - | |
21 | -void tearDown(void) | |
22 | -{ | |
23 | -} | |
24 | - | |
25 | -static const int a = -10; | |
26 | -static const int b = 3; | |
27 | - | |
28 | -void testMin(void) | |
29 | -{ | |
30 | - const int c = CT_MIN2(a, b); | |
31 | - | |
32 | - TEST_ASSERT_EQUAL_INT(c, a); | |
33 | -} | |
34 | - | |
35 | -void testMax(void) | |
36 | -{ | |
37 | - const int d = CT_MAX2(a, b); | |
38 | - | |
39 | - TEST_ASSERT_EQUAL_INT(d, b); | |
40 | -} |