<sys/time.h>: Add 3rd arg to timespecadd()/sub() and make them public.
authorSascha Wildner <saw@online.de>
Wed, 25 Sep 2019 17:40:52 +0000 (19:40 +0200)
committerSascha Wildner <saw@online.de>
Wed, 25 Sep 2019 18:48:41 +0000 (20:48 +0200)
* Switch to the three argument versions of the timespecadd() and
  timespecsub() macros. These are now the predominant ones. FreeBSD,
  OpenBSD, NetBSD, and Solaris (albeit only for the kernel) have them.

* Make those macros public too. This allows for a number of cleanups
  where they were defined locally.

Pointed-out-by: zrj
Reviewed-by: dillon
23 files changed:
games/tetris/input.h
sys/dev/acpica/acpi_cmbat.c
sys/dev/acpica/acpi_smbat.c
sys/dev/acpica/acpi_thermal.c
sys/dev/disk/isp/isp_freebsd.c
sys/dev/drm/include/linux/time.h
sys/dev/misc/joy/joy.c
sys/kern/kern_clock.c
sys/kern/kern_event.c
sys/kern/kern_sig.c
sys/kern/kern_time.c
sys/kern/sys_mqueue.c
sys/netproto/smb/smb_iod.c
sys/netproto/smb/smb_trantcp.c
sys/opencrypto/crypto.c
sys/sys/param.h
sys/sys/time.h
test/testcases/posixipc/common/common.c
test/testcases/posixipc/common/common.h
tools/tools/netrate/accept_connect/kq_connect_client/kq_connect_client.c
tools/tools/netrate/kq_sendrecv/kq_sendcli/kq_sendcli.c
usr.bin/who/utmpentry.c
usr.sbin/powerd/powerd.c

