From 50bb696a6b4d75a097f22274834f67961fa9e440 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 11 Dec 2025 18:32:27 +0100 Subject: Add signed integers (arithmetics and some testing not implemented yet) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libj2.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 7 deletions(-) (limited to 'libj2.h') diff --git a/libj2.h b/libj2.h index 6d45d26..dd7fc35 100644 --- a/libj2.h +++ b/libj2.h @@ -32,12 +32,29 @@ #define LIBJ2_JU_BIT ((unsigned)CHAR_BIT * (unsigned)sizeof(uintmax_t)) /** - * The number of bits in an `struct libj2_j2u` + * The number of bits in a `struct libj2_j2u` * * @since 1.0 */ #define LIBJ2_J2U_BIT (2U * LIBJ2_JU_BIT) +/** + * The number of bits in a `struct libj2_j2i`, + * including the sign bit + * + * @since 1.1 + */ +#define LIBJ2_J2I_BIT LIBJ2_J2U_BIT + +/** + * The number of value bits in a `struct libj2_j2i`, + * that is, the number of bits excluding the sign + * bit + * + * @since 1.1 + */ +#define LIBJ2_J2I_VBIT (LIBJ2_J2I_BIT - 1U) + /** * Unsigned double-maximum precision integer @@ -59,6 +76,37 @@ struct libj2_j2u { }; +/** + * Signed double-maximum precision integer, + * using two's complement + * + * libj2 does not implement bitwise operations, + * bit-scanning, or bit-rotation operations + * for this type, but a `struct libj2_j2i *` + * can safetly be casted to `struct libj2_j2u *` + * for such operators as well as the for + * unsigned bit shifting operations + * + * If `a` is the maximum representable value, + * `-a - 1` is the minimum representable value; + * there is only one zero value (has no sign) + * + * @since 1.1 + */ +struct libj2_j2i { + /** + * Most significant half, of which the most + * significant bit is the sign bit + */ + uintmax_t high; + + /** + * Least significant half + */ + uintmax_t low; +}; + + /** * Arithmetic overflow prediction result * @@ -91,15 +139,17 @@ enum libj2_overflow { #include "libj2/signum.h" #include "libj2/constructors.h" #include "libj2/unsigned-comparsion.h" +#include "libj2/signed-comparsion.h" +#include "libj2/mixed-comparsion.h" #include "libj2/bitwise-logic.h" +#include "libj2/sign-shifting.h" #include "libj2/bit-shifting.h" #include "libj2/bit-scanning.h" -#include "libj2/sign-shifting.h" -#include "libj2/addition.h" -#include "libj2/subtraction.h" -#include "libj2/multiplication.h" -#include "libj2/division.h" -#include "libj2/saturated-math.h" +#include "libj2/addition.h" /* TODO add signed versions */ +#include "libj2/subtraction.h" /* TODO add signed versions */ +#include "libj2/multiplication.h" /* TODO add signed versions */ +#include "libj2/division.h" /* TODO add signed versions */ +#include "libj2/saturated-math.h" /* TODO add signed versions */ #include "libj2/strings.h" -- cgit v1.2.3-70-g09d2