time.c (57e04eeda515ee979fec3bc3d64c408feae18acc) | time.c (751addac78b6f205ffd47c8736ca6d429dc77703) |
---|---|
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Copyright (C) 1991, 1992 Linus Torvalds 4 * 5 * This file contains the interface functions for the various time related 6 * system calls: time, stime, gettimeofday, settimeofday, adjtime 7 * 8 * Modification history: --- 612 unchanged lines hidden (view full) --- 621 * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're 622 * OK. 623 * 624 * Rather, we just shift the bits off the right. 625 * 626 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec 627 * value to a scaled second value. 628 */ | 1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Copyright (C) 1991, 1992 Linus Torvalds 4 * 5 * This file contains the interface functions for the various time related 6 * system calls: time, stime, gettimeofday, settimeofday, adjtime 7 * 8 * Modification history: --- 612 unchanged lines hidden (view full) --- 621 * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're 622 * OK. 623 * 624 * Rather, we just shift the bits off the right. 625 * 626 * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec 627 * value to a scaled second value. 628 */ |
629static unsigned long 630__timespec64_to_jiffies(u64 sec, long nsec) | 629 630unsigned long 631timespec64_to_jiffies(const struct timespec64 *value) |
631{ | 632{ |
632 nsec = nsec + TICK_NSEC - 1; | 633 u64 sec = value->tv_sec; 634 long nsec = value->tv_nsec + TICK_NSEC - 1; |
633 634 if (sec >= MAX_SEC_IN_JIFFIES){ 635 sec = MAX_SEC_IN_JIFFIES; 636 nsec = 0; 637 } 638 return ((sec * SEC_CONVERSION) + 639 (((u64)nsec * NSEC_CONVERSION) >> 640 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; 641 642} | 635 636 if (sec >= MAX_SEC_IN_JIFFIES){ 637 sec = MAX_SEC_IN_JIFFIES; 638 nsec = 0; 639 } 640 return ((sec * SEC_CONVERSION) + 641 (((u64)nsec * NSEC_CONVERSION) >> 642 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC; 643 644} |
643 644static unsigned long 645__timespec_to_jiffies(unsigned long sec, long nsec) 646{ 647 return __timespec64_to_jiffies((u64)sec, nsec); 648} 649 650unsigned long 651timespec64_to_jiffies(const struct timespec64 *value) 652{ 653 return __timespec64_to_jiffies(value->tv_sec, value->tv_nsec); 654} | |
655EXPORT_SYMBOL(timespec64_to_jiffies); 656 657void 658jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) 659{ 660 /* 661 * Convert jiffies to nanoseconds and separate with 662 * one divide. 663 */ 664 u32 rem; 665 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 666 NSEC_PER_SEC, &rem); 667 value->tv_nsec = rem; 668} 669EXPORT_SYMBOL(jiffies_to_timespec64); 670 671/* | 645EXPORT_SYMBOL(timespec64_to_jiffies); 646 647void 648jiffies_to_timespec64(const unsigned long jiffies, struct timespec64 *value) 649{ 650 /* 651 * Convert jiffies to nanoseconds and separate with 652 * one divide. 653 */ 654 u32 rem; 655 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 656 NSEC_PER_SEC, &rem); 657 value->tv_nsec = rem; 658} 659EXPORT_SYMBOL(jiffies_to_timespec64); 660 661/* |
672 * We could use a similar algorithm to timespec_to_jiffies (with a 673 * different multiplier for usec instead of nsec). But this has a 674 * problem with rounding: we can't exactly add TICK_NSEC - 1 to the 675 * usec value, since it's not necessarily integral. 676 * 677 * We could instead round in the intermediate scaled representation 678 * (i.e. in units of 1/2^(large scale) jiffies) but that's also 679 * perilous: the scaling introduces a small positive error, which 680 * combined with a division-rounding-upward (i.e. adding 2^(scale) - 1 681 * units to the intermediate before shifting) leads to accidental 682 * overflow and overestimates. 683 * 684 * At the cost of one additional multiplication by a constant, just 685 * use the timespec implementation. 686 */ 687unsigned long 688timeval_to_jiffies(const struct timeval *value) 689{ 690 return __timespec_to_jiffies(value->tv_sec, 691 value->tv_usec * NSEC_PER_USEC); 692} 693EXPORT_SYMBOL(timeval_to_jiffies); 694 695void jiffies_to_timeval(const unsigned long jiffies, struct timeval *value) 696{ 697 /* 698 * Convert jiffies to nanoseconds and separate with 699 * one divide. 700 */ 701 u32 rem; 702 703 value->tv_sec = div_u64_rem((u64)jiffies * TICK_NSEC, 704 NSEC_PER_SEC, &rem); 705 value->tv_usec = rem / NSEC_PER_USEC; 706} 707EXPORT_SYMBOL(jiffies_to_timeval); 708 709/* | |
710 * Convert jiffies/jiffies_64 to clock_t and back. 711 */ 712clock_t jiffies_to_clock_t(unsigned long x) 713{ 714#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 715# if HZ < USER_HZ 716 return x * (USER_HZ / HZ); 717# else --- 283 unchanged lines hidden --- | 662 * Convert jiffies/jiffies_64 to clock_t and back. 663 */ 664clock_t jiffies_to_clock_t(unsigned long x) 665{ 666#if (TICK_NSEC % (NSEC_PER_SEC / USER_HZ)) == 0 667# if HZ < USER_HZ 668 return x * (USER_HZ / HZ); 669# else --- 283 unchanged lines hidden --- |