diff options
Diffstat (limited to '')
l--------- | ccan/ccan/minmax/LICENSE | 1 | ||||
-rw-r--r-- | ccan/ccan/minmax/_info | 48 | ||||
-rw-r--r-- | ccan/ccan/minmax/minmax.h | 65 |
3 files changed, 114 insertions, 0 deletions
diff --git a/ccan/ccan/minmax/LICENSE b/ccan/ccan/minmax/LICENSE new file mode 120000 index 0000000..b7951da --- /dev/null +++ b/ccan/ccan/minmax/LICENSE @@ -0,0 +1 @@ +../../licenses/CC0
\ No newline at end of file diff --git a/ccan/ccan/minmax/_info b/ccan/ccan/minmax/_info new file mode 100644 index 0000000..2f6437e --- /dev/null +++ b/ccan/ccan/minmax/_info @@ -0,0 +1,48 @@ +#include "config.h" +#include <stdio.h> +#include <string.h> + +/** + * minmax - typesafe minimum and maximum functions + * + * The classic implementation of minimum / maximum macros in C can be + * very dangerous. If the two arguments have different sizes, or + * different signedness, type promotion rules can lead to very + * surprising results. + * + * This module implements typesafe versions, which will generate a + * compile time error, if the arguments have different types. + * + * Example: + * #include <ccan/minmax/minmax.h> + * #include <stdio.h> + * + * int main(int argc, char *argv[]) + * { + * printf("Signed max: %d\n", max(1, -1)); + * printf("Unsigned max: %u\n", max(1U, -1U)); + * return 0; + * } + * + * Author: David Gibson <david@gibson.dropbear.id.au> + * License: CC0 (Public domain) + */ +int main(int argc, char *argv[]) +{ + /* Expect exactly one argument */ + if (argc != 2) + return 1; + + if (strcmp(argv[1], "depends") == 0) { + printf("ccan/build_assert\n"); + return 0; + } + + if (strcmp(argv[1], "ccanlint") == 0) { + /* We need several gcc extensions */ + printf("tests_compile_without_features FAIL\n"); + return 0; + } + + return 1; +} diff --git a/ccan/ccan/minmax/minmax.h b/ccan/ccan/minmax/minmax.h new file mode 100644 index 0000000..d111d1b --- /dev/null +++ b/ccan/ccan/minmax/minmax.h @@ -0,0 +1,65 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_MINMAX_H +#define CCAN_MINMAX_H + +#include "config.h" + +#include <ccan/build_assert/build_assert.h> + +#if !HAVE_STATEMENT_EXPR || !HAVE_TYPEOF +/* + * Without these, there's no way to avoid unsafe double evaluation of + * the arguments + */ +#error Sorry, minmax module requires statement expressions and typeof +#endif + +#if HAVE_BUILTIN_TYPES_COMPATIBLE_P +#define MINMAX_ASSERT_COMPATIBLE(a, b) \ + BUILD_ASSERT(__builtin_types_compatible_p(a, b)) +#else +#define MINMAX_ASSERT_COMPATIBLE(a, b) \ + do { } while (0) +#endif + +#define min(a, b) \ + ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + MINMAX_ASSERT_COMPATIBLE(typeof(_a), typeof(_b)); \ + _a < _b ? _a : _b; \ + }) + +#define max(a, b) \ + ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + MINMAX_ASSERT_COMPATIBLE(typeof(_a), typeof(_b)); \ + _a > _b ? _a : _b; \ + }) + +#define clamp(v, f, c) (max(min((v), (c)), (f))) + + +#define min_t(t, a, b) \ + ({ \ + t _ta = (a); \ + t _tb = (b); \ + min(_ta, _tb); \ + }) +#define max_t(t, a, b) \ + ({ \ + t _ta = (a); \ + t _tb = (b); \ + max(_ta, _tb); \ + }) + +#define clamp_t(t, v, f, c) \ + ({ \ + t _tv = (v); \ + t _tf = (f); \ + t _tc = (c); \ + clamp(_tv, _tf, _tc); \ + }) + +#endif /* CCAN_MINMAX_H */ |