aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/go-patches/0004-runtime-use-correct-constant-when-computing-nsec-rem.patch
blob: 4a826068c3698a25405187a2623818638a234685 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Fri, 5 Nov 2021 00:10:31 +0100
Subject: [PATCH] runtime: use correct constant when computing nsec remainder

A code comment on amd64 for windows and plan9 contained a snippet for
splitting apart the sec and nsec components of a unix timestamp, with
produced assembly below, which was then cleaned up by hand. When arm64
was ported, that code snippet in the comment went through the compiler
to produce some code that was then pasted and cleaned up. Unfortunately,
the comment had a typo in it, containing 8 zeros instead of 9.

This resulted in the constant used in the assembly being wrong, spotted
by @bufflig's eagle eyes. So, this commit fixes the comment on all three
platforms, and the assembly on windows/arm64.

Fixes #48072.

Change-Id: I786fe89147328b0d25544f52c927ddfdb9f6f1cf
Reviewed-on: https://go-review.googlesource.com/c/go/+/361474
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Patrik Nyblom <pnyb@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
---
 src/runtime/sys_plan9_amd64.s    | 2 +-
 src/runtime/time_windows_amd64.s | 2 +-
 src/runtime/time_windows_arm64.s | 9 +++------
 3 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/runtime/sys_plan9_amd64.s b/src/runtime/sys_plan9_amd64.s
index 731306ab44..068200b7f3 100644
--- a/src/runtime/sys_plan9_amd64.s
+++ b/src/runtime/sys_plan9_amd64.s
@@ -94,7 +94,7 @@ TEXT runtime·walltime(SB),NOSPLIT,$8-12
 	MOVQ	0(SP), AX
 
 	// generated code for
-	//	func f(x uint64) (uint64, uint64) { return x/1000000000, x%100000000 }
+	//	func f(x uint64) (uint64, uint64) { return x/1000000000, x%1000000000 }
 	// adapted to reduce duplication
 	MOVQ	AX, CX
 	MOVQ	$1360296554856532783, AX
diff --git a/src/runtime/time_windows_amd64.s b/src/runtime/time_windows_amd64.s
index 045f64eb46..70f6a008cd 100644
--- a/src/runtime/time_windows_amd64.s
+++ b/src/runtime/time_windows_amd64.s
@@ -25,7 +25,7 @@ TEXT time·now(SB),NOSPLIT,$0-24
 	IMULQ	$100, AX
 
 	// generated code for
-	//	func f(x uint64) (uint64, uint64) { return x/1000000000, x%100000000 }
+	//	func f(x uint64) (uint64, uint64) { return x/1000000000, x%1000000000 }
 	// adapted to reduce duplication
 	MOVQ	AX, CX
 	MOVQ	$1360296554856532783, AX
diff --git a/src/runtime/time_windows_arm64.s b/src/runtime/time_windows_arm64.s
index e8a0eb2f93..ef5b848473 100644
--- a/src/runtime/time_windows_arm64.s
+++ b/src/runtime/time_windows_arm64.s
@@ -32,17 +32,14 @@ TEXT time·now(SB),NOSPLIT|NOFRAME,$0-24
 	// Code stolen from compiler output for:
 	//
 	//	var x uint64
-	//	func f() (sec uint64, nsec uint32) { return x / 1000000000, uint32(x % 100000000) }
+	//	func f() (sec uint64, nsec uint32) { return x / 1000000000, uint32(x % 1000000000) }
 	//
 	LSR	$1, R0, R1
 	MOVD	$-8543223759426509416, R2
-	UMULH	R2, R1, R1
+	UMULH	R1, R2, R1
 	LSR	$28, R1, R1
 	MOVD	R1, sec+0(FP)
-	MOVD	$-6067343680855748867, R1
-	UMULH	R0, R1, R1
-	LSR	$26, R1, R1
-	MOVD	$100000000, R2
+	MOVD	$1000000000, R2
 	MSUB	R1, R0, R2, R0
 	MOVW	R0, nsec+8(FP)
 	RET