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
// 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
// ----------------------------------------------------------------------------
// Compare bignums, x < y
// Inputs x[m], y[n]; output function return
//
// extern uint64_t bignum_lt(uint64_t m, const uint64_t *x, uint64_t n,
// const uint64_t *y);
//
// Standard ARM ABI: X0 = m, X1 = x, X2 = n, X3 = y, returns X0
// ----------------------------------------------------------------------------
macro_rules! m {
() => {
"x0"
};
}
macro_rules! x {
() => {
"x1"
};
}
macro_rules! n {
() => {
"x2"
};
}
macro_rules! y {
() => {
"x3"
};
}
macro_rules! i {
() => {
"x4"
};
}
macro_rules! a {
() => {
"x5"
};
}
macro_rules! d {
() => {
"x6"
};
}
/// Compare bignums, x < y
///
/// Inputs x[m], y[n]; output function return
pub(crate) fn bignum_cmp_lt(x: &[u64], y: &[u64]) -> u64 {
let ret: u64;
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
// Zero the main index counter for both branches
Q!(" mov " i!() ", xzr"),
// Speculatively form m := m - n and do case split
Q!(" subs " m!() ", " m!() ", " n!()),
Q!(" bcc " Label!("bignum_lt_ylonger", 2, After)),
// The case where x is longer or of the same size (m >= n)
// Note that CF=1 initially by the fact that we reach this point
Q!(" cbz " n!() ", " Label!("bignum_lt_xtest", 3, After)),
Q!(Label!("bignum_lt_xmainloop", 4) ":"),
Q!(" ldr " a!() ", [" x!() ", " i!() ", lsl #3]"),
Q!(" ldr " d!() ", [" y!() ", " i!() ", lsl #3]"),
Q!(" sbcs " "xzr, " a!() ", " d!()),
Q!(" add " i!() ", " i!() ", #1"),
Q!(" sub " n!() ", " n!() ", #1"),
Q!(" cbnz " n!() ", " Label!("bignum_lt_xmainloop", 4, Before)),
Q!(Label!("bignum_lt_xtest", 3) ":"),
Q!(" cbz " m!() ", " Label!("bignum_lt_xskip", 5, After)),
Q!(Label!("bignum_lt_xtoploop", 6) ":"),
Q!(" ldr " a!() ", [" x!() ", " i!() ", lsl #3]"),
Q!(" sbcs " "xzr, " a!() ", xzr"),
Q!(" add " i!() ", " i!() ", #1"),
Q!(" sub " m!() ", " m!() ", #1"),
Q!(" cbnz " m!() ", " Label!("bignum_lt_xtoploop", 6, Before)),
Q!(Label!("bignum_lt_xskip", 5) ":"),
Q!(" cset " "x0, cc"),
// linear hoisting in -> ret after bignum_lt_ytoploop
Q!(" b " Label!("hoist_finish", 7, After)),
// The case where y is longer (n > m)
// The first "adds" also makes sure CF=1 initially in this branch
Q!(Label!("bignum_lt_ylonger", 2) ":"),
Q!(" adds " m!() ", " m!() ", " n!()),
Q!(" cbz " m!() ", " Label!("bignum_lt_ytoploop", 8, After)),
Q!(" sub " n!() ", " n!() ", " m!()),
Q!(Label!("bignum_lt_ymainloop", 9) ":"),
Q!(" ldr " a!() ", [" x!() ", " i!() ", lsl #3]"),
Q!(" ldr " d!() ", [" y!() ", " i!() ", lsl #3]"),
Q!(" sbcs " "xzr, " a!() ", " d!()),
Q!(" add " i!() ", " i!() ", #1"),
Q!(" sub " m!() ", " m!() ", #1"),
Q!(" cbnz " m!() ", " Label!("bignum_lt_ymainloop", 9, Before)),
Q!(Label!("bignum_lt_ytoploop", 8) ":"),
Q!(" ldr " a!() ", [" y!() ", " i!() ", lsl #3]"),
Q!(" sbcs " "xzr, xzr, " a!()),
Q!(" add " i!() ", " i!() ", #1"),
Q!(" sub " n!() ", " n!() ", #1"),
Q!(" cbnz " n!() ", " Label!("bignum_lt_ytoploop", 8, Before)),
Q!(" cset " "x0, cc"),
Q!(Label!("hoist_finish", 7) ":"),
inout("x0") x.len() => ret,
inout("x1") x.as_ptr() => _,
inout("x2") y.len() => _,
inout("x3") y.as_ptr() => _,
// clobbers
out("x4") _,
out("x5") _,
out("x6") _,
)
};
ret
}