1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-21 06:55:39 +00:00

Reduce type checks in arithcompare

* src/data.c (coerce_marker, not_number_or_marker): New.
(arithcompare): Don't use NUMBERP to check types up-front since we
are going to branch on types anyway.
This commit is contained in:
Mattias Engdegård 2024-07-27 14:10:40 +02:00
parent e5a9449117
commit 7753a597fb

View File

@ -2682,11 +2682,23 @@ check_number_coerce_marker (Lisp_Object x)
return x; return x;
} }
static Lisp_Object
coerce_marker (Lisp_Object x)
{
return MARKERP (x) ? make_fixnum (marker_position (x)) : x;
}
static AVOID
not_number_or_marker (Lisp_Object x)
{
wrong_type_argument (Qnumber_or_marker_p, x);
}
cmp_bits_t cmp_bits_t
arithcompare (Lisp_Object num1, Lisp_Object num2) arithcompare (Lisp_Object num1, Lisp_Object num2)
{ {
num1 = check_number_coerce_marker (num1); num1 = coerce_marker (num1);
num2 = check_number_coerce_marker (num2); num2 = coerce_marker (num2);
bool lt, eq, gt; bool lt, eq, gt;
if (FLOATP (num1)) if (FLOATP (num1))
@ -2725,15 +2737,20 @@ arithcompare (Lisp_Object num1, Lisp_Object num2)
gt = f1 > f2; gt = f1 > f2;
} }
} }
else if (isnan (f1)) else if (BIGNUMP (num2))
lt = eq = gt = false;
else
{ {
int cmp = mpz_cmp_d (*xbignum_val (num2), f1); if (isnan (f1))
eq = cmp == 0; lt = eq = gt = false;
lt = cmp > 0; else
gt = cmp < 0; {
int cmp = mpz_cmp_d (*xbignum_val (num2), f1);
eq = cmp == 0;
lt = cmp > 0;
gt = cmp < 0;
}
} }
else
not_number_or_marker (num2);
} }
else if (FIXNUMP (num1)) else if (FIXNUMP (num1))
{ {
@ -2765,7 +2782,7 @@ arithcompare (Lisp_Object num1, Lisp_Object num2)
lt = i1 < i2; lt = i1 < i2;
gt = i1 > i2; gt = i1 > i2;
} }
else else if (BIGNUMP (num2))
{ {
int sgn = mpz_sgn (*xbignum_val (num2)); int sgn = mpz_sgn (*xbignum_val (num2));
eassume (sgn != 0); eassume (sgn != 0);
@ -2773,35 +2790,44 @@ arithcompare (Lisp_Object num1, Lisp_Object num2)
lt = sgn > 0; lt = sgn > 0;
gt = sgn < 0; gt = sgn < 0;
} }
}
else if (FLOATP (num2))
{
double f2 = XFLOAT_DATA (num2);
if (isnan (f2))
lt = eq = gt = false;
else else
not_number_or_marker (num2);
}
else if (BIGNUMP (num1))
{
if (FLOATP (num2))
{ {
int cmp = mpz_cmp_d (*xbignum_val (num1), f2); double f2 = XFLOAT_DATA (num2);
if (isnan (f2))
lt = eq = gt = false;
else
{
int cmp = mpz_cmp_d (*xbignum_val (num1), f2);
eq = cmp == 0;
lt = cmp < 0;
gt = cmp > 0;
}
}
else if (FIXNUMP (num2))
{
int sgn = mpz_sgn (*xbignum_val (num1));
eassume (sgn != 0);
eq = false;
lt = sgn < 0;
gt = sgn > 0;
}
else if (BIGNUMP (num2))
{
int cmp = mpz_cmp (*xbignum_val (num1), *xbignum_val (num2));
eq = cmp == 0; eq = cmp == 0;
lt = cmp < 0; lt = cmp < 0;
gt = cmp > 0; gt = cmp > 0;
} }
} else
else if (FIXNUMP (num2)) not_number_or_marker (num2);
{
int sgn = mpz_sgn (*xbignum_val (num1));
eassume (sgn != 0);
eq = false;
lt = sgn < 0;
gt = sgn > 0;
} }
else else
{ not_number_or_marker (num1);
int cmp = mpz_cmp (*xbignum_val (num1), *xbignum_val (num2));
eq = cmp == 0;
lt = cmp < 0;
gt = cmp > 0;
}
return lt << Cmp_Bit_LT | gt << Cmp_Bit_GT | eq << Cmp_Bit_EQ; return lt << Cmp_Bit_LT | gt << Cmp_Bit_GT | eq << Cmp_Bit_EQ;
} }