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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// generated source. do not edit.
#![allow(non_upper_case_globals, unused_macros, unused_imports)]
use crate::low::macros::*;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT-0
// ----------------------------------------------------------------------------
// Shift bignum right by c < 64 bits z := floor(x / 2^c)
// Inputs x[n], c; outputs function return (bits shifted out) and z[k]
//
// extern uint64_t bignum_shr_small(uint64_t k, uint64_t *z, uint64_t n,
// const uint64_t *x, uint64_t c);
//
// Does the "z := x >> c" operation where x is n digits, result z is p.
// The shift count c is masked to 6 bits so it actually uses c' = c mod 64.
// The return value is the inout mod 2^c'.
//
// Standard x86-64 ABI: RDI = k, RSI = z, RDX = n, RCX = x, R8 = c, returns RAX
// Microsoft x64 ABI: RCX = k, RDX = z, R8 = n, R9 = x, [RSP+40] = c, returns RAX
// ----------------------------------------------------------------------------
macro_rules! p {
() => {
"rdi"
};
}
macro_rules! z {
() => {
"rsi"
};
}
macro_rules! n {
() => {
"rdx"
};
}
// These get moved from their initial positions
macro_rules! c {
() => {
"rcx"
};
}
macro_rules! x {
() => {
"r9"
};
}
// Other variables
macro_rules! b {
() => {
"rax"
};
}
macro_rules! t {
() => {
"r8"
};
}
macro_rules! a {
() => {
"r10"
};
}
macro_rules! ashort {
() => {
"r10d"
};
}
/// Shift bignum right by c < 64 bits z := floor(x / 2^c)
///
/// Inputs x[n], c; outputs function return (bits shifted out) and z[k]
///
/// Does the "z := x >> c" operation where x is n digits, result z is p.
/// The shift count c is masked to 6 bits so it actually uses c' = c mod 64.
/// The return value is the inout mod 2^c'.
pub(crate) fn bignum_shr_small(z: &mut [u64], x: &[u64], c: u8) {
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
Q!(" endbr64 " ),
// Reshuffle registers to put the shift count into CL
Q!(" mov " x!() ", rcx"),
Q!(" mov " c!() ", r8"),
// Set default carry-in word to 0, useful for other things too
Q!(" xor " b!() ", " b!()),
// First, if p > n then pad output on the left with p-n zeros
Q!(" cmp " n!() ", " p!()),
Q!(" jnc " Label!("bignum_shr_small_nopad", 2, After)),
Q!(Label!("bignum_shr_small_padloop", 3) ":"),
Q!(" dec " p!()),
Q!(" mov " "[" z!() "+ 8 * " p!() "], " b!()),
Q!(" cmp " n!() ", " p!()),
Q!(" jc " Label!("bignum_shr_small_padloop", 3, Before)),
Q!(Label!("bignum_shr_small_nopad", 2) ":"),
// We now know that p <= n. If in fact p < n let carry word = x[p] instead of 0
Q!(" jz " Label!("bignum_shr_small_shiftstart", 4, After)),
Q!(" mov " b!() ", [" x!() "+ 8 * " p!() "]"),
Q!(Label!("bignum_shr_small_shiftstart", 4) ":"),
Q!(" test " p!() ", " p!()),
Q!(" jz " Label!("bignum_shr_small_trivial", 5, After)),
// Now the main loop
Q!(Label!("bignum_shr_small_loop", 6) ":"),
Q!(" mov " a!() ", [" x!() "+ 8 * " p!() "-8]"),
Q!(" mov " t!() ", " a!()),
Q!(" shrd " a!() ", " b!() ", cl"),
Q!(" mov " "[" z!() "+ 8 * " p!() "-8], " a!()),
Q!(" mov " b!() ", " t!()),
Q!(" dec " p!()),
Q!(" jnz " Label!("bignum_shr_small_loop", 6, Before)),
// Mask the carry word and return with that as RAX = b
Q!(Label!("bignum_shr_small_trivial", 5) ":"),
Q!(" mov " ashort!() ", 1"),
Q!(" shl " a!() ", cl"),
Q!(" dec " a!()),
Q!(" and " b!() ", " a!()),
Q!(Label!("bignum_shr_small_end", 7) ":"),
inout("rdi") z.len() => _,
inout("rsi") z.as_ptr() => _,
inout("rdx") x.len() => _,
inout("rcx") x.as_ptr() => _,
inout("r8") (c as u64) => _,
// clobbers
out("r10") _,
out("r9") _,
out("rax") _,
)
};
}