summaryrefslogtreecommitdiff
path: root/src/timefns.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2022-08-01 00:38:32 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2022-08-01 01:17:13 -0700
commit914cf4b91ba8bdf63cd10a66f970c396329beeab (patch)
treeedf43bdb4f21bcb3e4f3c53e32d87a898c60da38 /src/timefns.c
parente3b6242938fa9bcc3aebaa0eddd6aa561136dbf1 (diff)
downloademacs-914cf4b91ba8bdf63cd10a66f970c396329beeab.tar.gz
Make time arithmetic more like comparison
Since time comparison says X == X, have time arithmetic behave similarly for X - X. This should also be a bit faster due to not having to test for floats and NaNs. * src/timefns.c (time_arith, time_cmp): Simplify by not worrying about NaNs, which are not time values. (time_arith): Simplify by not worrying about subtracting nil from nil; the caller now handles this. (Ftime_subtract): Handle subtracting X from X specially.
Diffstat (limited to 'src/timefns.c')
-rw-r--r--src/timefns.c44
1 files changed, 11 insertions, 33 deletions
diff --git a/src/timefns.c b/src/timefns.c
index 25bfda513c2..c8becac8634 100644
--- a/src/timefns.c
+++ b/src/timefns.c
@@ -1074,27 +1074,9 @@ lispint_arith (Lisp_Object a, Lisp_Object b, bool subtract)
static Lisp_Object
time_arith (Lisp_Object a, Lisp_Object b, bool subtract)
{
- if (FLOATP (a) && !isfinite (XFLOAT_DATA (a)))
- {
- double da = XFLOAT_DATA (a);
- double db = float_time (b);
- return make_float (subtract ? da - db : da + db);
- }
enum timeform aform, bform;
struct lisp_time ta = lisp_time_struct (a, &aform);
-
- if (FLOATP (b) && !isfinite (XFLOAT_DATA (b)))
- return subtract ? make_float (-XFLOAT_DATA (b)) : b;
-
- /* Subtract nil from nil correctly, and handle other eq values
- quicker while we're at it. Compare here rather than earlier, to
- handle NaNs and check formats. */
- struct lisp_time tb;
- if (BASE_EQ (a, b))
- bform = aform, tb = ta;
- else
- tb = lisp_time_struct (b, &bform);
-
+ struct lisp_time tb = lisp_time_struct (b, &bform);
Lisp_Object ticks, hz;
if (FASTER_TIMEFNS && BASE_EQ (ta.hz, tb.hz))
@@ -1201,27 +1183,23 @@ See `format-time-string' for the various forms of a time value.
For example, nil stands for the current time. */)
(Lisp_Object a, Lisp_Object b)
{
+ /* Subtract nil from nil correctly, and handle other eq values
+ quicker while we're at it. This means (time-subtract X X) does
+ not signal an error if X is not a valid time value, but that's OK. */
+ if (BASE_EQ (a, b))
+ return timespec_to_lisp ((struct timespec) {0});
+
return time_arith (a, b, true);
}
-/* Return negative, 0, positive if a < b, a == b, a > b respectively.
- Return positive if either a or b is a NaN; this is good enough
- for the current callers. */
+/* Return negative, 0, positive if A < B, A == B, A > B respectively.
+ A and B should be Lisp time values. */
static int
time_cmp (Lisp_Object a, Lisp_Object b)
{
- if ((FLOATP (a) && !isfinite (XFLOAT_DATA (a)))
- || (FLOATP (b) && !isfinite (XFLOAT_DATA (b))))
- {
- double da = FLOATP (a) ? XFLOAT_DATA (a) : 0;
- double db = FLOATP (b) ? XFLOAT_DATA (b) : 0;
- return da < db ? -1 : da != db;
- }
-
/* Compare nil to nil correctly, and handle other eq values quicker
- while we're at it. Compare here rather than earlier, to handle
- NaNs. This means (time-equal-p X X) does not signal an error if
- X is not a valid time value, but that's OK. */
+ while we're at it. This means (time-equal-p X X) does not signal
+ an error if X is not a valid time value, but that's OK. */
if (BASE_EQ (a, b))
return 0;