Home | History | Annotate | Line # | Download | only in kern
subr_time_arith.c revision 1.2
      1  1.2  riastrad /*	$NetBSD: subr_time_arith.c,v 1.2 2025/04/01 23:02:29 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009, 2020
      5  1.1  riastrad  *     The NetBSD Foundation, Inc.
      6  1.1  riastrad  * All rights reserved.
      7  1.1  riastrad  *
      8  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      9  1.1  riastrad  * by Christopher G. Demetriou, by Andrew Doran, and by Jason R. Thorpe.
     10  1.1  riastrad  *
     11  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     12  1.1  riastrad  * modification, are permitted provided that the following conditions
     13  1.1  riastrad  * are met:
     14  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     15  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     16  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     19  1.1  riastrad  *
     20  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1  riastrad  */
     32  1.1  riastrad 
     33  1.1  riastrad /*
     34  1.1  riastrad  * Copyright (c) 1982, 1986, 1989, 1993
     35  1.1  riastrad  *	The Regents of the University of California.  All rights reserved.
     36  1.1  riastrad  *
     37  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     38  1.1  riastrad  * modification, are permitted provided that the following conditions
     39  1.1  riastrad  * are met:
     40  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     41  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     42  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     43  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     44  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     45  1.1  riastrad  * 3. Neither the name of the University nor the names of its contributors
     46  1.1  riastrad  *    may be used to endorse or promote products derived from this software
     47  1.1  riastrad  *    without specific prior written permission.
     48  1.1  riastrad  *
     49  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  1.1  riastrad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  1.1  riastrad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  1.1  riastrad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  1.1  riastrad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  1.1  riastrad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  1.1  riastrad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  1.1  riastrad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  1.1  riastrad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  1.1  riastrad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  1.1  riastrad  * SUCH DAMAGE.
     60  1.1  riastrad  *
     61  1.1  riastrad  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     62  1.1  riastrad  *	@(#)kern_time.c 8.4 (Berkeley) 5/26/95
     63  1.1  riastrad  */
     64  1.1  riastrad 
     65  1.1  riastrad #include <sys/cdefs.h>
     66  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: subr_time_arith.c,v 1.2 2025/04/01 23:02:29 riastradh Exp $");
     67  1.1  riastrad 
     68  1.1  riastrad #include <sys/types.h>
     69  1.1  riastrad 
     70  1.1  riastrad #include <sys/errno.h>
     71  1.1  riastrad #include <sys/time.h>
     72  1.1  riastrad #include <sys/timearith.h>
     73  1.1  riastrad 
     74  1.1  riastrad #if defined(_KERNEL)
     75  1.1  riastrad 
     76  1.1  riastrad #include <sys/kernel.h>
     77  1.1  riastrad #include <sys/systm.h>
     78  1.1  riastrad 
     79  1.1  riastrad #include <machine/limits.h>
     80  1.1  riastrad 
     81  1.1  riastrad #elif defined(_TIME_TESTING)
     82  1.1  riastrad 
     83  1.1  riastrad #include <assert.h>
     84  1.1  riastrad #include <limits.h>
     85  1.1  riastrad #include <stdbool.h>
     86  1.1  riastrad 
     87  1.1  riastrad extern int hz;
     88  1.2  riastrad extern int tick;
     89  1.1  riastrad 
     90  1.1  riastrad #define	KASSERT		assert
     91  1.1  riastrad 
     92  1.1  riastrad #endif
     93  1.1  riastrad 
     94  1.1  riastrad /*
     95  1.1  riastrad  * Compute number of ticks in the specified amount of time.
     96  1.1  riastrad  */
     97  1.1  riastrad int
     98  1.1  riastrad tvtohz(const struct timeval *tv)
     99  1.1  riastrad {
    100  1.1  riastrad 	unsigned long ticks;
    101  1.1  riastrad 	long sec, usec;
    102  1.1  riastrad 
    103  1.1  riastrad 	/*
    104  1.1  riastrad 	 * If the number of usecs in the whole seconds part of the time
    105  1.1  riastrad 	 * difference fits in a long, then the total number of usecs will
    106  1.1  riastrad 	 * fit in an unsigned long.  Compute the total and convert it to
    107  1.1  riastrad 	 * ticks, rounding up and adding 1 to allow for the current tick
    108  1.1  riastrad 	 * to expire.  Rounding also depends on unsigned long arithmetic
    109  1.1  riastrad 	 * to avoid overflow.
    110  1.1  riastrad 	 *
    111  1.1  riastrad 	 * Otherwise, if the number of ticks in the whole seconds part of
    112  1.1  riastrad 	 * the time difference fits in a long, then convert the parts to
    113  1.1  riastrad 	 * ticks separately and add, using similar rounding methods and
    114  1.1  riastrad 	 * overflow avoidance.  This method would work in the previous
    115  1.1  riastrad 	 * case, but it is slightly slower and assumes that hz is integral.
    116  1.1  riastrad 	 *
    117  1.1  riastrad 	 * Otherwise, round the time difference down to the maximum
    118  1.1  riastrad 	 * representable value.
    119  1.1  riastrad 	 *
    120  1.1  riastrad 	 * If ints are 32-bit, then the maximum value for any timeout in
    121  1.1  riastrad 	 * 10ms ticks is 248 days.
    122  1.1  riastrad 	 */
    123  1.1  riastrad 	sec = tv->tv_sec;
    124  1.1  riastrad 	usec = tv->tv_usec;
    125  1.1  riastrad 
    126  1.1  riastrad 	KASSERT(usec >= 0);
    127  1.1  riastrad 	KASSERT(usec < 1000000);
    128  1.1  riastrad 
    129  1.1  riastrad 	/* catch overflows in conversion time_t->int */
    130  1.1  riastrad 	if (tv->tv_sec > INT_MAX)
    131  1.1  riastrad 		return INT_MAX;
    132  1.1  riastrad 	if (tv->tv_sec < 0)
    133  1.1  riastrad 		return 0;
    134  1.1  riastrad 
    135  1.1  riastrad 	if (sec < 0 || (sec == 0 && usec == 0)) {
    136  1.1  riastrad 		/*
    137  1.1  riastrad 		 * Would expire now or in the past.  Return 0 ticks.
    138  1.1  riastrad 		 * This is different from the legacy tvhzto() interface,
    139  1.1  riastrad 		 * and callers need to check for it.
    140  1.1  riastrad 		 */
    141  1.1  riastrad 		ticks = 0;
    142  1.1  riastrad 	} else if (sec <= (LONG_MAX / 1000000))
    143  1.1  riastrad 		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
    144  1.1  riastrad 		    / tick) + 1;
    145  1.1  riastrad 	else if (sec <= (LONG_MAX / hz))
    146  1.1  riastrad 		ticks = (sec * hz) +
    147  1.1  riastrad 		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
    148  1.1  riastrad 	else
    149  1.1  riastrad 		ticks = LONG_MAX;
    150  1.1  riastrad 
    151  1.1  riastrad 	if (ticks > INT_MAX)
    152  1.1  riastrad 		ticks = INT_MAX;
    153  1.1  riastrad 
    154  1.1  riastrad 	return ((int)ticks);
    155  1.1  riastrad }
    156  1.1  riastrad 
    157  1.1  riastrad /*
    158  1.1  riastrad  * Check that a proposed value to load into the .it_value or
    159  1.1  riastrad  * .it_interval part of an interval timer is acceptable, and
    160  1.1  riastrad  * fix it to have at least minimal value (i.e. if it is less
    161  1.1  riastrad  * than the resolution of the clock, round it up.). We don't
    162  1.1  riastrad  * timeout the 0,0 value because this means to disable the
    163  1.1  riastrad  * timer or the interval.
    164  1.1  riastrad  */
    165  1.1  riastrad int
    166  1.1  riastrad itimerfix(struct timeval *tv)
    167  1.1  riastrad {
    168  1.1  riastrad 
    169  1.1  riastrad 	if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    170  1.1  riastrad 		return EINVAL;
    171  1.1  riastrad 	if (tv->tv_sec < 0)
    172  1.1  riastrad 		return ETIMEDOUT;
    173  1.1  riastrad 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    174  1.1  riastrad 		tv->tv_usec = tick;
    175  1.1  riastrad 	return 0;
    176  1.1  riastrad }
    177  1.1  riastrad 
    178  1.1  riastrad int
    179  1.1  riastrad itimespecfix(struct timespec *ts)
    180  1.1  riastrad {
    181  1.1  riastrad 
    182  1.1  riastrad 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
    183  1.1  riastrad 		return EINVAL;
    184  1.1  riastrad 	if (ts->tv_sec < 0)
    185  1.1  riastrad 		return ETIMEDOUT;
    186  1.1  riastrad 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
    187  1.1  riastrad 		ts->tv_nsec = tick * 1000;
    188  1.1  riastrad 	return 0;
    189  1.1  riastrad }
    190  1.1  riastrad 
    191  1.1  riastrad /*
    192  1.1  riastrad  * timespecaddok(tsp, usp)
    193  1.1  riastrad  *
    194  1.1  riastrad  *	True if tsp + usp can be computed without overflow, i.e., if it
    195  1.1  riastrad  *	is OK to do timespecadd(tsp, usp, ...).
    196  1.1  riastrad  */
    197  1.1  riastrad bool
    198  1.1  riastrad timespecaddok(const struct timespec *tsp, const struct timespec *usp)
    199  1.1  riastrad {
    200  1.1  riastrad 	enum { TIME_MIN = __type_min(time_t), TIME_MAX = __type_max(time_t) };
    201  1.1  riastrad 	time_t a = tsp->tv_sec;
    202  1.1  riastrad 	time_t b = usp->tv_sec;
    203  1.1  riastrad 	bool carry;
    204  1.1  riastrad 
    205  1.1  riastrad 	/*
    206  1.1  riastrad 	 * Caller is responsible for guaranteeing valid timespec
    207  1.1  riastrad 	 * inputs.  Any user-controlled inputs must be validated or
    208  1.1  riastrad 	 * adjusted.
    209  1.1  riastrad 	 */
    210  1.1  riastrad 	KASSERT(tsp->tv_nsec >= 0);
    211  1.1  riastrad 	KASSERT(usp->tv_nsec >= 0);
    212  1.1  riastrad 	KASSERT(tsp->tv_nsec < 1000000000L);
    213  1.1  riastrad 	KASSERT(usp->tv_nsec < 1000000000L);
    214  1.1  riastrad 	__CTASSERT(1000000000L <= __type_max(long) - 1000000000L);
    215  1.1  riastrad 
    216  1.1  riastrad 	/*
    217  1.1  riastrad 	 * Fail if a + b + carry overflows TIME_MAX, or if a + b
    218  1.1  riastrad 	 * overflows TIME_MIN because timespecadd adds the carry after
    219  1.1  riastrad 	 * computing a + b.
    220  1.1  riastrad 	 *
    221  1.1  riastrad 	 * Break it into two mutually exclusive and exhaustive cases:
    222  1.1  riastrad 	 * I. a >= 0
    223  1.1  riastrad 	 * II. a < 0
    224  1.1  riastrad 	 */
    225  1.1  riastrad 	carry = (tsp->tv_nsec + usp->tv_nsec >= 1000000000L);
    226  1.1  riastrad 	if (a >= 0) {
    227  1.1  riastrad 		/*
    228  1.1  riastrad 		 * Case I: a >= 0.  If b < 0, then b + 1 <= 0, so
    229  1.1  riastrad 		 *
    230  1.1  riastrad 		 *	a + b + 1 <= a + 0 <= TIME_MAX,
    231  1.1  riastrad 		 *
    232  1.1  riastrad 		 * and
    233  1.1  riastrad 		 *
    234  1.1  riastrad 		 *	a + b >= 0 + b = b >= TIME_MIN,
    235  1.1  riastrad 		 *
    236  1.1  riastrad 		 * so this can't overflow.
    237  1.1  riastrad 		 *
    238  1.1  riastrad 		 * If b >= 0, then a + b + carry >= a + b >= 0, so
    239  1.1  riastrad 		 * negative results and thus results below TIME_MIN are
    240  1.1  riastrad 		 * impossible; we need only avoid
    241  1.1  riastrad 		 *
    242  1.1  riastrad 		 *	a + b + carry > TIME_MAX,
    243  1.1  riastrad 		 *
    244  1.1  riastrad 		 * which we will do by rejecting if
    245  1.1  riastrad 		 *
    246  1.1  riastrad 		 *	b > TIME_MAX - a - carry,
    247  1.1  riastrad 		 *
    248  1.1  riastrad 		 * which in turn is incidentally always false if b < 0
    249  1.1  riastrad 		 * so we don't need extra logic to discriminate on the
    250  1.1  riastrad 		 * b >= 0 and b < 0 cases.
    251  1.1  riastrad 		 *
    252  1.1  riastrad 		 * Since 0 <= a <= TIME_MAX, we know
    253  1.1  riastrad 		 *
    254  1.1  riastrad 		 *	0 <= TIME_MAX - a <= TIME_MAX,
    255  1.1  riastrad 		 *
    256  1.1  riastrad 		 * and hence
    257  1.1  riastrad 		 *
    258  1.1  riastrad 		 *	-1 <= TIME_MAX - a - 1 < TIME_MAX.
    259  1.1  riastrad 		 *
    260  1.1  riastrad 		 * So we can compute TIME_MAX - a - carry (i.e., either
    261  1.1  riastrad 		 * TIME_MAX - a or TIME_MAX - a - 1) safely without
    262  1.1  riastrad 		 * overflow.
    263  1.1  riastrad 		 */
    264  1.1  riastrad 		if (b > TIME_MAX - a - carry)
    265  1.1  riastrad 			return false;
    266  1.1  riastrad 	} else {
    267  1.1  riastrad 		/*
    268  1.1  riastrad 		 * Case II: a < 0.  If b >= 0, then since a + 1 <= 0,
    269  1.1  riastrad 		 * we have
    270  1.1  riastrad 		 *
    271  1.1  riastrad 		 *	a + b + 1 <= b <= TIME_MAX,
    272  1.1  riastrad 		 *
    273  1.1  riastrad 		 * and
    274  1.1  riastrad 		 *
    275  1.1  riastrad 		 *	a + b >= a >= TIME_MIN,
    276  1.1  riastrad 		 *
    277  1.1  riastrad 		 * so this can't overflow.
    278  1.1  riastrad 		 *
    279  1.1  riastrad 		 * If b < 0, then the intermediate a + b is negative
    280  1.1  riastrad 		 * and the outcome a + b + 1 is nonpositive, so we need
    281  1.1  riastrad 		 * only avoid
    282  1.1  riastrad 		 *
    283  1.1  riastrad 		 *	a + b < TIME_MIN,
    284  1.1  riastrad 		 *
    285  1.1  riastrad 		 * which we will do by rejecting if
    286  1.1  riastrad 		 *
    287  1.1  riastrad 		 *	a < TIME_MIN - b.
    288  1.1  riastrad 		 *
    289  1.1  riastrad 		 * (Reminder: The carry is added afterward in
    290  1.1  riastrad 		 * timespecadd, so to avoid overflow it is not enough
    291  1.1  riastrad 		 * to merely reject a + b + carry < TIME_MIN.)
    292  1.1  riastrad 		 *
    293  1.1  riastrad 		 * It is safe to compute the difference TIME_MIN - b
    294  1.1  riastrad 		 * because b is negative, so the result lies in
    295  1.1  riastrad 		 * (TIME_MIN, 0].
    296  1.1  riastrad 		 */
    297  1.1  riastrad 		if (b < 0 && a < TIME_MIN - b)
    298  1.1  riastrad 			return false;
    299  1.1  riastrad 	}
    300  1.1  riastrad 
    301  1.1  riastrad 	return true;
    302  1.1  riastrad }
    303  1.1  riastrad 
    304  1.1  riastrad /*
    305  1.1  riastrad  * timespecsubok(tsp, usp)
    306  1.1  riastrad  *
    307  1.1  riastrad  *	True if tsp - usp can be computed without overflow, i.e., if it
    308  1.1  riastrad  *	is OK to do timespecsub(tsp, usp, ...).
    309  1.1  riastrad  */
    310  1.1  riastrad bool
    311  1.1  riastrad timespecsubok(const struct timespec *tsp, const struct timespec *usp)
    312  1.1  riastrad {
    313  1.1  riastrad 	enum { TIME_MIN = __type_min(time_t), TIME_MAX = __type_max(time_t) };
    314  1.1  riastrad 	time_t a = tsp->tv_sec, b = usp->tv_sec;
    315  1.1  riastrad 	bool borrow;
    316  1.1  riastrad 
    317  1.1  riastrad 	/*
    318  1.1  riastrad 	 * Caller is responsible for guaranteeing valid timespec
    319  1.1  riastrad 	 * inputs.  Any user-controlled inputs must be validated or
    320  1.1  riastrad 	 * adjusted.
    321  1.1  riastrad 	 */
    322  1.1  riastrad 	KASSERT(tsp->tv_nsec >= 0);
    323  1.1  riastrad 	KASSERT(usp->tv_nsec >= 0);
    324  1.1  riastrad 	KASSERT(tsp->tv_nsec < 1000000000L);
    325  1.1  riastrad 	KASSERT(usp->tv_nsec < 1000000000L);
    326  1.1  riastrad 	__CTASSERT(1000000000L <= __type_max(long) - 1000000000L);
    327  1.1  riastrad 
    328  1.1  riastrad 	/*
    329  1.1  riastrad 	 * Fail if a - b - borrow overflows TIME_MIN, or if a - b
    330  1.1  riastrad 	 * overflows TIME_MAX because timespecsub subtracts the borrow
    331  1.1  riastrad 	 * after computing a - b.
    332  1.1  riastrad 	 *
    333  1.1  riastrad 	 * Break it into two mutually exclusive and exhaustive cases:
    334  1.1  riastrad 	 * I. a < 0
    335  1.1  riastrad 	 * II. a >= 0
    336  1.1  riastrad 	 */
    337  1.1  riastrad 	borrow = (tsp->tv_nsec - usp->tv_nsec < 0);
    338  1.1  riastrad 	if (a < 0) {
    339  1.1  riastrad 		/*
    340  1.1  riastrad 		 * Case I: a < 0.  If b < 0, then -b - 1 >= 0, so
    341  1.1  riastrad 		 *
    342  1.1  riastrad 		 *	a - b - 1 >= a + 0 >= TIME_MIN,
    343  1.1  riastrad 		 *
    344  1.1  riastrad 		 * and, since a <= -1, provided that TIME_MIN <=
    345  1.1  riastrad 		 * -TIME_MAX - 1 so that TIME_MAX <= -TIME_MIN - 1 (in
    346  1.1  riastrad 		 * fact, equality holds, under the assumption of
    347  1.1  riastrad 		 * two's-complement arithmetic),
    348  1.1  riastrad 		 *
    349  1.1  riastrad 		 *	a - b <= -1 - b = -b - 1 <= TIME_MAX,
    350  1.1  riastrad 		 *
    351  1.1  riastrad 		 * so this can't overflow.
    352  1.1  riastrad 		 */
    353  1.1  riastrad 		__CTASSERT(TIME_MIN <= -TIME_MAX - 1);
    354  1.1  riastrad 
    355  1.1  riastrad 		/*
    356  1.1  riastrad 		 * If b >= 0, then a - b - borrow <= a - b < 0, so
    357  1.1  riastrad 		 * positive results and thus results above TIME_MAX are
    358  1.1  riastrad 		 * impossible; we need only avoid
    359  1.1  riastrad 		 *
    360  1.1  riastrad 		 *	a - b - borrow < TIME_MIN,
    361  1.1  riastrad 		 *
    362  1.1  riastrad 		 * which we will do by rejecting if
    363  1.1  riastrad 		 *
    364  1.1  riastrad 		 *	a < TIME_MIN + b + borrow.
    365  1.1  riastrad 		 *
    366  1.1  riastrad 		 * The right-hand side is safe to evaluate for any
    367  1.1  riastrad 		 * values of b and borrow as long as TIME_MIN +
    368  1.1  riastrad 		 * TIME_MAX + 1 <= TIME_MAX, i.e., TIME_MIN <= -1.
    369  1.1  riastrad 		 * (Note: If time_t were unsigned, this would fail!)
    370  1.1  riastrad 		 *
    371  1.1  riastrad 		 * Note: Unlike Case I in timespecaddok, this criterion
    372  1.1  riastrad 		 * does not work for b < 0, nor can the roles of a and
    373  1.1  riastrad 		 * b in the inequality be reversed (e.g., -b < TIME_MIN
    374  1.1  riastrad 		 * - a + borrow) without extra cases like checking for
    375  1.1  riastrad 		 * b = TEST_MIN.
    376  1.1  riastrad 		 */
    377  1.1  riastrad 		__CTASSERT(TIME_MIN < -1);
    378  1.1  riastrad 		if (b >= 0 && a < TIME_MIN + b + borrow)
    379  1.1  riastrad 			return false;
    380  1.1  riastrad 	} else {
    381  1.1  riastrad 		/*
    382  1.1  riastrad 		 * Case II: a >= 0.  If b >= 0, then
    383  1.1  riastrad 		 *
    384  1.1  riastrad 		 *	a - b <= a <= TIME_MAX,
    385  1.1  riastrad 		 *
    386  1.1  riastrad 		 * and, provided TIME_MIN <= -TIME_MAX - 1 (in fact,
    387  1.1  riastrad 		 * equality holds, under the assumption of
    388  1.1  riastrad 		 * two's-complement arithmetic)
    389  1.1  riastrad 		 *
    390  1.1  riastrad 		 *	a - b - 1 >= -b - 1 >= -TIME_MAX - 1 >= TIME_MIN,
    391  1.1  riastrad 		 *
    392  1.1  riastrad 		 * so this can't overflow.
    393  1.1  riastrad 		 */
    394  1.1  riastrad 		__CTASSERT(TIME_MIN <= -TIME_MAX - 1);
    395  1.1  riastrad 
    396  1.1  riastrad 		/*
    397  1.1  riastrad 		 * If b < 0, then a - b >= a >= 0, so negative results
    398  1.1  riastrad 		 * and thus results below TIME_MIN are impossible; we
    399  1.1  riastrad 		 * need only avoid
    400  1.1  riastrad 		 *
    401  1.1  riastrad 		 *	a - b > TIME_MAX,
    402  1.1  riastrad 		 *
    403  1.1  riastrad 		 * which we will do by rejecting if
    404  1.1  riastrad 		 *
    405  1.1  riastrad 		 *	a > TIME_MAX + b.
    406  1.1  riastrad 		 *
    407  1.1  riastrad 		 * (Reminder: The borrow is subtracted afterward in
    408  1.1  riastrad 		 * timespecsub, so to avoid overflow it is not enough
    409  1.1  riastrad 		 * to merely reject a - b - borrow > TIME_MAX.)
    410  1.1  riastrad 		 *
    411  1.1  riastrad 		 * It is safe to compute the sum TIME_MAX + b because b
    412  1.1  riastrad 		 * is negative, so the result lies in [0, TIME_MAX).
    413  1.1  riastrad 		 */
    414  1.1  riastrad 		if (b < 0 && a > TIME_MAX + b)
    415  1.1  riastrad 			return false;
    416  1.1  riastrad 	}
    417  1.1  riastrad 
    418  1.1  riastrad 	return true;
    419  1.1  riastrad }
    420  1.1  riastrad 
    421  1.1  riastrad /*
    422  1.1  riastrad  * itimer_transition(it, now, next, &overruns)
    423  1.1  riastrad  *
    424  1.1  riastrad  *	Given:
    425  1.1  riastrad  *
    426  1.1  riastrad  *	- it: the current state of an itimer (it_value = last expiry
    427  1.1  riastrad  *	  time, it_interval = periodic rescheduling interval), and
    428  1.1  riastrad  *
    429  1.1  riastrad  *	- now: the current time on the itimer's clock;
    430  1.1  riastrad  *
    431  1.1  riastrad  *	compute:
    432  1.1  riastrad  *
    433  1.1  riastrad  *	- next: the next time the itimer should be scheduled for, and
    434  1.1  riastrad  *	- overruns: the number of overruns if we're firing late.
    435  1.1  riastrad  *
    436  1.1  riastrad  *	XXX This should maybe also say whether the itimer should expire
    437  1.1  riastrad  *	at all.
    438  1.1  riastrad  */
    439  1.1  riastrad void
    440  1.1  riastrad itimer_transition(const struct itimerspec *restrict it,
    441  1.1  riastrad     const struct timespec *restrict now,
    442  1.1  riastrad     struct timespec *restrict next,
    443  1.1  riastrad     int *restrict overrunsp)
    444  1.1  riastrad {
    445  1.1  riastrad 	uint64_t last_val, next_val, interval, now_ns;
    446  1.1  riastrad 	int backwards;
    447  1.1  riastrad 
    448  1.1  riastrad 	/*
    449  1.1  riastrad 	 * Zero the outputs so we can test assertions in userland
    450  1.1  riastrad 	 * without undefined behaviour.
    451  1.1  riastrad 	 */
    452  1.1  riastrad 	timespecclear(next);
    453  1.1  riastrad 	*overrunsp = 0;
    454  1.1  riastrad 
    455  1.1  riastrad 	/*
    456  1.1  riastrad 	 * Paranoia: Caller should guarantee this.
    457  1.1  riastrad 	 */
    458  1.1  riastrad 	if (!timespecisset(&it->it_interval)) {
    459  1.1  riastrad 		timespecclear(next);
    460  1.1  riastrad 		return;
    461  1.1  riastrad 	}
    462  1.1  riastrad 
    463  1.1  riastrad 	backwards = (timespeccmp(&it->it_value, now, >));
    464  1.1  riastrad 
    465  1.1  riastrad 	/* Nonnegative interval guaranteed by itimerfix.  */
    466  1.1  riastrad 	KASSERT(it->it_interval.tv_sec >= 0);
    467  1.1  riastrad 	KASSERT(it->it_interval.tv_nsec >= 0);
    468  1.1  riastrad 
    469  1.1  riastrad 	/* Handle the easy case of non-overflown timers first. */
    470  1.1  riastrad 	if (!backwards &&
    471  1.1  riastrad 	    timespecaddok(&it->it_value, &it->it_interval)) {
    472  1.1  riastrad 		timespecadd(&it->it_value, &it->it_interval,
    473  1.1  riastrad 		    next);
    474  1.1  riastrad 	} else {
    475  1.1  riastrad 		now_ns = timespec2ns(now);
    476  1.1  riastrad 		last_val = timespec2ns(&it->it_value);
    477  1.1  riastrad 		interval = timespec2ns(&it->it_interval);
    478  1.1  riastrad 
    479  1.1  riastrad 		next_val = now_ns +
    480  1.1  riastrad 		    (now_ns - last_val + interval - 1) % interval;
    481  1.1  riastrad 
    482  1.1  riastrad 		if (backwards)
    483  1.1  riastrad 			next_val += interval;
    484  1.1  riastrad 		else
    485  1.1  riastrad 			*overrunsp = (now_ns - last_val) / interval;
    486  1.1  riastrad 
    487  1.1  riastrad 		next->tv_sec = next_val / 1000000000;
    488  1.1  riastrad 		next->tv_nsec = next_val % 1000000000;
    489  1.1  riastrad 	}
    490  1.1  riastrad }
    491