index 351fa6b..eb1d1f1 100644 (file)
  *     @(#)input.h     8.1 (Berkeley) 5/31/93
  */
 
-/* Operations on timespecs. */
-#define        timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
-#define        timespecsub(tsp, usp, vsp)                                      \
-       do {                                                            \
-               (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
-               (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
-               if ((vsp)->tv_nsec < 0) {                               \
-                       (vsp)->tv_sec--;                                \
-                       (vsp)->tv_nsec += 1000000000L;                  \
-               }                                                       \
-       } while (/* CONSTCOND */ 0)
-
 int rwait(struct timespec *);
 int tgetchar(void);
 void tsleep(void);
index 1f63643..ae32aed 100644 (file)
@@ -222,7 +222,7 @@ acpi_cmbat_info_expired(struct timespec *lastupdated)
        return (TRUE);
 
     getnanotime(&curtime);
-    timespecsub(&curtime, lastupdated);
+    timespecsub(&curtime, lastupdated, &curtime);
     return (curtime.tv_sec < 0 ||
            curtime.tv_sec > acpi_battery_get_info_expire());
 }
index 37672d2..3b4a7aa 100644 (file)
@@ -171,7 +171,7 @@ acpi_smbat_info_expired(struct timespec *lastupdated)
                return (TRUE);
 
        getnanotime(&curtime);
-       timespecsub(&curtime, lastupdated);
+       timespecsub(&curtime, lastupdated, &curtime);
        return (curtime.tv_sec < 0 ||
            curtime.tv_sec > acpi_battery_get_info_expire());
 }
index 12a91aa..caf7f8a 100644 (file)
@@ -570,7 +570,7 @@ acpi_tz_monitor(void *Context)
        (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
 
        getnanotime(&curtime);
-       timespecsub(&curtime, &sc->tz_cooling_started);
+       timespecsub(&curtime, &sc->tz_cooling_started, &curtime);
        if (curtime.tv_sec < acpi_tz_min_runtime)
            newactive = sc->tz_active;
     }
index 7696ed9..a004552 100644 (file)
@@ -5595,8 +5595,9 @@ uint64_t
 isp_nanotime_sub(struct timespec *b, struct timespec *a)
 {
        uint64_t elapsed;
-       struct timespec x = *b;
-       timespecsub(&x, a);
+       struct timespec x;
+
+       timespecsub(b, a, &x);
        elapsed = GET_NANOSEC(&x);
        if (elapsed == 0)
                elapsed++;
index 4cba112..b2f2211 100644 (file)
@@ -75,9 +75,7 @@ timespec_sub(struct timespec lhs, struct timespec rhs)
 {
        struct timespec ts;
 
-       ts.tv_sec = lhs.tv_sec;
-       ts.tv_nsec = lhs.tv_nsec;
-       timespecsub(&ts, &rhs);
+       timespecsub(&lhs, &rhs, &ts);
 
        return ts;
 }
index faf9603..5f4b7fe 100644 (file)
@@ -193,7 +193,7 @@ joyread(struct dev_read_args *ap)
     nanotime(&start);
     end.tv_sec = 0;
     end.tv_nsec = joy->timeout[joypart(dev)] * 1000;
-    timespecadd(&end, &start);
+    timespecadd(&end, &start, &end);
     t = start;
     timespecclear(&x);
     timespecclear(&y);
@@ -213,12 +213,12 @@ joyread(struct dev_read_args *ap)
     crit_exit();
 
     if (timespecisset(&x)) {
-       timespecsub(&x, &start);
+       timespecsub(&x, &start, &x);
        c.x = joy->x_off[joypart(dev)] + x.tv_nsec / 1000;
     } else
        c.x = 0x80000000;
     if (timespecisset(&y)) {
-       timespecsub(&y, &start);
+       timespecsub(&y, &start, &y);
        c.y = joy->y_off[joypart(dev)] + y.tv_nsec / 1000;
     } else
        c.y = 0x80000000;
index 30c44ac..ca9c5b7 100644 (file)
@@ -1654,7 +1654,7 @@ pps_event(struct pps_state *pps, sysclock_t count, int event)
        *tsp = ts;
 
        if (foff) {
-               timespecadd(tsp, osp);
+               timespecadd(tsp, osp, tsp);
                if (tsp->tv_nsec < 0) {
                        tsp->tv_nsec += 1000000000;
                        tsp->tv_sec -= 1;
index 82f7f89..021b451 100644 (file)
@@ -864,7 +864,7 @@ kern_kevent(struct kqueue *kq, int nevents, int *res, void *uap,
        if (tsp != NULL) {
                if (tsp->tv_sec || tsp->tv_nsec) {
                        getnanouptime(&ats);
-                       timespecadd(tsp, &ats);         /* tsp = target time */
+                       timespecadd(tsp, &ats, tsp);    /* tsp = target time */
                }
        }
 
@@ -915,7 +915,7 @@ kern_kevent(struct kqueue *kq, int nevents, int *res, void *uap,
                                struct timespec atx = *tsp;
 
                                getnanouptime(&ats);
-                               timespecsub(&atx, &ats);
+                               timespecsub(&atx, &ats, &atx);
                                if (atx.tv_sec < 0) {
                                        error = EWOULDBLOCK;
                                        break;
index b5c868b..43f4881 100644 (file)
@@ -1747,8 +1747,7 @@ kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
                    timeout->tv_nsec < 1000000000) {
                        timevalid = 1;
                        getnanouptime(&rts);
-                       ets = rts;
-                       timespecadd(&ets, timeout);
+                       timespecadd(&rts, timeout, &ets);
                }
        }
 
@@ -1793,8 +1792,7 @@ kern_sigtimedwait(sigset_t waitset, siginfo_t *info, struct timespec *timeout)
                                error = EAGAIN;
                                break;
                        }
-                       ts = ets;
-                       timespecsub(&ts, &rts);
+                       timespecsub(&ets, &rts, &ts);
                        TIMESPEC_TO_TIMEVAL(&tv, &ts);
                        hz = tvtohz_high(&tv);
                } else {
index b49af43..6e8ba40 100644 (file)
@@ -474,7 +474,7 @@ nanosleep1(struct timespec *rqt, struct timespec *rmt)
        if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
                return (0);
        nanouptime(&ts);
-       timespecadd(&ts, rqt);          /* ts = target timestamp compare */
+       timespecadd(&ts, rqt, &ts);     /* ts = target timestamp compare */
        TIMESPEC_TO_TIMEVAL(&tv, rqt);  /* tv = sleep interval */
 
        for (;;) {
@@ -511,7 +511,7 @@ nanosleep1(struct timespec *rqt, struct timespec *rmt)
                        if (error == ERESTART)
                                error = EINTR;
                        if (rmt != NULL) {
-                               timespecsub(&ts, &ts2);
+                               timespecsub(&ts, &ts2, &ts);
                                if (ts.tv_sec < 0)
                                        timespecclear(&ts);
                                *rmt = ts;
@@ -520,8 +520,7 @@ nanosleep1(struct timespec *rqt, struct timespec *rmt)
                }
                if (timespeccmp(&ts2, &ts, >=))
                        return (0);
-               ts3 = ts;
-               timespecsub(&ts3, &ts2);
+               timespecsub(&ts, &ts2, &ts3);
                TIMESPEC_TO_TIMEVAL(&tv, &ts3);
        }
 }
index 3c44b78..2d57d6c 100644 (file)
@@ -247,7 +247,7 @@ abstimeout2timo(struct timespec *ts, int *timo)
                return error;
        }
        getnanotime(&tsd);
-       timespecsub(ts, &tsd);
+       timespecsub(ts, &tsd, ts);
        if (ts->tv_sec < 0 || (ts->tv_sec == 0 && ts->tv_nsec <= 0)) {
                return ETIMEDOUT;
        }
index ccef727..9f5590f 100644 (file)
@@ -574,9 +574,9 @@ smb_iod_sendall(struct smbiod *iod)
                        break;
                    case SMBRQ_SENT:
                        SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout);
-                       timespecadd(&tstimeout, &tstimeout);
+                       timespecadd(&tstimeout, &tstimeout, &tstimeout);
                        getnanotime(&ts);
-                       timespecsub(&ts, &tstimeout);
+                       timespecsub(&ts, &tstimeout, &ts);
                        if (timespeccmp(&ts, &rqp->sr_timesent, >)) {
                                smb_iod_rqprocessed(rqp, ETIMEDOUT);
                        }
@@ -647,7 +647,7 @@ smb_iod_main(struct smbiod *iod)
 #if 0
        if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
                getnanotime(&tsnow);
-               timespecsub(&tsnow, &iod->iod_pingtimo);
+               timespecsub(&tsnow, &iod->iod_pingtimo, &tsnow);
                if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) {
                        smb_smb_echo(vcp, &iod->iod_scred);
                }
