poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [COMMITTED] pkl, testsuite, doc: defunit, support for arbitrary name


From: Jose E. Marchesi
Subject: Re: [COMMITTED] pkl, testsuite, doc: defunit, support for arbitrary named units
Date: Sun, 01 Mar 2020 19:03:53 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Allright, I just installed the patch below, that reworks the standard
units to match the suffixes used in coreutils.  It also uses the power
operator to calculate the value of the units.


commit 4afd8ac44618f7ae9c711f2e4f536cd6561cfae3 (HEAD -> master)
Author: Jose E. Marchesi <address@hidden>
Date:   Sun Mar 1 19:01:20 2020 +0100

    std,testsuite: consolidate standard units with coreutils
    
    2020-03-01  Jose E. Marchesi  <address@hidden>
    
            * src/std.pk: Define standard units using the power operator, and
            use the same prefixes than coreutils.
            * src/pvm-val.h (PVM_VAL_OFF_UNIT_GIGABYTES): Define.
            (PVM_VAL_OFF_UNIT_KIBIBITS): Likewise.
            (PVM_VAL_OFF_UNIT_KIBIBYTES): Likewise.
            (PVM_VAL_OFF_UNIT_MEBIBITS): Likewise.
            (PVM_VAL_OFF_UNIT_MEBIBYTES): Likewise.
            (PVM_VAL_OFF_UNIT_KILOBITS): Changed to base 10.
            (PVM_VAL_OFF_UNIT_KILOBYTES): Likewise.
            (PVM_VAL_OFF_UNIT_MEGABITS): Likewise.
            (PVM_VAL_OFF_UNIT_MEGABYTES): Likewise.
            (PVM_VAL_OFF_UNIT_GIGABITS): Likewise.
            * src/pvm-val.c (pvm_print_val): Fix unit names.
            (print_unit_name): New function.
            (pvm_print_val): Use print_unit_name.
            * testsuite/poke.pkl/ass-offset-1.pk: Adjust test to new units.
            * testsuite/poke.pkl/promo-offset-arg-2.pk: Likewise.
            * testsuite/poke.pkl/promo-offset-arg-1.pk: Likewise.
            * testsuite/poke.pkl/offsets-53.pk: Likewise.
            * testsuite/poke.pkl/mod-offsets-diag-1.pk: Likewise.
            * testsuite/poke.pkl/div-offsets-diag-2.pk: Likewise.
            * testsuite/poke.pkl/units-8.pk: New test.

diff --git a/ChangeLog b/ChangeLog
index 7cefb9b..05578f5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,28 @@
+2020-03-01  Jose E. Marchesi  <address@hidden>
+
+       * src/std.pk: Define standard units using the power operator, and
+       use the same prefixes than coreutils.
+       * src/pvm-val.h (PVM_VAL_OFF_UNIT_GIGABYTES): Define.
+       (PVM_VAL_OFF_UNIT_KIBIBITS): Likewise.
+       (PVM_VAL_OFF_UNIT_KIBIBYTES): Likewise.
+       (PVM_VAL_OFF_UNIT_MEBIBITS): Likewise.
+       (PVM_VAL_OFF_UNIT_MEBIBYTES): Likewise.
+       (PVM_VAL_OFF_UNIT_KILOBITS): Changed to base 10.
+       (PVM_VAL_OFF_UNIT_KILOBYTES): Likewise.
+       (PVM_VAL_OFF_UNIT_MEGABITS): Likewise.
+       (PVM_VAL_OFF_UNIT_MEGABYTES): Likewise.
+       (PVM_VAL_OFF_UNIT_GIGABITS): Likewise.
+       * src/pvm-val.c (pvm_print_val): Fix unit names.
+       (print_unit_name): New function.
+       (pvm_print_val): Use print_unit_name.
+       * testsuite/poke.pkl/ass-offset-1.pk: Adjust test to new units.
+       * testsuite/poke.pkl/promo-offset-arg-2.pk: Likewise.
+       * testsuite/poke.pkl/promo-offset-arg-1.pk: Likewise.
+       * testsuite/poke.pkl/offsets-53.pk: Likewise.
+       * testsuite/poke.pkl/mod-offsets-diag-1.pk: Likewise.
+       * testsuite/poke.pkl/div-offsets-diag-2.pk: Likewise.
+       * testsuite/poke.pkl/units-8.pk: New test.
+
 2020-03-01  Jose E. Marchesi  <address@hidden>
 
        * src/pkl-ops.def (PKL_AST_OP_POW): New operator.
