> On 16 Jun 2021, at 01:10, Scott Cheloha <scottchel...@gmail.com> wrote:
> 
> Hi,
> 
> dlg@ just moved nsecuptime() and getnsecuptime() into kern_tc.c.
> 
> To tidy it up I'd like to refactor the fraction-to-nanosecond and
> bintime-to-nanosecond conversions into new functions so we only need
> to write them once.

yes please. every time i copied that code i thought i could do it better and 
wasted time trying. having the macro wrap it up will take that temptation away 
i think.

> 
> ok?

ok.

> 
> Index: sys/time.h
> ===================================================================
> RCS file: /cvs/src/sys/sys/time.h,v
> retrieving revision 1.60
> diff -u -p -r1.60 time.h
> --- sys/time.h        15 Jun 2021 05:24:47 -0000      1.60
> +++ sys/time.h        15 Jun 2021 13:10:01 -0000
> @@ -222,11 +222,17 @@ bintimesub(const struct bintime *bt, con
>  *   time_second ticks after N.999999999 not after N.4999999999
>  */
> 
> +static inline uint32_t
> +FRAC_TO_NSEC(uint64_t frac)
> +{
> +     return ((frac >> 32) * 1000000000ULL) >> 32;
> +}
> +
> static inline void
> BINTIME_TO_TIMESPEC(const struct bintime *bt, struct timespec *ts)
> {
>       ts->tv_sec = bt->sec;
> -     ts->tv_nsec = (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 
> 32)) >> 32);
> +     ts->tv_nsec = FRAC_TO_NSEC(bt->frac);
> }
> 
> static inline void
> @@ -250,6 +256,12 @@ TIMEVAL_TO_BINTIME(const struct timeval 
>       bt->sec = (time_t)tv->tv_sec;
>       /* 18446744073709 = int(2^64 / 1000000) */
>       bt->frac = (uint64_t)tv->tv_usec * (uint64_t)18446744073709ULL;
> +}
> +
> +static inline uint64_t
> +BINTIME_TO_NSEC(const struct bintime *bt)
> +{
> +     return bt->sec * 1000000000ULL + FRAC_TO_NSEC(bt->frac);
> }
> #endif
> 
> Index: kern/kern_tc.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/kern_tc.c,v
> retrieving revision 1.73
> diff -u -p -r1.73 kern_tc.c
> --- kern/kern_tc.c    15 Jun 2021 05:24:46 -0000      1.73
> +++ kern/kern_tc.c    15 Jun 2021 13:10:02 -0000
> @@ -254,28 +254,18 @@ uint64_t
> nsecuptime(void)
> {
>       struct bintime bt;
> -     uint64_t nsec;
> 
>       binuptime(&bt);
> -
> -     nsec = (1000000000ULL * (bt.frac >> 32)) >> 32;
> -     nsec += bt.sec * 1000000000ULL;
> -
> -     return (nsec);
> +     return BINTIME_TO_NSEC(&bt);
> }
> 
> uint64_t
> getnsecuptime(void)
> {
>       struct bintime bt;
> -     uint64_t nsec;
> 
>       getbinuptime(&bt);
> -
> -     nsec = (1000000000ULL * (bt.frac >> 32)) >> 32;
> -     nsec += bt.sec * 1000000000ULL;
> -
> -     return (nsec);
> +     return BINTIME_TO_NSEC(&bt);
> }
> 
> void

Reply via email to