From c7d8ae5e57c992032296b141d0ead876cc36185c Mon Sep 17 00:00:00 2001 From: Drew Richardson Date: Mon, 1 May 2023 22:08:41 -0700 Subject: [PATCH] Add support unsigned char The signedness of char is implementation specific. On x86_64-linux-gnu it's signed, but on aarch64-linux-gnu it is unsigned. --- include/qrintf.h | 24 ++++++++++++------------ misc/gen-qrintf.h.pl | 8 +++++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/qrintf.h b/include/qrintf.h index 986f0df..784de9b 100644 --- a/include/qrintf.h +++ b/include/qrintf.h @@ -443,28 +443,28 @@ static inline qrintf_chk_t _qrintf_chk_long_long_core(qrintf_chk_t ctx, int fill return ctx; } -static inline qrintf_nck_t _qrintf_nck_hhd(qrintf_nck_t ctx, char v) +static inline qrintf_nck_t _qrintf_nck_hhd(qrintf_nck_t ctx, signed char v) { unsigned char val = v >= 0 ? v : -(unsigned char)v; int sign = v < 0; - if (sizeof(char) < sizeof(long long)) { + if (sizeof(signed char) < sizeof(long long)) { return _qrintf_nck_long_core(ctx, 0, 0, (unsigned long)val, sign); } else { - assert(sizeof(char) == sizeof(long long)); + assert(sizeof(signed char) == sizeof(long long)); return _qrintf_nck_long_long_core(ctx, 0, 0, (unsigned long long)val, sign); } } -static inline qrintf_nck_t _qrintf_nck_width_hhd(qrintf_nck_t ctx, int fill_ch, int width, char v) +static inline qrintf_nck_t _qrintf_nck_width_hhd(qrintf_nck_t ctx, int fill_ch, int width, signed char v) { unsigned char val = v >= 0 ? v : -(unsigned char)v; int sign = v < 0; - if (sizeof(char) < sizeof(long long)) { + if (sizeof(signed char) < sizeof(long long)) { return _qrintf_nck_long_core(ctx, fill_ch, width, (unsigned long)val, sign); } else { - assert(sizeof(char) == sizeof(long long)); + assert(sizeof(signed char) == sizeof(long long)); return _qrintf_nck_long_long_core(ctx, fill_ch, width, (unsigned long long)val, sign); } } @@ -993,28 +993,28 @@ static inline qrintf_nck_t _qrintf_nck_width_zx(qrintf_nck_t ctx, int fill_ch, i return ctx; } -static inline qrintf_chk_t _qrintf_chk_hhd(qrintf_chk_t ctx, char v) +static inline qrintf_chk_t _qrintf_chk_hhd(qrintf_chk_t ctx, signed char v) { unsigned char val = v >= 0 ? v : -(unsigned char)v; int sign = v < 0; - if (sizeof(char) < sizeof(long long)) { + if (sizeof(signed char) < sizeof(long long)) { return _qrintf_chk_long_core(ctx, 0, 0, (unsigned long)val, sign); } else { - assert(sizeof(char) == sizeof(long long)); + assert(sizeof(signed char) == sizeof(long long)); return _qrintf_chk_long_long_core(ctx, 0, 0, (unsigned long long)val, sign); } } -static inline qrintf_chk_t _qrintf_chk_width_hhd(qrintf_chk_t ctx, int fill_ch, int width, char v) +static inline qrintf_chk_t _qrintf_chk_width_hhd(qrintf_chk_t ctx, int fill_ch, int width, signed char v) { unsigned char val = v >= 0 ? v : -(unsigned char)v; int sign = v < 0; - if (sizeof(char) < sizeof(long long)) { + if (sizeof(signed char) < sizeof(long long)) { return _qrintf_chk_long_core(ctx, fill_ch, width, (unsigned long)val, sign); } else { - assert(sizeof(char) == sizeof(long long)); + assert(sizeof(signed char) == sizeof(long long)); return _qrintf_chk_long_long_core(ctx, fill_ch, width, (unsigned long long)val, sign); } } diff --git a/misc/gen-qrintf.h.pl b/misc/gen-qrintf.h.pl index bb821e7..7ba1b5f 100755 --- a/misc/gen-qrintf.h.pl +++ b/misc/gen-qrintf.h.pl @@ -27,9 +27,11 @@ sub build_d { return build_mt(template => << 'EOT', escape_func => undef)->(@_); ? my ($check, $type, $suffix) = @_; +? my $unsigned_type = $type; +? $unsigned_type =~ s/^(signed )?/unsigned /g; static inline qrintf__t _qrintf__(qrintf__t ctx, v) { - unsigned val = v >= 0 ? v : -(unsigned )v; + val = v >= 0 ? v : -()v; int sign = v < 0; if (sizeof() < sizeof(long long)) { return _qrintf__long_core(ctx, 0, 0, (unsigned long)val, sign); @@ -42,7 +44,7 @@ sub build_d { static inline qrintf__t _qrintf__width_(qrintf__t ctx, int fill_ch, int width, v) { - unsigned val = v >= 0 ? v : -(unsigned )v; + val = v >= 0 ? v : -()v; int sign = v < 0; if (sizeof() < sizeof(long long)) { return _qrintf__long_core(ctx, fill_ch, width, (unsigned long)val, sign); @@ -564,7 +566,7 @@ sub build_x { } ? for my $check (qw(nck chk)) { -($check, "char", "hhd") ?> +($check, "signed char", "hhd") ?> ($check, "short", "hd") ?> ($check, "int", "d",) ?> ($check, "long", "ld") ?>