diff --git a/lib/Makefile.am b/lib/Makefile.am index adcd9fbf..b5f601a6 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -164,6 +164,7 @@ libshadow_la_SOURCES = \ time/day_to_str.c \ time/day_to_str.h \ ttytype.c \ + typetraits.h \ tz.c \ ulimit.c \ user_busy.c \ diff --git a/lib/typetraits.h b/lib/typetraits.h new file mode 100644 index 00000000..47bd30d9 --- /dev/null +++ b/lib/typetraits.h @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2022-2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_TYPETRAITS_H_ +#define SHADOW_INCLUDE_LIB_TYPETRAITS_H_ + + +#include + +#include "sizeof.h" + + +#define is_unsigned(x) \ +( \ + (typeof(x)) -1 > 1 \ +) + +#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) \ +) + + +#endif // include guard