This is an automated email from the ASF dual-hosted git repository.

archer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 512a496467 riscv_mtimer: modify riscv_mtimer_current to reduce 
precision lost
512a496467 is described below

commit 512a4964672bc5bd710494f1b8d6eea70f8394ef
Author: liaoao <lia...@xiaomi.com>
AuthorDate: Thu Mar 7 16:07:25 2024 +0800

    riscv_mtimer: modify riscv_mtimer_current to reduce precision lost
    
    Signed-off-by: liaoao <lia...@xiaomi.com>
---
 arch/risc-v/src/common/riscv_mtimer.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_mtimer.c 
b/arch/risc-v/src/common/riscv_mtimer.c
index b8e3f4600f..1b4df41ce7 100644
--- a/arch/risc-v/src/common/riscv_mtimer.c
+++ b/arch/risc-v/src/common/riscv_mtimer.c
@@ -319,10 +319,11 @@ static int riscv_mtimer_current(struct 
oneshot_lowerhalf_s *lower,
   struct riscv_mtimer_lowerhalf_s *priv =
     (struct riscv_mtimer_lowerhalf_s *)lower;
   uint64_t mtime = riscv_mtimer_get_mtime(priv);
-  uint64_t nsec = mtime / (priv->freq / USEC_PER_SEC) * NSEC_PER_USEC;
+  uint64_t left;
 
-  ts->tv_sec  = nsec / NSEC_PER_SEC;
-  ts->tv_nsec = nsec % NSEC_PER_SEC;
+  ts->tv_sec  = mtime / priv->freq;
+  left        = mtime - ts->tv_sec * priv->freq;
+  ts->tv_nsec = NSEC_PER_SEC * left / priv->freq;
 
   return 0;
 }

Reply via email to