]> code.delx.au - gnu-emacs/commitdiff
Simplify by assuming C99 integer division
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 1 Aug 2015 07:26:37 +0000 (00:26 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 1 Aug 2015 07:27:15 +0000 (00:27 -0700)
* src/floatfns.c (ceiling2, floor2, truncate2):
Assume C99 (i.e., Fortran) semantics for integer division.
This simplifies the code.

src/floatfns.c

index 072e85776b5231b172946c2ac56098d1ca0ace03..63d35b8ad6b11a067b068c4c5a301ed867ad8ca4 100644 (file)
@@ -377,32 +377,22 @@ rounding_driver (Lisp_Object arg, Lisp_Object divisor,
   return arg;
 }
 
-/* With C's /, the result is implementation-defined if either operand
-   is negative, so take care with negative operands in the following
-   integer functions.  */
-
 static EMACS_INT
 ceiling2 (EMACS_INT i1, EMACS_INT i2)
 {
-  return (i2 < 0
-         ? (i1 < 0  ?  ((-1 - i1) / -i2) + 1  :  - (i1 / -i2))
-         : (i1 <= 0  ?  - (-i1 / i2)  :  ((i1 - 1) / i2) + 1));
+  return i1 / i2 + ((i1 % i2 != 0) & ((i1 < 0) == (i2 < 0)));
 }
 
 static EMACS_INT
 floor2 (EMACS_INT i1, EMACS_INT i2)
 {
-  return (i2 < 0
-         ? (i1 <= 0  ?  -i1 / -i2  :  -1 - ((i1 - 1) / -i2))
-         : (i1 < 0  ?  -1 - ((-1 - i1) / i2)  :  i1 / i2));
+  return i1 / i2 - ((i1 % i2 != 0) & ((i1 < 0) != (i2 < 0)));
 }
 
 static EMACS_INT
 truncate2 (EMACS_INT i1, EMACS_INT i2)
 {
-  return (i2 < 0
-         ? (i1 < 0  ?  -i1 / -i2  :  - (i1 / -i2))
-         : (i1 < 0  ?  - (-i1 / i2)  :  i1 / i2));
+  return i1 / i2;
 }
 
 static EMACS_INT