Avoid overflow when TIMER_HW_HPTICKS_PER_SEC is big.
authorbatt <batt@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 18 Oct 2007 16:18:14 +0000 (16:18 +0000)
committerbatt <batt@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 18 Oct 2007 16:18:14 +0000 (16:18 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@910 38d2e660-2303-0410-9eaa-f027e97ec537

drv/timer.h

index 7e2a628871f7a069f85d9f29a8095dc4244d6781..d5bb77437a2fda0c98c578a857057585fa5307c7 100644 (file)
@@ -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 */
 }