time.c (336879b1da97fffc097f77c6d6f818660f2826f0) time.c (d78c9300c51d6ceed9f6d078d4e9366f259de28c)
1/*
2 * linux/kernel/time.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * This file contains the interface functions for the various
7 * time related system calls: time, stime, gettimeofday, settimeofday,
8 * adjtime

--- 545 unchanged lines hidden (view full) ---

554}
555EXPORT_SYMBOL(usecs_to_jiffies);
556
557/*
558 * The TICK_NSEC - 1 rounds up the value to the next resolution. Note
559 * that a remainder subtract here would not do the right thing as the
560 * resolution values don't fall on second boundries. I.e. the line:
561 * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
1/*
2 * linux/kernel/time.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * This file contains the interface functions for the various
7 * time related system calls: time, stime, gettimeofday, settimeofday,
8 * adjtime

--- 545 unchanged lines hidden (view full) ---

554}
555EXPORT_SYMBOL(usecs_to_jiffies);
556
557/*
558 * The TICK_NSEC - 1 rounds up the value to the next resolution. Note
559 * that a remainder subtract here would not do the right thing as the
560 * resolution values don't fall on second boundries. I.e. the line:
561 * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
562 * Note that due to the small error in the multiplier here, this
563 * rounding is incorrect for sufficiently large values of tv_nsec, but
564 * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're
565 * OK.
562 *
563 * Rather, we just shift the bits off the right.
564 *
565 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
566 * value to a scaled second value.
567 */
566 *
567 * Rather, we just shift the bits off the right.
568 *
569 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
570 * value to a scaled second value.
571 */
568unsigned long
569timespec_to_jiffies(const struct timespec *value)
572static unsigned long
573__timespec_to_jiffies(unsigned long sec, long nsec)
570{
574{
571 unsigned long sec = value->tv_sec;
572 long nsec = value->tv_nsec + TICK_NSEC - 1;
575 nsec = nsec + TICK_NSEC - 1;
573
574 if (sec >= MAX_SEC_IN_JIFFIES){
575 sec = MAX_SEC_IN_JIFFIES;
576 nsec = 0;
577 }
578 return (((u64)sec * SEC_CONVERSION) +
579 (((u64)nsec * NSEC_CONVERSION) >>
580 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
581
582}
576
577 if (sec >= MAX_SEC_IN_JIFFIES){
578 sec = MAX_SEC_IN_JIFFIES;
579 nsec = 0;
580 }
581 return (((u64)sec * SEC_CONVERSION) +
582 (((u64)nsec * NSEC_CONVERSION) >>
583 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
584
585}
586
587unsigned long
588timespec_to_jiffies(const struct timespec *value)
589{
590 return __timespec_to_jiffies(value->tv_sec, value->tv_nsec);
591}
592
583EXPORT_SYMBOL(timespec_to_jiffies);
584
585void
586jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
587{
588 /*
589 * Convert jiffies to nanoseconds and separate with
590 * one divide.
591 */
592 u32 rem;
593 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
594 NSEC_PER_SEC, &rem);
595 value->tv_nsec = rem;
596}
597EXPORT_SYMBOL(jiffies_to_timespec);
598
593EXPORT_SYMBOL(timespec_to_jiffies);
594
595void
596jiffies_to_timespec(const unsigned long jiffies, struct timespec *value)
597{
598 /*
599 * Convert jiffies to nanoseconds and separate with
600 * one divide.
601 */
602 u32 rem;
603 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC,
604 NSEC_PER_SEC, &rem);
605 value->tv_nsec = rem;
606}
607EXPORT_SYMBOL(jiffies_to_timespec);
608
599/* Same for "timeval"
609/*
610 * We could use a similar algorithm to timespec_to_jiffies (with a
611 * different multiplier for usec instead of nsec). But this has a
612 * problem with rounding: we can't exactly add TICK_NSEC - 1 to the
613 * usec value, since it's not necessarily integral.
600 *
614 *
601 * Well, almost. The problem here is that the real system resolution is
602 * in nanoseconds and the value being converted is in micro seconds.
603 * Also for some machines (those that use HZ = 1024, in-particular),
604 * there is a LARGE error in the tick size in microseconds.
605
606 * The solution we use is to do the rounding AFTER we convert the
607 * microsecond part. Thus the USEC_ROUND, the bits to be shifted off.
608 * Instruction wise, this should cost only an additional add with carry
609 * instruction above the way it was done above.
615 * We could instead round in the intermediate scaled representation
616 * (i.e. in units of 1/2^(large scale) jiffies) but that's also
617 * perilous: the scaling introduces a small positive error, which
618 * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1
619 * units to the intermediate before shifting) leads to accidental
620 * overflow and overestimates.
621 *
622 * At the cost of one additional multiplication by a constant, just
623 * use the timespec implementation.
610 */
611unsigned long
612timeval_to_jiffies(const struct timeval *value)
613{
624 */
625unsigned long
626timeval_to_jiffies(const struct timeval *value)
627{
614 unsigned long sec = value->tv_sec;
615 long usec = value->tv_usec;
616
617 if (sec >= MAX_SEC_IN_JIFFIES){
618 sec = MAX_SEC_IN_JIFFIES;
619 usec = 0;
620 }
621 return (((u64)sec * SEC_CONVERSION) +
622 (((u64)usec * USEC_CONVERSION + USEC_ROUND) >>
623 (USEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
628 return __timespec_to_jiffies(value->tv_sec,
629 value->tv_usec * NSEC_PER_USEC);
624}
625EXPORT_SYMBOL(timeval_to_jiffies);
626
627void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
628{
629 /*
630 * Convert jiffies to nanoseconds and separate with
631 * one divide.

--- 147 unchanged lines hidden ---
630}
631EXPORT_SYMBOL(timeval_to_jiffies);
632
633void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value)
634{
635 /*
636 * Convert jiffies to nanoseconds and separate with
637 * one divide.

--- 147 unchanged lines hidden ---