[2/8] timekeeping: optimize ns_to_timespec64

Message ID 20191108203435.112759-3-arnd@arndb.de
State Accepted
Commit 20d087368d38c7350a4519a3b316ef7eb2504692
Headers show
Series
  • Untitled series #24835
Related show

Commit Message

Arnd Bergmann Nov. 8, 2019, 8:34 p.m.
I noticed that ns_to_timespec64() calls div_s64_rem(), which is
a rather slow function on 32-bit architectures, as it cannot take
advantage of the do_div() optimizations for constant arguments.

This open-codes the div_s64_rem() function here, so we can pass
a constant divider into the optimized div_u64_rem() function.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>

---
 kernel/time/time.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

-- 
2.20.0

Comments

Deepa Dinamani Nov. 10, 2019, 8:46 p.m. | #1
The arithmetic looks right to me.

Acked-by: Deepa Dinamani <deepa.kernel@gmail.com>

Patch

diff --git a/kernel/time/time.c b/kernel/time/time.c
index 5c54ca632d08..45a358953f09 100644
--- a/kernel/time/time.c
+++ b/kernel/time/time.c
@@ -550,18 +550,21 @@  EXPORT_SYMBOL(set_normalized_timespec64);
  */
 struct timespec64 ns_to_timespec64(const s64 nsec)
 {
-	struct timespec64 ts;
+	struct timespec64 ts = { 0, 0 };
 	s32 rem;
 
-	if (!nsec)
-		return (struct timespec64) {0, 0};
-
-	ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);
-	if (unlikely(rem < 0)) {
-		ts.tv_sec--;
-		rem += NSEC_PER_SEC;
+	if (likely(nsec > 0)) {
+		ts.tv_sec = div_u64_rem(nsec, NSEC_PER_SEC, &rem);
+		ts.tv_nsec = rem;
+	} else if (nsec < 0) {
+		/*
+		 * With negative times, tv_sec points to the earlier
+		 * second, and tv_nsec counts the nanoseconds since
+		 * then, so tv_nsec is always a positive number.
+		 */
+		ts.tv_sec = -div_u64_rem(-nsec - 1, NSEC_PER_SEC, &rem) - 1;
+		ts.tv_nsec = NSEC_PER_SEC - rem - 1;
 	}
-	ts.tv_nsec = rem;
 
 	return ts;
 }