diff --git a/src/pvm-val.c b/src/pvm-val.c
index 92ed336..0a2cc34 100644
--- a/src/pvm-val.c
+++ b/src/pvm-val.c
@@ -527,6 +527,63 @@ pvm_print_binary (uint64_t val, int size, int sign)
     }
 }
 
+static void
+print_unit_name (uint64_t unit)
+{
+  switch (unit)
+    {
+    case PVM_VAL_OFF_UNIT_BITS:
+      pk_puts ("b");
+      break;
+    case PVM_VAL_OFF_UNIT_NIBBLES:
+      pk_puts ("N");
+      break;
+    case PVM_VAL_OFF_UNIT_BYTES:
+      pk_puts ("B");
+      break;
+    case PVM_VAL_OFF_UNIT_KILOBITS:
+      pk_puts ("Kb");
+      break;
+    case PVM_VAL_OFF_UNIT_KILOBYTES:
+      pk_puts ("KB");
+      break;
+    case PVM_VAL_OFF_UNIT_MEGABITS:
+      pk_puts ("Mb");
+      break;
+    case PVM_VAL_OFF_UNIT_MEGABYTES:
+      pk_puts ("MB");
+      break;
+    case PVM_VAL_OFF_UNIT_GIGABITS:
+      pk_puts ("Gb");
+      break;
+    case PVM_VAL_OFF_UNIT_GIGABYTES:
+      pk_puts ("GB");
+      break;
+    case PVM_VAL_OFF_UNIT_KIBIBITS:
+      pk_puts ("Kib");
+      break;
+    case PVM_VAL_OFF_UNIT_KIBIBYTES:
+      pk_puts ("KiB");
+      break;
+    case PVM_VAL_OFF_UNIT_MEBIBITS:
+      pk_puts ("Mib");
+      break;
+    case PVM_VAL_OFF_UNIT_MEBIBYTES:
+      pk_puts ("MiB");
+      break;
+    case PVM_VAL_OFF_UNIT_GIGIBITS:
+      pk_puts ("Gib");
+      break;
+    case PVM_VAL_OFF_UNIT_GIGIBYTES:
+      pk_puts ("GiB");
+      break;
+    default:
+      /* XXX: print here the name of the base type of the
+         offset.  */
+      pk_printf ("%" PRIu64, unit);
+    }
+}
+
 void
 pvm_print_val (pvm_val val, int base, uint32_t flags)
 {
@@ -944,32 +1001,7 @@ pvm_print_val (pvm_val val, int base, uint32_t flags)
           pk_puts ("[");
           pvm_print_val (PVM_VAL_TYP_O_BASE_TYPE (val), base, flags);
           pk_puts (" ");
-          switch (PVM_VAL_ULONG (PVM_VAL_TYP_O_UNIT (val)))
-            {
-            case PVM_VAL_OFF_UNIT_BITS:
-              pk_puts ("b");
-              break;
-            case PVM_VAL_OFF_UNIT_BYTES:
-              pk_puts ("B");
-              break;
-            case PVM_VAL_OFF_UNIT_KILOBITS:
-              pk_puts ("Kb");
-              break;
-            case PVM_VAL_OFF_UNIT_KILOBYTES:
-              pk_puts ("KB");
-              break;
-            case PVM_VAL_OFF_UNIT_MEGABITS:
-              pk_puts ("Mb");
-              break;
-            case PVM_VAL_OFF_UNIT_MEGABYTES:
-              pk_puts ("MB");
-              break;
-            case PVM_VAL_OFF_UNIT_GIGABITS:
-              pk_puts ("Gb");
-              break;
-            default:
-              assert (0);
-            }
+          print_unit_name (PVM_VAL_ULONG (PVM_VAL_TYP_O_UNIT (val)));
           pk_puts ("]");
           break;
         case PVM_TYPE_CLOSURE:
@@ -1021,39 +1053,7 @@ pvm_print_val (pvm_val val, int base, uint32_t flags)
       pk_term_class ("offset");
       pvm_print_val (PVM_VAL_OFF_MAGNITUDE (val), base, flags);
       pk_puts ("#");
-      switch (PVM_VAL_ULONG (PVM_VAL_OFF_UNIT (val)))
-        {
-        case PVM_VAL_OFF_UNIT_BITS:
-          pk_puts ("b");
-          break;
-        case PVM_VAL_OFF_UNIT_NIBBLES:
-          pk_puts ("N");
-          break;
-        case PVM_VAL_OFF_UNIT_BYTES:
-          pk_puts ("B");
-          break;
-        case PVM_VAL_OFF_UNIT_KILOBITS:
-          pk_puts ("Kb");
-          break;
-        case PVM_VAL_OFF_UNIT_KILOBYTES:
-          pk_puts ("KB");
-          break;
-        case PVM_VAL_OFF_UNIT_MEGABITS:
-          pk_puts ("Mb");
-          break;
-        case PVM_VAL_OFF_UNIT_MEGABYTES:
-          pk_puts ("MB");
-          break;
-        case PVM_VAL_OFF_UNIT_GIGABITS:
-          pk_puts ("Gb");
-          break;
-        default:
-          /* XXX: print here the name of the base type of the
-             offset.  */
-          pk_printf ("%" PRIu64, PVM_VAL_ULONG (PVM_VAL_OFF_UNIT (val)));
-          break;
-        }
-
+      print_unit_name (PVM_VAL_ULONG (PVM_VAL_OFF_UNIT (val)));
       pk_term_end_class ("offset");
     }
   else if (PVM_IS_CLS (val))
