[ccan] [PATCH] minmax: New module

David Gibson david at gibson.dropbear.id.au
Sun Jun 1 17:26:29 EST 2014


Add a 'minmax' module with typesafe macros to compute minimum, maximum and
clamping.  Inspired by the versions used in the Linux kernel, but using
a different implementation based on __builtin_types_compatible_p() and the
cast module.

Signed-off-by: David Gibson <david at gibson.dropbear.id.au>
---
 ccan/minmax/LICENSE                       |  1 +
 ccan/minmax/_info                         | 41 +++++++++++++++++++++++++++++
 ccan/minmax/minmax.h                      | 43 +++++++++++++++++++++++++++++++
 ccan/minmax/test/compile_fail-wrongsign.c | 20 ++++++++++++++
 ccan/minmax/test/compile_fail-wrongsize.c | 20 ++++++++++++++
 ccan/minmax/test/run.c                    | 37 ++++++++++++++++++++++++++
 6 files changed, 162 insertions(+)
 create mode 120000 ccan/minmax/LICENSE
 create mode 100644 ccan/minmax/_info
 create mode 100644 ccan/minmax/minmax.h
 create mode 100644 ccan/minmax/test/compile_fail-wrongsign.c
 create mode 100644 ccan/minmax/test/compile_fail-wrongsize.c
 create mode 100644 ccan/minmax/test/run.c

diff --git a/ccan/minmax/LICENSE b/ccan/minmax/LICENSE
new file mode 120000
index 0000000..dc314ec
--- /dev/null
+++ b/ccan/minmax/LICENSE
@@ -0,0 +1 @@
+../../licenses/LGPL-2.1
\ No newline at end of file
diff --git a/ccan/minmax/_info b/ccan/minmax/_info
new file mode 100644
index 0000000..ca4636d
--- /dev/null
+++ b/ccan/minmax/_info
@@ -0,0 +1,41 @@
+#include <string.h>
+#include "config.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 at gibson.dropbear.id.au>
+ * License: LGPL (v2.1 or any later version)
+ */
+int main(int argc, char *argv[])
+{
+	/* Expect exactly one argument */
+	if (argc != 2)
+		return 1;
+
+	if (strcmp(argv[1], "depends") == 0) {
+		printf("ccan/cast\n");
+		return 0;
+	}
+
+	return 1;
+}
diff --git a/ccan/minmax/minmax.h b/ccan/minmax/minmax.h
new file mode 100644
index 0000000..90f64ef
--- /dev/null
+++ b/ccan/minmax/minmax.h
@@ -0,0 +1,43 @@
+/* Licensed under LGPLv2.1+ - see LICENSE file for details */
+#ifndef CCAN_MINMAX_H
+#define CCAN_MINMAX_H
+
+#include "config.h"
+
+#include <ccan/cast/cast.h>
+
+#if HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF && HAVE_BUILTIN_CHOOSE_EXPR
+#define min(_a, _b) \
+	(__builtin_choose_expr(__builtin_types_compatible_p(typeof(_a), \
+							    typeof(_b)), \
+			       (_a) < (_b) ? (_a) : (_b),		\
+			       (void)0))
+
+#define max(_a, _b) \
+	(__builtin_choose_expr(__builtin_types_compatible_p(typeof(_a), \
+							    typeof(_b)), \
+			       (_a) > (_b) ? (_a) : (_b),		\
+			       (void)0))
+#else
+#define min(_a, _b) \
+	((_a) < (_b) ? (_a) : (_b))
+
+#define max(_a, _b) \
+	((_a) > (_b) ? (_a) : (_b))
+#endif
+
+#define clamp(_v, _floor, _ceil) \
+	(max(min((_v), (_ceil)), (_floor)))
+
+
+#define max_t(_type, _a, _b) \
+	(max(cast_static(_type, (_a)), cast_static(_type, (_b))))
+
+#define min_t(_type, _a, _b) \
+	(min(cast_static(_type, (_a)), cast_static(_type, (_b))))
+
+#define clamp_t(_type, _v, _floor, _ceil) \
+	(clamp(cast_static(_type, (_v)), \
+	       cast_static(_type, (_floor)), cast_static(_type, (_ceil))))
+
+#endif /* CCAN_MINMAX_H */
diff --git a/ccan/minmax/test/compile_fail-wrongsign.c b/ccan/minmax/test/compile_fail-wrongsign.c
new file mode 100644
index 0000000..806412f
--- /dev/null
+++ b/ccan/minmax/test/compile_fail-wrongsign.c
@@ -0,0 +1,20 @@
+#include <ccan/minmax/minmax.h>
+
+static int function(void)
+{
+#ifdef FAIL
+	return min(1, 1U);
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if the typechecks are noops."
+#endif
+#else
+	return 0;
+#endif
+}
+
+int main(int argc, char *argv[])
+{
+	function();
+	return 0;
+}
+
diff --git a/ccan/minmax/test/compile_fail-wrongsize.c b/ccan/minmax/test/compile_fail-wrongsize.c
new file mode 100644
index 0000000..1ec3ccb
--- /dev/null
+++ b/ccan/minmax/test/compile_fail-wrongsize.c
@@ -0,0 +1,20 @@
+#include <ccan/minmax/minmax.h>
+
+static int function(void)
+{
+#ifdef FAIL
+	return min(1, 1L);
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if the typechecks are noops."
+#endif
+#else
+	return 0;
+#endif
+}
+
+int main(int argc, char *argv[])
+{
+	function();
+	return 0;
+}
+
diff --git a/ccan/minmax/test/run.c b/ccan/minmax/test/run.c
new file mode 100644
index 0000000..d516781
--- /dev/null
+++ b/ccan/minmax/test/run.c
@@ -0,0 +1,37 @@
+#include <ccan/minmax/minmax.h>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+	/* This is how many tests you plan to run */
+	plan_tests(19);
+
+	ok1(min(1, 2) == 1);
+	ok1(max(1, 2) == 2);
+	ok1(min(-1, 1) == -1);
+	ok1(max(-1, 1) == 1);
+
+	ok1(min(-1U, 1U) == 1U);
+	ok1(max(-1U, 1U) == -1U);
+
+	ok1(max_t(signed int, -1, 1U) == 1);
+	ok1(max_t(unsigned int, -1, 1) == -1U);
+
+	ok1(min_t(signed int, -1, 1U) == -1);
+	ok1(min_t(unsigned int, -1, 1) == 1U);
+
+	ok1(clamp(1, 2, 5) == 2);
+	ok1(clamp(2, 2, 5) == 2);
+	ok1(clamp(3, 2, 5) == 3);
+	ok1(clamp(5, 2, 5) == 5);
+	ok1(clamp(6, 2, 5) == 5);
+
+	ok1(clamp(-1, 2, 5) == 2);
+	ok1(clamp(-1U, 2U, 5U) == 5U);
+
+	ok1(clamp_t(signed int, -1, 2, 5) == 2);
+	ok1(clamp_t(unsigned int, -1, 2, 5) == 5);
+
+	/* This exits depending on whether all tests passed */
+	return exit_status();
+}
-- 
1.9.3



More information about the ccan mailing list