index 27e18d1..de6468c 100644 (file)
@@ -473,15 +473,14 @@ smb_nbst_connect(struct smb_vc *vcp, struct sockaddr *sap, struct thread *td)
        if (error)
                return error;
        getnanotime(&ts2);
-       timespecsub(&ts2, &ts1);
+       timespecsub(&ts2, &ts1, &ts2);
        if (ts2.tv_sec == 0) {
                ts2.tv_sec = 1;
                ts2.tv_nsec = 0;
        }
-       nbp->nbp_timo = ts2;
-       timespecadd(&nbp->nbp_timo, &ts2);
-       timespecadd(&nbp->nbp_timo, &ts2);
-       timespecadd(&nbp->nbp_timo, &ts2);      /*  * 4 */
+       timespecadd(&ts2, &ts2, &nbp->nbp_timo);
+       timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);
+       timespecadd(&nbp->nbp_timo, &ts2, &nbp->nbp_timo);      /*  * 4 */
        error = nbssn_rq_request(nbp, td);
        if (error)
                smb_nbst_disconnect(vcp, td);
index 2666b72..b4e32c2 100644 (file)
@@ -1027,7 +1027,7 @@ crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
                t.tv_sec--;
                t.tv_nsec += 1000000000;
        }
-       timespecadd(&ts->acc, &t);
+       timespecadd(&ts->acc, &t, &ts->acc);
        if (timespeccmp(&t, &ts->min, <))
                ts->min = t;
        if (timespeccmp(&t, &ts->max, >))
