+static EMACS_INT
+truncate2 (i1, i2)
+ EMACS_INT i1, i2;
+{
+ return (i2 < 0
+ ? (i1 < 0 ? -i1 / -i2 : - (i1 / -i2))
+ : (i1 < 0 ? - (-i1 / i2) : i1 / i2));
+}
+
+static EMACS_INT
+round2 (i1, i2)
+ EMACS_INT i1, i2;
+{
+ /* The C language's division operator gives us one remainder R, but
+ we want the remainder R1 on the other side of 0 if R1 is closer
+ to 0 than R is; because we want to round to even, we also want R1
+ if R and R1 are the same distance from 0 and if C's quotient is
+ odd. */
+ EMACS_INT q = i1 / i2;
+ EMACS_INT r = i1 % i2;
+ EMACS_INT abs_r = r < 0 ? -r : r;
+ EMACS_INT abs_r1 = (i2 < 0 ? -i2 : i2) - abs_r;
+ return q + (abs_r + (q & 1) <= abs_r1 ? 0 : (i2 ^ r) < 0 ? -1 : 1);
+}
+
+/* The code uses emacs_rint, so that it works to undefine HAVE_RINT
+ if `rint' exists but does not work right. */
+#ifdef HAVE_RINT
+#define emacs_rint rint
+#else
+static double
+emacs_rint (d)
+ double d;
+{
+ return floor (d + 0.5);
+}
+#endif
+
+static double
+double_identity (d)
+ double d;
+{
+ return d;
+}
+
+DEFUN ("ceiling", Fceiling, Sceiling, 1, 2, 0,
+ "Return the smallest integer no less than ARG. (Round toward +inf.)\n\
+With optional DIVISOR, return the smallest integer no less than ARG/DIVISOR.")
+ (arg, divisor)
+ Lisp_Object arg, divisor;
+{
+ return rounding_driver (arg, divisor, ceil, ceiling2, "ceiling");
+}
+
+DEFUN ("floor", Ffloor, Sfloor, 1, 2, 0,
+ "Return the largest integer no greater than ARG. (Round towards -inf.)\n\
+With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR.")
+ (arg, divisor)
+ Lisp_Object arg, divisor;
+{
+ return rounding_driver (arg, divisor, floor, floor2, "floor");