diff --git a/src/pvm-val.h b/src/pvm-val.h
index 20e2ca6..a08b2d6 100644
--- a/src/pvm-val.h
+++ b/src/pvm-val.h
@@ -505,11 +505,20 @@ int pvm_call_pretty_printer (pvm_val val, pvm_val cls);
 #define PVM_VAL_OFF_UNIT_BITS 1
 #define PVM_VAL_OFF_UNIT_NIBBLES 4
 #define PVM_VAL_OFF_UNIT_BYTES (2 * PVM_VAL_OFF_UNIT_NIBBLES)
-#define PVM_VAL_OFF_UNIT_KILOBITS (1024 * PVM_VAL_OFF_UNIT_BITS)
-#define PVM_VAL_OFF_UNIT_KILOBYTES (1024 * PVM_VAL_OFF_UNIT_BYTES)
-#define PVM_VAL_OFF_UNIT_MEGABITS (1024 * PVM_VAL_OFF_UNIT_KILOBITS)
-#define PVM_VAL_OFF_UNIT_MEGABYTES (1024 * PVM_VAL_OFF_UNIT_KILOBYTES)
-#define PVM_VAL_OFF_UNIT_GIGABITS (1024 * PVM_VAL_OFF_UNIT_MEGABITS)
+
+#define PVM_VAL_OFF_UNIT_KILOBITS (1000 * PVM_VAL_OFF_UNIT_BITS)
+#define PVM_VAL_OFF_UNIT_KILOBYTES (1000 * PVM_VAL_OFF_UNIT_BYTES)
+#define PVM_VAL_OFF_UNIT_MEGABITS (1000 * PVM_VAL_OFF_UNIT_KILOBITS)
+#define PVM_VAL_OFF_UNIT_MEGABYTES (1000 * PVM_VAL_OFF_UNIT_KILOBYTES)
+#define PVM_VAL_OFF_UNIT_GIGABITS (1000 * PVM_VAL_OFF_UNIT_MEGABITS)
+#define PVM_VAL_OFF_UNIT_GIGABYTES (1000LU * PVM_VAL_OFF_UNIT_MEGABYTES)
+
+#define PVM_VAL_OFF_UNIT_KIBIBITS (1024 * PVM_VAL_OFF_UNIT_BITS)
+#define PVM_VAL_OFF_UNIT_KIBIBYTES (1024 * PVM_VAL_OFF_UNIT_BYTES)
+#define PVM_VAL_OFF_UNIT_MEBIBITS (1024 * PVM_VAL_OFF_UNIT_KIBIBITS)
+#define PVM_VAL_OFF_UNIT_MEBIBYTES (1024 * PVM_VAL_OFF_UNIT_KIBIBYTES)
+#define PVM_VAL_OFF_UNIT_GIGIBITS (1024 * PVM_VAL_OFF_UNIT_MEBIBITS)
+#define PVM_VAL_OFF_UNIT_GIGIBYTES (1024LU * PVM_VAL_OFF_UNIT_MEBIBYTES)
 
 struct pvm_off
 {
diff --git a/src/std.pk b/src/std.pk
index 2426730..a437aaa 100644
--- a/src/std.pk
+++ b/src/std.pk
@@ -21,12 +21,20 @@
 defunit b = 1;
 defunit N = 4;
 defunit B = 8;
-defunit Kb = 1024;
-defunit KB = 1024 * 8;
-defunit Mb = 1024UL * 1024;
-defunit MB = 1024UL * 1024 * 8;
-defunit Gb = 1024UL * 1024 * 1024;
-defunit GB = 1024UL * 1024 * 1024 * 8;
+
+defunit Kb = 10 ** 3;
+defunit KB = 10 ** 3 * 8;
+defunit Mb = 10 ** 6;
+defunit MB = 10 ** 6 * 8;
+defunit Gb = 10 ** 9;
+defunit GB = 10 ** 9 * 8;
+
+defunit Kib = 2 ** 10;
+defunit KiB = 2 ** 10 * 8;
+defunit Mib = 2 ** 20;
+defunit MiB = 2 ** 20 * 8;
+defunit Gib = 2 ** 30;
+defunit GiB = 2 ** 30 * 8;
 
 /*** Standard Integral Types.  */
 
diff --git a/testsuite/poke.pkl/ass-offset-1.pk 
b/testsuite/poke.pkl/ass-offset-1.pk
index f5bee2b..4b82370 100644
--- a/testsuite/poke.pkl/ass-offset-1.pk
+++ b/testsuite/poke.pkl/ass-offset-1.pk
@@ -1,7 +1,7 @@
 /* { dg-do run } */
 
-defvar foo = 0#Kb;
+defvar foo = 0#Kib;
 
-/* { dg-command { foo = 1#Mb; } } */
+/* { dg-command { foo = 1#Mib; } } */
 /* { dg-command { foo } } */
-/* { dg-output "1024#Kb" } */
+/* { dg-output "1024#Kib" } */
diff --git a/testsuite/poke.pkl/div-offsets-diag-2.pk 
b/testsuite/poke.pkl/div-offsets-diag-2.pk
index 01c3c86..272bd15 100644
--- a/testsuite/poke.pkl/div-offsets-diag-2.pk
+++ b/testsuite/poke.pkl/div-offsets-diag-2.pk
@@ -1,3 +1,3 @@
 /* { dg-do compile } */
 
-defvar o = 3#B / (1#Mb - 1024#Kb); /* { dg-error "" } */
+defvar o = 3#B / (1#Mib - 1024#Kib); /* { dg-error "" } */
diff --git a/testsuite/poke.pkl/mod-offsets-diag-1.pk 
b/testsuite/poke.pkl/mod-offsets-diag-1.pk
index a8dccdf..818fb43 100644
--- a/testsuite/poke.pkl/mod-offsets-diag-1.pk
+++ b/testsuite/poke.pkl/mod-offsets-diag-1.pk
@@ -1,3 +1,3 @@
 /* { dg-do compile } */
 
-defvar o = 3#B % (1#Mb - 1024#Kb); /* { dg-error "" } */
+defvar o = 3#B % (1#Mib - 1024#Kib); /* { dg-error "" } */
diff --git a/testsuite/poke.pkl/offsets-53.pk b/testsuite/poke.pkl/offsets-53.pk
index 9af20be..b7493ef 100644
--- a/testsuite/poke.pkl/offsets-53.pk
+++ b/testsuite/poke.pkl/offsets-53.pk
@@ -7,4 +7,4 @@
 /* { dg-output "\n12#9" } */
 
 /* { dg-command { 0UL#1024 } }*/
-/* { dg-output "\n0UL#Kb" } */
+/* { dg-output "\n0UL#Kib" } */
diff --git a/testsuite/poke.pkl/promo-offset-arg-1.pk 
b/testsuite/poke.pkl/promo-offset-arg-1.pk
index bb93373..6b5d17f 100644
--- a/testsuite/poke.pkl/promo-offset-arg-1.pk
+++ b/testsuite/poke.pkl/promo-offset-arg-1.pk
@@ -1,6 +1,6 @@
 /* { dg-do run } */
 
-defun foo = (offset<int,Kb> num_kb) int: { return num_kb'magnitude; }
+defun foo = (offset<int,Kib> num_kb) int: { return num_kb'magnitude; }
 
-/* { dg-command { foo (1#Mb) } } */
+/* { dg-command { foo (1#Mib) } } */
 /* { dg-output "1024" } */
diff --git a/testsuite/poke.pkl/promo-offset-arg-2.pk 
b/testsuite/poke.pkl/promo-offset-arg-2.pk
index 5557e2a..e09f87b 100644
--- a/testsuite/poke.pkl/promo-offset-arg-2.pk
+++ b/testsuite/poke.pkl/promo-offset-arg-2.pk
@@ -1,6 +1,6 @@
 /* { dg-do run } */
 
-defun foo = (offset<int,Kb> num_kb = 2#Mb) int: { return num_kb'magnitude; }
+defun foo = (offset<int,Kib> num_kb = 2#Mib) int: { return num_kb'magnitude; }
 
 /* { dg-command { foo } } */
 /* { dg-output "2048" } */
diff --git a/testsuite/poke.pkl/promo-offset-return-1.pk 
b/testsuite/poke.pkl/promo-offset-return-1.pk
index 0cc4b4e..55bd49d 100644
--- a/testsuite/poke.pkl/promo-offset-return-1.pk
+++ b/testsuite/poke.pkl/promo-offset-return-1.pk
@@ -1,6 +1,6 @@
 /* { dg-do run } */
 
-defun ja = offset<int,Kb>: { return 1#Mb; }
+defun ja = offset<int,Kib>: { return 1#Mib; }
 
 /* { dg-command { ja } } */
-/* { dg-output "1024#Kb" } */
+/* { dg-output "1024#Kib" } */
diff --git a/testsuite/poke.pkl/units-8.pk b/testsuite/poke.pkl/units-8.pk
new file mode 100644
index 0000000..b53d964
--- /dev/null
+++ b/testsuite/poke.pkl/units-8.pk
@@ -0,0 +1,49 @@
+/* { dg-do run } */
+
+/* This tests that the standard units defined in std.pk are recognized
+   by the PVM value printer.  */
+
+/* { dg-command { 1#b } } *
+/* { dg-output "1#b" } */
+
+/* { dg-command { 1#N } } *
+/* { dg-output "\n1#N" } */
+
+/* { dg-command { 1#B } } *
+/* { dg-output "\n1#B" } */
+
+/* { dg-command { 1#Kb } } *
+/* { dg-output "\n1#Kb" } */
+
+/* { dg-command { 1#KB } } *
+/* { dg-output "\n1#KB" } */
+
+/* { dg-command { 1#Mb } } *
+/* { dg-output "\n1#Mb" } */
+
+/* { dg-command { 1#MB } } *
+/* { dg-output "\n1#MB" } */
+
+/* { dg-command { 1#Gb } } *
+/* { dg-output "\n1#Gb" } */
+
+/* { dg-command { 1#GB } } *
+/* { dg-output "\n1#GB" } */
+
+/* { dg-command { 1#Kib } } *
+/* { dg-output "\n1#Kib" } */
+
+/* { dg-command { 1#KiB } } *
+/* { dg-output "\n1#KiB" } */
+
+/* { dg-command { 1#Mib } } *
+/* { dg-output "\n1#Mib" } */
+
+/* { dg-command { 1#MiB } } *
+/* { dg-output "\n1#MiB" } */
+
+/* { dg-command { 1#Gib } } *
+/* { dg-output "\n1#Gib" } */
+
+/* { dg-command { 1#GiB } } *
+/* { dg-output "\n1#GiB" } */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]