Commit ecb985c1 authored by Paul Eggert's avatar Paul Eggert

Simplify bignum->intmax conversion

* src/lisp.h (integer_to_intmax, integer_to_uintmax): New functions.
* src/data.c (cons_to_unsigned, cons_to_signed)
(arith_driver):
* src/dbusbind.c (xd_extract_signed, xd_extract_unsigned):
* src/dispnew.c (sit_for):
* src/editfns.c (styled_format):
* src/emacs-module.c (module_extract_integer):
* src/fileio.c (file_offset):
* src/font.c (font_unparse_xlfd, Fopen_font):
* src/xdisp.c (calc_line_height_property):
* src/process.c (handle_child_signal):
parent e3661f8c
......@@ -2653,17 +2653,7 @@ cons_to_unsigned (Lisp_Object c, uintmax_t max)
else
{
Lisp_Object hi = CONSP (c) ? XCAR (c) : c;
if (FIXNUMP (hi))
{
val = XFIXNUM (hi);
valid = 0 <= val;
}
else
{
val = bignum_to_uintmax (hi);
valid = val != 0;
}
valid = integer_to_uintmax (hi, &val);
if (valid && CONSP (c))
{
......@@ -2724,17 +2714,7 @@ cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
else
{
Lisp_Object hi = CONSP (c) ? XCAR (c) : c;
if (FIXNUMP (hi))
{
val = XFIXNUM (hi);
valid = true;
}
else if (BIGNUMP (hi))
{
val = bignum_to_intmax (hi);
valid = val != 0;
}
valid = integer_to_intmax (hi, &val);
if (valid && CONSP (c))
{
......@@ -2972,16 +2952,8 @@ arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args,
/* Set NEXT to the next value if it fits, else exit the loop. */
intmax_t next;
if (FIXNUMP (val))
next = XFIXNUM (val);
else if (FLOATP (val))
if (! (INTEGERP (val) && integer_to_intmax (val, &next)))
break;
else
{
next = bignum_to_intmax (val);
if (next == 0)
break;
}
/* Set ACCUM to the next operation's result if it fits,
else exit the loop. */
......
......@@ -520,12 +520,13 @@ static intmax_t
xd_extract_signed (Lisp_Object x, intmax_t lo, intmax_t hi)
{
CHECK_NUMBER (x);
if (FIXNUMP (x))
if (INTEGERP (x))
{
if (lo <= XFIXNUM (x) && XFIXNUM (x) <= hi)
return XFIXNUM (x);
intmax_t i;
if (integer_to_intmax (x, &i) && lo <= i && i <= hi)
return i;
}
else if (FLOATP (x))
else
{
double d = XFLOAT_DATA (x);
if (lo <= d && d < 1.0 + hi)
......@@ -535,12 +536,6 @@ xd_extract_signed (Lisp_Object x, intmax_t lo, intmax_t hi)
return n;
}
}
else if (! (MOST_NEGATIVE_FIXNUM <= lo && hi <= MOST_POSITIVE_FIXNUM))
{
intmax_t i = bignum_to_intmax (x);
if (i != 0 && lo <= i && i <= hi)
return i;
}
if (xd_in_read_queued_messages)
Fthrow (Qdbus_error, Qnil);
......@@ -553,12 +548,13 @@ static uintmax_t
xd_extract_unsigned (Lisp_Object x, uintmax_t hi)
{
CHECK_NUMBER (x);
if (FIXNUMP (x))
if (INTEGERP (x))
{
if (0 <= XFIXNUM (x) && XFIXNUM (x) <= hi)
return XFIXNUM (x);
uintmax_t i;
if (integer_to_uintmax (x, &i) && i <= hi)
return i;
}
else if (FLOATP (x))
else
{
double d = XFLOAT_DATA (x);
if (0 <= d && d < 1.0 + hi)
......@@ -568,12 +564,6 @@ xd_extract_unsigned (Lisp_Object x, uintmax_t hi)
return n;
}
}
else if (! (hi <= MOST_POSITIVE_FIXNUM))
{
uintmax_t i = bignum_to_uintmax (x);
if (i != 0 && i <= hi)
return i;
}
if (xd_in_read_queued_messages)
Fthrow (Qdbus_error, Qnil);
......
......@@ -5765,20 +5765,20 @@ sit_for (Lisp_Object timeout, bool reading, int display_option)
if (display_option > 1)
redisplay_preserve_echo_area (2);
if (FIXNUMP (timeout))
if (INTEGERP (timeout))
{
sec = XFIXNUM (timeout);
if (sec <= 0)
return Qt;
nsec = 0;
}
else if (BIGNUMP (timeout))
{
if (NILP (Fnatnump (timeout)))
return Qt;
sec = bignum_to_intmax (timeout);
if (sec == 0)
sec = WAIT_READING_MAX;
if (integer_to_intmax (timeout, &sec))
{
if (sec <= 0)
return Qt;
sec = min (sec, WAIT_READING_MAX);
}
else
{
if (NILP (Fnatnump (timeout)))
return Qt;
sec = WAIT_READING_MAX;
}
nsec = 0;
}
else if (FLOATP (timeout))
......
......@@ -4691,21 +4691,16 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
}
else
{
if (FIXNUMP (arg))
ldarg = XFIXNUM (arg);
else
if (INTEGERP (arg))
{
intmax_t iarg = bignum_to_intmax (arg);
if (iarg != 0)
intmax_t iarg;
uintmax_t uarg;
if (integer_to_intmax (arg, &iarg))
ldarg = iarg;
else if (integer_to_uintmax (arg, &uarg))
ldarg = uarg;
else
{
uintmax_t uarg = bignum_to_uintmax (arg);
if (uarg != 0)
ldarg = uarg;
else
format_bignum_as_double = true;
}
format_bignum_as_double = true;
}
if (!format_bignum_as_double)
{
......
......@@ -519,14 +519,10 @@ module_extract_integer (emacs_env *env, emacs_value n)
MODULE_FUNCTION_BEGIN (0);
Lisp_Object l = value_to_lisp (n);
CHECK_INTEGER (l);
if (BIGNUMP (l))
{
intmax_t i = bignum_to_intmax (l);
if (i == 0)
xsignal1 (Qoverflow_error, l);
return i;
}
return XFIXNUM (l);
intmax_t i;
if (! integer_to_intmax (l, &i))
xsignal1 (Qoverflow_error, l);
return i;
}
static emacs_value
......
......@@ -3424,17 +3424,13 @@ read_non_regular_quit (Lisp_Object ignore)
static off_t
file_offset (Lisp_Object val)
{
if (RANGED_FIXNUMP (0, val, TYPE_MAXIMUM (off_t)))
return XFIXNUM (val);
if (BIGNUMP (val))
if (INTEGERP (val))
{
intmax_t v = bignum_to_intmax (val);
if (0 < v && v <= TYPE_MAXIMUM (off_t))
intmax_t v;
if (integer_to_intmax (val, &v) && 0 <= v && v <= TYPE_MAXIMUM (off_t))
return v;
}
if (FLOATP (val))
else if (FLOATP (val))
{
double v = XFLOAT_DATA (val);
if (0 <= v && v < 1.0 + TYPE_MAXIMUM (off_t))
......
......@@ -1289,8 +1289,9 @@ font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1 + DBL_MAX_10_EXP + 1)];
if (INTEGERP (val))
{
intmax_t v = FIXNUMP (val) ? XFIXNUM (val) : bignum_to_intmax (val);
if (! (0 < v && v <= TYPE_MAXIMUM (uprintmax_t)))
intmax_t v;
if (! (integer_to_intmax (val, &v)
&& 0 < v && v <= TYPE_MAXIMUM (uprintmax_t)))
v = pixel_size;
if (v > 0)
{
......@@ -4747,16 +4748,10 @@ DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
else
{
CHECK_NUMBER (size);
if (BIGNUMP (size))
{
isize = bignum_to_intmax (size);
if (isize == 0)
args_out_of_range (font_entity, size);
}
else
isize = (FLOATP (size)
? POINT_TO_PIXEL (XFLOAT_DATA (size), FRAME_RES_Y (f))
: XFIXNUM (size));
if (FLOATP (size))
isize = POINT_TO_PIXEL (XFLOAT_DATA (size), FRAME_RES_Y (f));
else if (! integer_to_intmax (size, &isize))
args_out_of_range (font_entity, size);
if (! (INT_MIN <= isize && isize <= INT_MAX))
args_out_of_range (font_entity, size);
if (isize == 0)
......
......@@ -3297,6 +3297,39 @@ extern Lisp_Object bignum_to_string (Lisp_Object, int);
extern Lisp_Object make_bignum_str (char const *, int);
extern Lisp_Object double_to_bignum (double);
/* Converthe integer NUM to *N. Return true if successful, false
(possibly setting *N) otherwise. */
INLINE bool
integer_to_intmax (Lisp_Object num, intmax_t *n)
{
if (FIXNUMP (num))
{
*n = XFIXNUM (num);
return true;
}
else
{
intmax_t i = bignum_to_intmax (num);
*n = i;
return i != 0;
}
}
INLINE bool
integer_to_uintmax (Lisp_Object num, uintmax_t *n)
{
if (FIXNUMP (num))
{
*n = XFIXNUM (num);
return 0 <= XFIXNUM (num);
}
else
{
uintmax_t i = bignum_to_uintmax (num);
*n = i;
return i != 0;
}
}
/* Defined in data.c. */
extern _Noreturn void wrong_choice (Lisp_Object, Lisp_Object);
extern void notify_variable_watchers (Lisp_Object, Lisp_Object,
......
......@@ -7055,8 +7055,9 @@ handle_child_signal (int sig)
xpid = XCAR (head);
if (all_pids_are_fixnums ? FIXNUMP (xpid) : INTEGERP (xpid))
{
pid_t deleted_pid = (FIXNUMP (xpid) ? XFIXNUM (xpid)
: bignum_to_intmax (xpid));
intmax_t deleted_pid;
bool ok = integer_to_intmax (xpid, &deleted_pid);
eassert (ok);
if (child_status_changed (deleted_pid, 0, 0))
{
if (STRINGP (XCDR (head)))
......
......@@ -27910,10 +27910,12 @@ calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
/* FIXME: Check for overflow in multiplication or conversion. */
if (FLOATP (val))
height = (int)(XFLOAT_DATA (val) * height);
else if (FIXNUMP (val))
height *= XFIXNUM (val);
else
height *= bignum_to_intmax (val);
{
intmax_t v;
if (integer_to_intmax (val, &v))
height *= v;
}
return make_fixnum (height);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment