diff options
| author | Paul Eggert | 2020-03-08 00:20:57 -0800 |
|---|---|---|
| committer | Paul Eggert | 2020-03-08 00:20:57 -0800 |
| commit | 4415534ef01309417e8f552eb4c075095603f2f3 (patch) | |
| tree | ca55889bb9636c2b2d16e4a29fcc14b2d18e3fdc /src | |
| parent | e4fb95fa18072cedb021a82f7aa0e79fa6ca387a (diff) | |
| parent | 0a3682a566d5563e3d57defe49359cee236e0274 (diff) | |
| download | emacs-4415534ef01309417e8f552eb4c075095603f2f3.tar.gz emacs-4415534ef01309417e8f552eb4c075095603f2f3.zip | |
Merge from origin/emacs-27
0a3682a566 * src/timefns.c: Add comments.
b16ba4041d ; lisp/emacs-lisp/seq.el: Explain why we don't use cl-lib ...
3cbf4cb796 Eliminate use of cl-concatenate in 'seq' package
363d927086 Fix bug with JIT stealth timers
818333c85a * doc/lispref/os.texi (time-subtract): Doc fix.
Diffstat (limited to 'src')
| -rw-r--r-- | src/timefns.c | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/timefns.c b/src/timefns.c index 41db1e68759..6dd6e1611a4 100644 --- a/src/timefns.c +++ b/src/timefns.c | |||
| @@ -491,11 +491,14 @@ timespec_mpz (struct timespec t) | |||
| 491 | static Lisp_Object | 491 | static Lisp_Object |
| 492 | timespec_ticks (struct timespec t) | 492 | timespec_ticks (struct timespec t) |
| 493 | { | 493 | { |
| 494 | /* For speed, use intmax_t arithmetic if it will do. */ | ||
| 494 | intmax_t accum; | 495 | intmax_t accum; |
| 495 | if (FASTER_TIMEFNS | 496 | if (FASTER_TIMEFNS |
| 496 | && !INT_MULTIPLY_WRAPV (t.tv_sec, TIMESPEC_HZ, &accum) | 497 | && !INT_MULTIPLY_WRAPV (t.tv_sec, TIMESPEC_HZ, &accum) |
| 497 | && !INT_ADD_WRAPV (t.tv_nsec, accum, &accum)) | 498 | && !INT_ADD_WRAPV (t.tv_nsec, accum, &accum)) |
| 498 | return make_int (accum); | 499 | return make_int (accum); |
| 500 | |||
| 501 | /* Fall back on bignum arithmetic. */ | ||
| 499 | timespec_mpz (t); | 502 | timespec_mpz (t); |
| 500 | return make_integer_mpz (); | 503 | return make_integer_mpz (); |
| 501 | } | 504 | } |
| @@ -505,12 +508,17 @@ timespec_ticks (struct timespec t) | |||
| 505 | static Lisp_Object | 508 | static Lisp_Object |
| 506 | lisp_time_hz_ticks (struct lisp_time t, Lisp_Object hz) | 509 | lisp_time_hz_ticks (struct lisp_time t, Lisp_Object hz) |
| 507 | { | 510 | { |
| 511 | /* For speed, just return TICKS if T is (TICKS . HZ). */ | ||
| 508 | if (FASTER_TIMEFNS && EQ (t.hz, hz)) | 512 | if (FASTER_TIMEFNS && EQ (t.hz, hz)) |
| 509 | return t.ticks; | 513 | return t.ticks; |
| 514 | |||
| 515 | /* Check HZ for validity. */ | ||
| 510 | if (FIXNUMP (hz)) | 516 | if (FIXNUMP (hz)) |
| 511 | { | 517 | { |
| 512 | if (XFIXNUM (hz) <= 0) | 518 | if (XFIXNUM (hz) <= 0) |
| 513 | invalid_hz (hz); | 519 | invalid_hz (hz); |
| 520 | |||
| 521 | /* For speed, use intmax_t arithmetic if it will do. */ | ||
| 514 | intmax_t ticks; | 522 | intmax_t ticks; |
| 515 | if (FASTER_TIMEFNS && FIXNUMP (t.ticks) && FIXNUMP (t.hz) | 523 | if (FASTER_TIMEFNS && FIXNUMP (t.ticks) && FIXNUMP (t.hz) |
| 516 | && !INT_MULTIPLY_WRAPV (XFIXNUM (t.ticks), XFIXNUM (hz), &ticks)) | 524 | && !INT_MULTIPLY_WRAPV (XFIXNUM (t.ticks), XFIXNUM (hz), &ticks)) |
| @@ -520,6 +528,7 @@ lisp_time_hz_ticks (struct lisp_time t, Lisp_Object hz) | |||
| 520 | else if (! (BIGNUMP (hz) && 0 < mpz_sgn (*xbignum_val (hz)))) | 528 | else if (! (BIGNUMP (hz) && 0 < mpz_sgn (*xbignum_val (hz)))) |
| 521 | invalid_hz (hz); | 529 | invalid_hz (hz); |
| 522 | 530 | ||
| 531 | /* Fall back on bignum arithmetic. */ | ||
| 523 | mpz_mul (mpz[0], | 532 | mpz_mul (mpz[0], |
| 524 | *bignum_integer (&mpz[0], t.ticks), | 533 | *bignum_integer (&mpz[0], t.ticks), |
| 525 | *bignum_integer (&mpz[1], hz)); | 534 | *bignum_integer (&mpz[1], hz)); |
| @@ -533,9 +542,13 @@ lisp_time_seconds (struct lisp_time t) | |||
| 533 | { | 542 | { |
| 534 | if (!FASTER_TIMEFNS) | 543 | if (!FASTER_TIMEFNS) |
| 535 | return lisp_time_hz_ticks (t, make_fixnum (1)); | 544 | return lisp_time_hz_ticks (t, make_fixnum (1)); |
| 545 | |||
| 546 | /* For speed, use EMACS_INT arithmetic if it will do. */ | ||
| 536 | if (FIXNUMP (t.ticks) && FIXNUMP (t.hz)) | 547 | if (FIXNUMP (t.ticks) && FIXNUMP (t.hz)) |
| 537 | return make_fixnum (XFIXNUM (t.ticks) / XFIXNUM (t.hz) | 548 | return make_fixnum (XFIXNUM (t.ticks) / XFIXNUM (t.hz) |
| 538 | - (XFIXNUM (t.ticks) % XFIXNUM (t.hz) < 0)); | 549 | - (XFIXNUM (t.ticks) % XFIXNUM (t.hz) < 0)); |
| 550 | |||
| 551 | /* For speed, inline what lisp_time_hz_ticks would do. */ | ||
| 539 | mpz_fdiv_q (mpz[0], | 552 | mpz_fdiv_q (mpz[0], |
| 540 | *bignum_integer (&mpz[0], t.ticks), | 553 | *bignum_integer (&mpz[0], t.ticks), |
| 541 | *bignum_integer (&mpz[1], t.hz)); | 554 | *bignum_integer (&mpz[1], t.hz)); |
| @@ -1122,21 +1135,22 @@ time_arith (Lisp_Object a, Lisp_Object b, bool subtract) | |||
| 1122 | (subtract ? mpz_submul : mpz_addmul) (*iticks, *fa, *nb); | 1135 | (subtract ? mpz_submul : mpz_addmul) (*iticks, *fa, *nb); |
| 1123 | 1136 | ||
| 1124 | /* Normalize iticks/ihz by dividing both numerator and | 1137 | /* Normalize iticks/ihz by dividing both numerator and |
| 1125 | denominator by ig = gcd (iticks, ihz). However, if that | 1138 | denominator by ig = gcd (iticks, ihz). For speed, though, |
| 1126 | would cause the denominator to become less than hzmin, | 1139 | skip this division if ihz = 1. */ |
| 1127 | rescale the denominator upwards from its ordinary value by | ||
| 1128 | multiplying numerator and denominator so that the denominator | ||
| 1129 | becomes at least hzmin. This rescaling avoids returning a | ||
| 1130 | timestamp that is less precise than both a and b, or a | ||
| 1131 | timestamp that looks obsolete when that might be a problem. */ | ||
| 1132 | mpz_t *ig = &mpz[3]; | 1140 | mpz_t *ig = &mpz[3]; |
| 1133 | mpz_gcd (*ig, *iticks, *ihz); | 1141 | mpz_gcd (*ig, *iticks, *ihz); |
| 1134 | |||
| 1135 | if (!FASTER_TIMEFNS || mpz_cmp_ui (*ig, 1) > 0) | 1142 | if (!FASTER_TIMEFNS || mpz_cmp_ui (*ig, 1) > 0) |
| 1136 | { | 1143 | { |
| 1137 | mpz_divexact (*iticks, *iticks, *ig); | 1144 | mpz_divexact (*iticks, *iticks, *ig); |
| 1138 | mpz_divexact (*ihz, *ihz, *ig); | 1145 | mpz_divexact (*ihz, *ihz, *ig); |
| 1139 | 1146 | ||
| 1147 | /* However, if dividing the denominator by ig would cause the | ||
| 1148 | denominator to become less than hzmin, rescale the denominator | ||
| 1149 | upwards by multiplying the normalized numerator and denominator | ||
| 1150 | so that the resulting denominator becomes at least hzmin. | ||
| 1151 | This rescaling avoids returning a timestamp that is less precise | ||
| 1152 | than both a and b, or a timestamp that looks obsolete when that | ||
| 1153 | might be a problem. */ | ||
| 1140 | if (!FASTER_TIMEFNS || mpz_cmp (*ihz, *hzmin) < 0) | 1154 | if (!FASTER_TIMEFNS || mpz_cmp (*ihz, *hzmin) < 0) |
| 1141 | { | 1155 | { |
| 1142 | /* Rescale straightforwardly. Although this might not | 1156 | /* Rescale straightforwardly. Although this might not |
| @@ -1150,6 +1164,8 @@ time_arith (Lisp_Object a, Lisp_Object b, bool subtract) | |||
| 1150 | mpz_mul (*ihz, *ihz, *rescale); | 1164 | mpz_mul (*ihz, *ihz, *rescale); |
| 1151 | } | 1165 | } |
| 1152 | } | 1166 | } |
| 1167 | |||
| 1168 | /* mpz[0] and iticks now correspond to the (HZ . TICKS) pair. */ | ||
| 1153 | hz = make_integer_mpz (); | 1169 | hz = make_integer_mpz (); |
| 1154 | mpz_swap (mpz[0], *iticks); | 1170 | mpz_swap (mpz[0], *iticks); |
| 1155 | ticks = make_integer_mpz (); | 1171 | ticks = make_integer_mpz (); |