[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] pkl: Fix binary representation in printf/format
From: |
Mohammad-Reza Nabipoor |
Subject: |
[PATCH] pkl: Fix binary representation in printf/format |
Date: |
Mon, 24 Jan 2022 20:54:28 +0330 |
printf/format should not add suffix except for %v. This commit
fix that.
2022-01-24 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* common/pk-utils.h (pk_print_binary): Add new param `use_suffix_p`.
(pk_format_binary): Likewise.
* common/pk-utils.c (pk_print_binary): Likewise.
(pk_format_binary): Likewise.
* libpoke/pvm-val.c (pvm_print_val_1): Update `pk_print_binary` usage.
* libpoke/pvm.jitter (PVM_PRINTI): Likewise.
(PVM_PRINTL): Likewise.
(PVM_FORMATI): Likewise.
(PVM_FORMATL): Likewise.
* testsuite/poke.pkl/format-1.pk: Update test to reflect new behavior.
* testsuite/poke.pkl/printf-binary-5.pk: New test.
* testsuite/poke.pkl/printf-binary-6.pk: Likewise.
* testsuite/poke.pkl/printf-binary-7.pk: Likewise.
* testsuite/Makefile.am (EXTRA_DIST): Update.
---
ChangeLog | 17 ++++++++++
common/pk-utils.c | 48 +++++++++++++++------------
common/pk-utils.h | 5 +--
libpoke/pvm-val.c | 12 ++++---
libpoke/pvm.jitter | 8 ++---
testsuite/Makefile.am | 3 ++
testsuite/poke.pkl/format-1.pk | 18 +++++-----
testsuite/poke.pkl/printf-binary-5.pk | 4 +++
testsuite/poke.pkl/printf-binary-6.pk | 4 +++
testsuite/poke.pkl/printf-binary-7.pk | 4 +++
10 files changed, 82 insertions(+), 41 deletions(-)
create mode 100644 testsuite/poke.pkl/printf-binary-5.pk
create mode 100644 testsuite/poke.pkl/printf-binary-6.pk
create mode 100644 testsuite/poke.pkl/printf-binary-7.pk
diff --git a/ChangeLog b/ChangeLog
index 4ad95d12..be7ddfb3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2022-01-24 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
+
+ * common/pk-utils.h (pk_print_binary): Add new param `use_suffix_p`.
+ (pk_format_binary): Likewise.
+ * common/pk-utils.c (pk_print_binary): Likewise.
+ (pk_format_binary): Likewise.
+ * libpoke/pvm-val.c (pvm_print_val_1): Update `pk_print_binary` usage.
+ * libpoke/pvm.jitter (PVM_PRINTI): Likewise.
+ (PVM_PRINTL): Likewise.
+ (PVM_FORMATI): Likewise.
+ (PVM_FORMATL): Likewise.
+ * testsuite/poke.pkl/format-1.pk: Update test to reflect new behavior.
+ * testsuite/poke.pkl/printf-binary-5.pk: New test.
+ * testsuite/poke.pkl/printf-binary-6.pk: Likewise.
+ * testsuite/poke.pkl/printf-binary-7.pk: Likewise.
+ * testsuite/Makefile.am (EXTRA_DIST): Update.
+
2022-01-24 Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
* libpoke/ios-dev-sub.c (ios_dev_sub_handler_normalize): Fix error
diff --git a/common/pk-utils.c b/common/pk-utils.c
index 62e63660..f578e2a9 100644
--- a/common/pk-utils.c
+++ b/common/pk-utils.c
@@ -86,46 +86,50 @@ PK_POW (pk_upow, uint64_t)
void
pk_print_binary (void (*puts_fn) (const char *str),
- uint64_t val, int size, int sign)
+ uint64_t val, int size, int sign_p, int use_suffix_p)
{
char b[65];
- for (int z = 0; z < size; z++) {
+ for (int z = 0; z < size; z++)
b[size-1-z] = ((val >> z) & 0x1) + '0';
- }
b[size] = '\0';
puts_fn (b);
- if (size == 64)
- puts_fn (sign ? "L" : "UL");
- else if (size == 16)
- puts_fn (sign ? "H" : "UH");
- else if (size == 8)
- puts_fn (sign ? "B" : "UB");
- else if (size == 4)
- puts_fn (sign ? "N" : "UN");
+ if (use_suffix_p)
+ {
+ if (size == 64)
+ puts_fn (sign_p ? "L" : "UL");
+ else if (size == 16)
+ puts_fn (sign_p ? "H" : "UH");
+ else if (size == 8)
+ puts_fn (sign_p ? "B" : "UB");
+ else if (size == 4)
+ puts_fn (sign_p ? "N" : "UN");
+ }
}
int
pk_format_binary (char* out, size_t outlen,
- uint64_t val, int size, int sign)
+ uint64_t val, int size, int sign_p, int use_suffix_p)
{
char b[64 /* digits */ + 2 /* suffix */ + 1 /* nul */];
- for (int z = 0; z < size; z++) {
+ for (int z = 0; z < size; z++)
b[size-1-z] = ((val >> z) & 0x1) + '0';
- }
b[size] = '\0';
- if (size == 64)
- strcat (b, sign ? "L" : "UL");
- else if (size == 16)
- strcat (b, sign ? "H" : "UH");
- else if (size == 8)
- strcat (b, sign ? "B" : "UB");
- else if (size == 4)
- strcat (b, sign ? "N" : "UN");
+ if (use_suffix_p)
+ {
+ if (size == 64)
+ strcat (b, sign_p ? "L" : "UL");
+ else if (size == 16)
+ strcat (b, sign_p ? "H" : "UH");
+ else if (size == 8)
+ strcat (b, sign_p ? "B" : "UB");
+ else if (size == 4)
+ strcat (b, sign_p ? "N" : "UN");
+ }
if (strlen (b) < outlen)
{
diff --git a/common/pk-utils.h b/common/pk-utils.h
index eeb1abe8..d0995486 100644
--- a/common/pk-utils.h
+++ b/common/pk-utils.h
@@ -48,11 +48,12 @@ int64_t pk_ipow (int64_t base, uint32_t exp);
uint64_t pk_upow (uint64_t base, uint32_t exp);
/* Print the given unsigned 64-bit integer in binary. */
-void pk_print_binary (void (*puts_fn) (const char *str), uint64_t val, int
size, int sign);
+void pk_print_binary (void (*puts_fn) (const char *str), uint64_t val,
+ int size, int sign_p, int use_suffix_p);
/* Format the given unsigned 64-bit integer in binary. */
int pk_format_binary (char* out, size_t outlen, uint64_t val, int size,
- int sign);
+ int sign_p, int use_suffix_p);
/* Concatenate string arguments into an malloc'ed string. */
char *pk_str_concat (const char *s0, ...) __attribute__ ((sentinel));
diff --git a/libpoke/pvm-val.c b/libpoke/pvm-val.c
index 9dfb8fd7..18da3209 100644
--- a/libpoke/pvm-val.c
+++ b/libpoke/pvm-val.c
@@ -1143,7 +1143,8 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base,
int indent,
if (base == 2)
{
pk_puts ("0b");
- pk_print_binary (pk_puts, ulongval, size, 1);
+ pk_print_binary (pk_puts, ulongval, size, /*signed_p*/ 1,
+ /*use_suffix_p*/ 1);
}
else
{
@@ -1172,7 +1173,8 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base,
int indent,
if (base == 2)
{
pk_puts ("0b");
- pk_print_binary (pk_puts, (uint64_t) uintval, size, 1);
+ pk_print_binary (pk_puts, (uint64_t) uintval, size, /*signed*/ 1,
+ /*use_suffix_p*/ 1);
}
else
{
@@ -1201,7 +1203,8 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base,
int indent,
if (base == 2)
{
pk_puts ("0b");
- pk_print_binary (pk_puts, ulongval, size, 0);
+ pk_print_binary (pk_puts, ulongval, size, /*signed_p*/ 0,
+ /*use_suffix_p*/ 1);
}
else
{
@@ -1223,7 +1226,8 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base,
int indent,
if (base == 2)
{
pk_puts ("0b");
- pk_print_binary (pk_puts, uintval, size, 0);
+ pk_print_binary (pk_puts, uintval, size, /*signed_p*/ 0,
+ /*use_suffix_p*/ 1);
}
else
{
diff --git a/libpoke/pvm.jitter b/libpoke/pvm.jitter
index be89b5f9..246f52d0 100644
--- a/libpoke/pvm.jitter
+++ b/libpoke/pvm.jitter
@@ -635,7 +635,7 @@ late-header-c
} \
else if ((BASE) == 2) \
{ \
- pk_print_binary (pk_puts, val, JITTER_ARGN0, 1); \
+ pk_print_binary (pk_puts, val, JITTER_ARGN0, 1, 0); \
JITTER_DROP_STACK (); \
JITTER_DROP_STACK (); \
break; \
@@ -689,7 +689,7 @@ late-header-c
} \
else if ((BASE) == 2) \
{ \
- pk_print_binary (pk_puts, val, JITTER_ARGN0, 1); \
+ pk_print_binary (pk_puts, val, JITTER_ARGN0, 1, 0); \
JITTER_DROP_STACK (); \
JITTER_DROP_STACK (); \
break; \
@@ -750,7 +750,7 @@ late-header-c
} \
else if ((BASE) == 2) \
{ \
- n = pk_format_binary ((OUT), (OUTLEN), val, JITTER_ARGN0, SIGNED_P);\
+ n = pk_format_binary ((OUT), (OUTLEN), val, JITTER_ARGN0, SIGNED_P,
0);\
assert (n == 0); \
JITTER_DROP_STACK (); \
JITTER_DROP_STACK (); \
@@ -809,7 +809,7 @@ late-header-c
} \
else if ((BASE) == 2) \
{ \
- n = pk_format_binary ((OUT), (OUTLEN), val, JITTER_ARGN0, SIGNED_P);\
+ n = pk_format_binary ((OUT), (OUTLEN), val, JITTER_ARGN0, SIGNED_P,
0);\
assert (n == 0); \
JITTER_DROP_STACK (); \
JITTER_DROP_STACK (); \
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index ade647a3..3bbf10d7 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -1681,6 +1681,9 @@ EXTRA_DIST = \
poke.pkl/printf-binary-2.pk \
poke.pkl/printf-binary-3.pk \
poke.pkl/printf-binary-4.pk \
+ poke.pkl/printf-binary-5.pk \
+ poke.pkl/printf-binary-6.pk \
+ poke.pkl/printf-binary-7.pk \
poke.pkl/printf-class-1.pk \
poke.pkl/printf-class-2.pk \
poke.pkl/printf-class-3.pk \
diff --git a/testsuite/poke.pkl/format-1.pk b/testsuite/poke.pkl/format-1.pk
index 331985f1..f1cc48aa 100644
--- a/testsuite/poke.pkl/format-1.pk
+++ b/testsuite/poke.pkl/format-1.pk
@@ -3,19 +3,19 @@
var eq = [
format("") == "",
format("%i1b", 0) == "0",
- format("%i8b", 1) == "00000001B",
- format("%i8b", -1) == "11111111B",
- format("%u8b", 1) == "00000001UB",
- format("%u8b", -1) == "11111111UB",
+ format("%i8b", 1) == "00000001",
+ format("%i8b", -1) == "11111111",
+ format("%u8b", 1) == "00000001",
+ format("%u8b", -1) == "11111111",
format("%i13b", -1 as uint<64>) == "1" * 13,
- format("%i16b", -1) == "1" * 16 + "H",
- format("%i16b", 0xa5) == "0" * 8 + "10100101H",
- format("%i16b", 0xa5) == "0" * 8 + "10100101H",
- format("%u16b", 0xa5) == "0" * 8 + "10100101UH",
+ format("%i16b", -1) == "1" * 16,
+ format("%i16b", 0xa5) == "0" * 8 + "10100101",
+ format("%i16b", 0xa5) == "0" * 8 + "10100101",
+ format("%u16b", 0xa5) == "0" * 8 + "10100101",
format("%i32b", 0xdeadbeaf) == "11011110101011011011111010101111",
format("%u32b", 0xdeadbeaf) == "11011110101011011011111010101111",
format("%u63b", -1 as uint<64>) == "1" * 63,
- format("%u64b", -1 as uint<64>) == "1" * 64 + "UL",
+ format("%u64b", -1 as uint<64>) == "1" * 64,
];
for (i in eq) printf ("%i32d", i); /* { dg-output "111111111111111" } */
diff --git a/testsuite/poke.pkl/printf-binary-5.pk
b/testsuite/poke.pkl/printf-binary-5.pk
new file mode 100644
index 00000000..d6334d71
--- /dev/null
+++ b/testsuite/poke.pkl/printf-binary-5.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+/* { dg-command { printf "0b%u4b\n",4L } } */
+/* { dg-output "0b0100" } */
diff --git a/testsuite/poke.pkl/printf-binary-6.pk
b/testsuite/poke.pkl/printf-binary-6.pk
new file mode 100644
index 00000000..8e85878e
--- /dev/null
+++ b/testsuite/poke.pkl/printf-binary-6.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+/* { dg-command { printf "0b%u8b\n",4U } } */
+/* { dg-output "0b00000100" } */
diff --git a/testsuite/poke.pkl/printf-binary-7.pk
b/testsuite/poke.pkl/printf-binary-7.pk
new file mode 100644
index 00000000..e00ba551
--- /dev/null
+++ b/testsuite/poke.pkl/printf-binary-7.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+/* { dg-command { printf "0b%u16b\n",-1 } } */
+/* { dg-output "0b1111111111111111" } */
--
2.34.1
- [PATCH] pkl: Fix binary representation in printf/format,
Mohammad-Reza Nabipoor <=