]> gitweb.factorcode.org Git - factor.git/commitdiff
vm: Fix C++ warning for comparing a signed to less-than 0.
authorDoug Coleman <doug.coleman@gmail.com>
Wed, 15 Dec 2021 06:26:07 +0000 (00:26 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 15 Dec 2021 06:34:15 +0000 (00:34 -0600)
Still a bit more duplication than I would like.

Found with:

c++ --version
c++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Warning:

vm/bignum.cpp: In member function ‘factor::bignum* factor::factor_vm::cell_to_bignum(factor::cell)’:
vm/bignum.cpp:332:11: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  332 |     if (n < (type) 0 && n == (type) - 1)                              \
      |         ~~^~~~~~~~~~
vm/bignum.cpp:361:1: note: in expansion of macro ‘FOO_TO_BIGNUM’
  361 | FOO_TO_BIGNUM(cell, cell, fixnum, cell)
      | ^~~~~~~~~~~~~
vm/bignum.cpp:336:29: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  336 |           ((negative_p = (n < (type) 0)) ? ((type)(-(stype) n)) : n); \
      |                           ~~^~~~~~~~~~
vm/bignum.cpp:361:1: note: in expansion of macro ‘FOO_TO_BIGNUM’
  361 | FOO_TO_BIGNUM(cell, cell, fixnum, cell)
      | ^~~~~~~~~~~~~
vm/bignum.cpp: In member function ‘factor::bignum* factor::factor_vm::ulong_long_to_bignum(uint64_t)’:
vm/bignum.cpp:332:11: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  332 |     if (n < (type) 0 && n == (type) - 1)                              \
      |         ~~^~~~~~~~~~
vm/bignum.cpp:364:1: note: in expansion of macro ‘FOO_TO_BIGNUM’
  364 | FOO_TO_BIGNUM(ulong_long, uint64_t, int64_t, uint64_t)
      | ^~~~~~~~~~~~~
vm/bignum.cpp:336:29: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  336 |           ((negative_p = (n < (type) 0)) ? ((type)(-(stype) n)) : n); \
      |                           ~~^~~~~~~~~~
vm/bignum.cpp:364:1: note: in expansion of macro ‘FOO_TO_BIGNUM’
  364 | FOO_TO_BIGNUM(ulong_long, uint64_t, int64_t, uint64_t)
      | ^~~~~~~~~~~~~

vm/bignum.cpp

index cdb4d596cc351b910d4e24ce7c36eaf73b7cb029..ed1bbfaa6220075c9aab2a163e8fcdbf9669d1b2 100644 (file)
@@ -321,7 +321,7 @@ bignum* factor_vm::bignum_remainder(bignum* numerator, bignum* denominator) {
 // cell_to_bignum, fixnum_to_bignum, long_long_to_bignum, ulong_long_to_bignum
 
 // Allocates memory
-#define FOO_TO_BIGNUM(name, type, stype, utype)                       \
+#define FOO_TO_BIGNUM_SIGNED(name, type, utype)                       \
   bignum* factor_vm::name##_to_bignum(type n) {                       \
     int negative_p;                                                   \
     /* Special cases win when these small constants are cached. */    \
@@ -329,11 +329,11 @@ bignum* factor_vm::bignum_remainder(bignum* numerator, bignum* denominator) {
       return (BIGNUM_ZERO());                                         \
     if (n == 1)                                                       \
       return (BIGNUM_ONE(0));                                         \
-    if (n < (type) 0 && n == (type) - 1)                              \
+    if (n < (type) 0 && n == (type) -1)                               \
       return (BIGNUM_ONE(1));                                         \
     {                                                                 \
       utype accumulator =                                             \
-          ((negative_p = (n < (type) 0)) ? ((type)(-(stype) n)) : n); \
+          ((negative_p = n < (type) 0) ? -n : n);                     \
       if (accumulator < BIGNUM_RADIX)                                 \
       {                                                               \
         bignum* result = allot_bignum(1, negative_p);                 \
@@ -358,10 +358,44 @@ bignum* factor_vm::bignum_remainder(bignum* numerator, bignum* denominator) {
     }                                                                 \
   }
 
-FOO_TO_BIGNUM(cell, cell, fixnum, cell)
-FOO_TO_BIGNUM(fixnum, fixnum, fixnum, cell)
-FOO_TO_BIGNUM(long_long, int64_t, int64_t, uint64_t)
-FOO_TO_BIGNUM(ulong_long, uint64_t, int64_t, uint64_t)
+// Allocates memory
+#define FOO_TO_BIGNUM_UNSIGNED(name, type, utype)                     \
+  bignum* factor_vm::name##_to_bignum(type n) {                       \
+    /* Special cases win when these small constants are cached. */    \
+    if (n == 0)                                                       \
+      return (BIGNUM_ZERO());                                         \
+    if (n == 1)                                                       \
+      return (BIGNUM_ONE(0));                                         \
+    {                                                                 \
+      utype accumulator = n;                                          \
+      if (accumulator < BIGNUM_RADIX)                                 \
+      {                                                               \
+        bignum* result = allot_bignum(1, false);                      \
+        bignum_digit_type* scan = (BIGNUM_START_PTR(result));         \
+        *scan = (accumulator & BIGNUM_DIGIT_MASK);                    \
+        return (result);                                              \
+      } else {                                                        \
+        bignum_digit_type result_digits[BIGNUM_DIGITS_FOR(type)];     \
+        bignum_digit_type* end_digits = result_digits;                \
+        do {                                                          \
+          (*end_digits++) = (accumulator & BIGNUM_DIGIT_MASK);        \
+          accumulator >>= BIGNUM_DIGIT_LENGTH;                        \
+        } while (accumulator != 0);                                   \
+        bignum* result =                                              \
+           (allot_bignum((end_digits - result_digits), false));       \
+        bignum_digit_type* scan_digits = result_digits;               \
+        bignum_digit_type* scan_result = (BIGNUM_START_PTR(result));  \
+        while (scan_digits < end_digits)                              \
+          (*scan_result++) = (*scan_digits++);                        \
+        return (result);                                              \
+      }                                                               \
+    }                                                                 \
+  }
+
+FOO_TO_BIGNUM_SIGNED(fixnum, fixnum, cell)
+FOO_TO_BIGNUM_UNSIGNED(cell, cell, cell)
+FOO_TO_BIGNUM_SIGNED(long_long, int64_t, uint64_t)
+FOO_TO_BIGNUM_UNSIGNED(ulong_long, uint64_t, uint64_t)
 
 // cannot allocate memory
 // bignum_to_cell, fixnum_to_cell, long_long_to_cell, ulong_long_to_cell