GCC 16 has added two new operators, _Minof() and _Maxof().
Let's name our macros like these (but without the reserved names).
I'm also working on standardization of these operators within the
C Committee.
Link: <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3628.txt>
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
#define a2ul(...) a2i(unsigned long, __VA_ARGS__)
#define a2ull(...) a2i(unsigned long long, __VA_ARGS__)
-#define str2i(T, ...) a2i(T, __VA_ARGS__, NULL, 0, type_min(T), type_max(T))
+#define str2i(T, ...) a2i(T, __VA_ARGS__, NULL, 0, minof(T), maxof(T))
#define str2sh(...) str2i(short, __VA_ARGS__)
#define str2si(...) str2i(int, __VA_ARGS__)
inline int
get_gid(const char *restrict gidstr, gid_t *restrict gid)
{
- return a2i(gid_t, gid, gidstr, NULL, 10, type_min(gid_t), type_max(gid_t));
+ return a2i(gid_t, gid, gidstr, NULL, 10, minof(gid_t), maxof(gid_t));
}
inline int
get_pid(const char *restrict pidstr, pid_t *restrict pid)
{
- return a2i(pid_t, pid, pidstr, NULL, 10, 1, type_max(pid_t));
+ return a2i(pid_t, pid, pidstr, NULL, 10, 1, maxof(pid_t));
}
inline int
get_uid(const char *restrict uidstr, uid_t *restrict uid)
{
- return a2i(uid_t, uid, uidstr, NULL, 10, type_min(uid_t), type_max(uid_t));
+ return a2i(uid_t, uid, uidstr, NULL, 10, minof(uid_t), maxof(uid_t));
}
limit = RLIM_INFINITY;
} else {
- if (a2i(rlim_t, &l, value, NULL, 10, 0, type_max(rlim_t)) == -1
+ if (a2i(rlim_t, &l, value, NULL, 10, 0, maxof(rlim_t)) == -1
&& errno != ENOTSUP)
{
return 0; // FIXME: we could instead throw an error, though.
# define is_signed(x) ((typeof(x)) -1 < 1)
# define stype_max(T) ((T) (((((T) 1 << (WIDTHOF(T) - 2)) - 1) << 1) + 1))
# define utype_max(T) ((T) -1)
-# define type_max(T) ((T) (is_signed(T) ? stype_max(T) : utype_max(T)))
-# define type_min(T) ((T) ~type_max(T))
+# define maxof(T) ((T) (is_signed(T) ? stype_max(T) : utype_max(T)))
+# define minof(T) ((T) ~maxof(T))
#define is_same_type(a, b) \
id_t first, last;
const char *pos;
struct id_range result = {
- .first = type_max(id_t),
- .last = type_min(id_t)
+ .first = maxof(id_t),
+ .last = minof(id_t)
};
static_assert(is_same_type(id_t, uid_t), "");
static_assert(is_same_type(id_t, gid_t), "");
- first = type_min(id_t);
- last = type_max(id_t);
+ first = minof(id_t);
+ last = maxof(id_t);
if (a2i(id_t, &first, str, &pos, 10, first, last) == -1
&& errno != ENOTSUP)
#include "typetraits.h"
#include "attr.h"
-static void test_type_max(MAYBE_UNUSED void ** _1);
-static void test_type_min(MAYBE_UNUSED void ** _1);
+
+static void test_maxof(MAYBE_UNUSED void ** _1);
+static void test_minof(MAYBE_UNUSED void ** _1);
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_type_max),
- cmocka_unit_test(test_type_min),
+ cmocka_unit_test(test_maxof),
+ cmocka_unit_test(test_minof),
};
return cmocka_run_group_tests(tests, NULL, NULL);
static void
-test_type_max(MAYBE_UNUSED void ** _1)
+test_maxof(MAYBE_UNUSED void ** _1)
{
- assert_true(type_max(long) == LONG_MAX);
- assert_true(type_max(unsigned long) == ULONG_MAX);
- assert_true(type_max(int) == INT_MAX);
- assert_true(type_max(unsigned short) == USHRT_MAX);
- assert_true(type_max(char) == CHAR_MAX);
- assert_true(type_max(signed char) == SCHAR_MAX);
- assert_true(type_max(unsigned char) == UCHAR_MAX);
+ assert_true(maxof(long) == LONG_MAX);
+ assert_true(maxof(unsigned long) == ULONG_MAX);
+ assert_true(maxof(int) == INT_MAX);
+ assert_true(maxof(unsigned short) == USHRT_MAX);
+ assert_true(maxof(char) == CHAR_MAX);
+ assert_true(maxof(signed char) == SCHAR_MAX);
+ assert_true(maxof(unsigned char) == UCHAR_MAX);
}
static void
-test_type_min(MAYBE_UNUSED void ** _1)
+test_minof(MAYBE_UNUSED void ** _1)
{
- assert_true(type_min(long) == LONG_MIN);
- assert_true(type_min(unsigned long) == 0);
- assert_true(type_min(int) == INT_MIN);
- assert_true(type_min(unsigned short) == 0);
- assert_true(type_min(char) == CHAR_MIN);
- assert_true(type_min(signed char) == SCHAR_MIN);
- assert_true(type_min(unsigned char) == 0);
+ assert_true(minof(long) == LONG_MIN);
+ assert_true(minof(unsigned long) == 0);
+ assert_true(minof(int) == INT_MIN);
+ assert_true(minof(unsigned short) == 0);
+ assert_true(minof(char) == CHAR_MIN);
+ assert_true(minof(signed char) == SCHAR_MIN);
+ assert_true(minof(unsigned char) == 0);
}