From: batt Date: Thu, 18 Oct 2007 16:18:14 +0000 (+0000) Subject: Avoid overflow when TIMER_HW_HPTICKS_PER_SEC is big. X-Git-Tag: 1.0.0~332 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=4193143e05964bebf86903e3966875b917771c72;p=bertos.git Avoid overflow when TIMER_HW_HPTICKS_PER_SEC is big. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@910 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/drv/timer.h b/drv/timer.h index 7e2a6288..d5bb7743 100644 --- a/drv/timer.h +++ b/drv/timer.h @@ -158,7 +158,7 @@ INLINE hptime_t us_to_hptime(utime_t us) #if TIMER_HW_HPTICKS_PER_SEC > 10000000UL return us * ((TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL); #else - return (us * TIMER_HW_HPTICKS_PER_SEC + 500000UL) / 1000000UL; + return (us * ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL) + 500) / 1000UL; #endif } @@ -166,9 +166,9 @@ INLINE hptime_t us_to_hptime(utime_t us) INLINE utime_t hptime_to_us(hptime_t hpticks) { #if TIMER_HW_HPTICKS_PER_SEC < 100000UL - return hpticks * (1000000UL / TIMER_HW_HPTICKS_PER_SEC); + return hpticks * ((1000000UL + TIMER_HW_HPTICKS_PER_SEC / 2) / TIMER_HW_HPTICKS_PER_SEC); #else - return (hpticks * 1000000UL) / TIMER_HW_HPTICKS_PER_SEC; + return (hpticks * 1000UL) / ((TIMER_HW_HPTICKS_PER_SEC + 500) / 1000UL); #endif /* TIMER_HW_HPTICKS_PER_SEC < 100000UL */ }