index b27b258..fb1c1ca 100644 (file)
  * 500703 - Static TLS bindings support for late-loaded shared libraries
  * 500704 - Announce IP6 address flag changes via route(4)
  * 500705 - Move us to utmpx only, delete utmp
+ * 500706 - Switch to the now common three argument versions of the
+ *         timespecadd() and timespecsub() macros in <sys/time.h>
  */
 #undef __DragonFly_version
-#define __DragonFly_version 500705     /* propagated to newvers */
+#define __DragonFly_version 500706     /* propagated to newvers */
 
 #include <sys/_null.h>
 
index 355308f..807787a 100644 (file)
@@ -67,8 +67,6 @@ struct timezone {
 #define        DST_EET         5       /* Eastern European dst */
 #define        DST_CAN         6       /* Canada */
 
-#ifdef _KERNEL
-
 /* Operations on timespecs */
 #define        timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
 #define        timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
@@ -76,22 +74,22 @@ struct timezone {
        (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
            ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
            ((tvp)->tv_sec cmp (uvp)->tv_sec))
-#define timespecadd(vvp, uvp)                                          \
+#define        timespecadd(tsp, usp, vsp)                                      \
        do {                                                            \
-               (vvp)->tv_sec += (uvp)->tv_sec;                         \
-               (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
-               if ((vvp)->tv_nsec >= 1000000000) {                     \
-                       (vvp)->tv_sec++;                                \
-                       (vvp)->tv_nsec -= 1000000000;                   \
+               (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
+               (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
+               if ((vsp)->tv_nsec >= 1000000000L) {                    \
+                       (vsp)->tv_sec++;                                \
+                       (vsp)->tv_nsec -= 1000000000L;                  \
                }                                                       \
        } while (0)
-#define timespecsub(vvp, uvp)                                          \
+#define        timespecsub(tsp, usp, vsp)                                      \
        do {                                                            \
-               (vvp)->tv_sec -= (uvp)->tv_sec;                         \
-               (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
-               if ((vvp)->tv_nsec < 0) {                               \
-                       (vvp)->tv_sec--;                                \
-                       (vvp)->tv_nsec += 1000000000;                   \
+               (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
+               (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
+               if ((vsp)->tv_nsec < 0) {                               \
+                       (vsp)->tv_sec--;                                \
+                       (vsp)->tv_nsec += 1000000000L;                  \
                }                                                       \
        } while (0)
 
@@ -106,8 +104,6 @@ struct timezone {
 
 /* timevaladd and timevalsub are not inlined */
 
-#endif /* _KERNEL */
-
 #ifndef _KERNEL                        /* NetBSD/OpenBSD compatible interfaces */
 
 #define        timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
index fc97e6b..d806b2b 100644 (file)
@@ -118,7 +118,7 @@ testwait(sem_t *id, u_int *delta)
                perror("clock_gettime(CLOCK_REALTIME)");
                return (-1);
        }
-       timespecsub(&end, &start);
+       timespecsub(&end, &start, &end);
        *delta = end.tv_nsec / 1000000;
        *delta += end.tv_sec * 1000;
        return (0);
@@ -268,7 +268,7 @@ timedwait(sem_t *id, u_int msec, u_int *delta, int error)
        }
        end.tv_sec = msec / 1000;
        end.tv_nsec = msec % 1000 * 1000000;
-       timespecadd(&end, &start);
+       timespecadd(&end, &start, &end);
        if (sem_timedwait(id, &end) < 0) {
                if (errno != error) {
                        perror("sem_timedwait");
@@ -281,7 +281,7 @@ timedwait(sem_t *id, u_int msec, u_int *delta, int error)
                perror("clock_gettime(CLOCK_REALTIME)");
                return (-1);
        }
-       timespecsub(&end, &start);
+       timespecsub(&end, &start, &end);
        *delta = end.tv_nsec / 1000000;
        *delta += end.tv_sec * 1000;
        return (0);
index e5c3d37..2c97ae0 100644 (file)
@@ -51,35 +51,6 @@ extern sem_t *alarm_id;
 extern int alarm_errno;
 extern int alarm_handler_installed;
 
-/* Cut and pasted from kernel header, bah! */
-
-/* Operations on timespecs */
-#define        timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
-#define        timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
-#define        timespeccmp(tvp, uvp, cmp)                                      \
-       (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
-           ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
-           ((tvp)->tv_sec cmp (uvp)->tv_sec))
-#define timespecadd(vvp, uvp)                                          \
-       do {                                                            \
-               (vvp)->tv_sec += (uvp)->tv_sec;                         \
-               (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
-               if ((vvp)->tv_nsec >= 1000000000) {                     \
-                       (vvp)->tv_sec++;                                \
-                       (vvp)->tv_nsec -= 1000000000;                   \
-               }                                                       \
-       } while (0)
-#define timespecsub(vvp, uvp)                                          \
-       do {                                                            \
-               (vvp)->tv_sec -= (uvp)->tv_sec;                         \
-               (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
-               if ((vvp)->tv_nsec < 0) {                               \
-                       (vvp)->tv_sec--;                                \
-                       (vvp)->tv_nsec += 1000000000;                   \
-               }                                                       \
-       } while (0)
-
-
 /* Macros for passing child status to parent over a pipe. */
 #define        CSTAT(class, error)             ((class) << 16 | (error))
 #define        CSTAT_CLASS(stat)               ((stat) >> 16)
index 6b1957c..fe4d533 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
-#ifndef timespecsub
-#define timespecsub(vvp, uvp)                                           \
-       do {                                                            \
-               (vvp)->tv_sec -= (uvp)->tv_sec;                         \
-               (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
-               if ((vvp)->tv_nsec < 0) {                               \
-                       (vvp)->tv_sec--;                                \
-                       (vvp)->tv_nsec += 1000000000;                   \
-               }                                                       \
-       } while (0)
-#endif
-
 static void    mainloop(const struct sockaddr_in *, int, int, long, u_long *,
                    int, int);
 
@@ -310,7 +298,7 @@ mainloop(const struct sockaddr_in *in, int in_cnt, int nconn_max,
        }
        clock_gettime(CLOCK_MONOTONIC_PRECISE, &end);
 
-       timespecsub(&end, &start);
+       timespecsub(&end, &start, &end);
        time_us = ((double)end.tv_sec * 1000000.0) +
            ((double)end.tv_nsec / 1000.0);
 
index bc9b0e4..884be47 100644 (file)
  * start roughly at the same time.
  */
 
-#ifndef timespecsub
-#define timespecsub(vvp, uvp)                                          \
-       do {                                                            \
-               (vvp)->tv_sec -= (uvp)->tv_sec;                         \
-               (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
-               if ((vvp)->tv_nsec < 0) {                               \
-                       (vvp)->tv_sec--;                                \
-                       (vvp)->tv_nsec += 1000000000;                   \
-               }                                                       \
-       } while (0)
-#endif
-
-#ifndef timespeccmp
-#define        timespeccmp(tvp, uvp, cmp)                                      \
-       (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
-           ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
-           ((tvp)->tv_sec cmp (uvp)->tv_sec))
-#endif
-
 #if 0
 #define SEND_DEBUG
 #endif
@@ -425,8 +406,7 @@ main(int argc, char *argv[])
                ctx = &ctx_arr[i];
                pthread_join(ctx->t_tid, NULL);
 
-               run = ctx->t_end;
-               timespecsub(&run, &ctx->t_start);
+               timespecsub(&ctx->t_end, &ctx->t_start, &run);
                ctx->t_run_us = ((double)run.tv_sec * 1000000.0) +
                    ((double)run.tv_nsec / 1000.0);
 
@@ -452,8 +432,7 @@ main(int argc, char *argv[])
            start.tv_sec, start.tv_nsec, end.tv_sec, end.tv_nsec);
 #endif
 
-       run = end;
-       timespecsub(&run, &start);
+       timespecsub(&end, &start, &run);
        total_run_us = ((double)run.tv_sec * 1000000.0) +
            ((double)run.tv_nsec / 1000.0);
        total = 0.0;
@@ -508,7 +487,8 @@ main(int argc, char *argv[])
                                continue;
 
                        run = conn->c_terr;
-                       timespecsub(&run, &ctx_arr[conn->c_thr_id].t_start);
+                       timespecsub(&conn->c_terr,
+                           &ctx_arr[conn->c_thr_id].t_start, &run);
                        tmp_run = ((double)run.tv_sec * 1000000.0) +
                            ((double)run.tv_nsec / 1000.0);
                        fprintf(stderr, "snd%d ->%s:%d, %ld sec, %.2lf Mbps, "
index a6427fb..b13d73a 100644 (file)
 #include "utmpentry.h"
 
 /* Operations on timespecs. */
-#define        timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
-#define        timespecisset(tsp)      ((tsp)->tv_sec || (tsp)->tv_nsec)
-#define        timespeccmp(tsp, usp, cmp)                                      \
-       (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
-           ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
-           ((tsp)->tv_sec cmp (usp)->tv_sec))
-#define        timespecadd(tsp, usp, vsp)                                      \
-       do {                                                            \
-               (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
-               (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
-               if ((vsp)->tv_nsec >= 1000000000L) {                    \
-                       (vsp)->tv_sec++;                                \
-                       (vsp)->tv_nsec -= 1000000000L;                  \
-               }                                                       \
-       } while (/* CONSTCOND */ 0)
-#define        timespecsub(tsp, usp, vsp)                                      \
-       do {                                                            \
-               (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
-               (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
-               if ((vsp)->tv_nsec < 0) {                               \
-                       (vsp)->tv_sec--;                                \
-                       (vsp)->tv_nsec += 1000000000L;                  \
-               }                                                       \
-       } while (/* CONSTCOND */ 0)
 #define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
 
 
index 2e1c825..0975d73 100644 (file)
@@ -696,18 +696,6 @@ usage(void)
        exit(1);
 }
 
-#ifndef timespecsub
-#define timespecsub(vvp, uvp)                                          \
-       do {                                                            \
-               (vvp)->tv_sec -= (uvp)->tv_sec;                         \
-               (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
-               if ((vvp)->tv_nsec < 0) {                               \
-                       (vvp)->tv_sec--;                                \
-                       (vvp)->tv_nsec += 1000000000;                   \
-               }                                                       \
-       } while (0)
-#endif
-
 #define BAT_SYSCTL_TIME_MAX    50000000 /* unit: nanosecond */
 
 static int
@@ -727,7 +715,7 @@ has_battery(void)
        }
        clock_gettime(CLOCK_MONOTONIC_FAST, &e);
 
-       timespecsub(&e, &s);
+       timespecsub(&e, &s, &e);
        if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
                /* hw.acpi.acline takes to long to be useful */
                syslog(LOG_NOTICE, "hw.acpi.acline takes too long");
@@ -742,7 +730,7 @@ has_battery(void)
        }
        clock_gettime(CLOCK_MONOTONIC_FAST, &e);
 
-       timespecsub(&e, &s);
+       timespecsub(&e, &s, &e);
        if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
                /* hw.acpi.battery.life takes to long to be useful */
                syslog(LOG_NOTICE, "hw.acpi.battery.life takes too long");
@@ -795,8 +783,7 @@ mon_battery(void)
        size_t len;
 
        clock_gettime(CLOCK_MONOTONIC_FAST, &cur);
-       ts = cur;
-       timespecsub(&ts, &BatLifePrevT);
+       timespecsub(&cur, &BatLifePrevT, &ts);
        if (ts.tv_sec < BatLifePollIntvl)
                return 1;
        BatLifePrevT = cur;
@@ -852,8 +839,7 @@ after_backlight:
                return 1;
 
        if (BatShutdownLinger > 0) {
-               ts = cur;
-               timespecsub(&ts, &BatShutdownStartT);
+               timespecsub(&cur, &BatShutdownStartT, &ts);
                if (ts.tv_sec > BatShutdownLinger)
                        BatShutdownLinger = 0;
        }