1
- #[ allow( unused) ]
2
1
use core:: arch:: asm;
3
2
4
- #[ allow( unused) ]
5
- macro_rules! constify_imm2 {
6
- ( $imm2: expr, $expand: ident) => {
7
- #[ allow( overflowing_literals) ]
8
- match $imm2 & 0b11 {
9
- 0b00 => $expand!( 0 ) ,
10
- 0b01 => $expand!( 1 ) ,
11
- 0b10 => $expand!( 2 ) ,
12
- _ => $expand!( 3 ) ,
13
- }
3
+ macro_rules! static_assert_imm2 {
4
+ ( $imm: ident) => {
5
+ static_assert!(
6
+ $imm < 4 ,
7
+ "Immediate value allowed to be a constant from 0 up to including 3"
8
+ )
14
9
} ;
15
10
}
16
11
12
+ extern "unadjusted" {
13
+ #[ link_name = "llvm.riscv.aes32esi" ]
14
+ fn _aes32esi ( rs1 : i32 , rs2 : i32 , bs : i32 ) -> i32 ;
15
+
16
+ #[ link_name = "llvm.riscv.aes32esmi" ]
17
+ fn _aes32esmi ( rs1 : i32 , rs2 : i32 , bs : i32 ) -> i32 ;
18
+
19
+ #[ link_name = "llvm.riscv.aes32dsi" ]
20
+ fn _aes32dsi ( rs1 : i32 , rs2 : i32 , bs : i32 ) -> i32 ;
21
+
22
+ #[ link_name = "llvm.riscv.aes32dsmi" ]
23
+ fn _aes32dsmi ( rs1 : i32 , rs2 : i32 , bs : i32 ) -> i32 ;
24
+ }
25
+
17
26
/// AES final round encryption instruction for RV32.
18
27
///
19
28
/// This instruction sources a single byte from rs2 according to bs. To this it applies the
@@ -29,32 +38,20 @@ macro_rules! constify_imm2 {
29
38
///
30
39
/// # Note
31
40
///
32
- /// The `bs ` parameter is expected to be a constant value and only the bottom 2 bits of `bs` are
41
+ /// The `BS ` parameter is expected to be a constant value and only the bottom 2 bits of `bs` are
33
42
/// used.
34
43
///
35
44
/// # Safety
36
45
///
37
46
/// This function is safe to use if the `zkne` target feature is present.
38
47
#[ target_feature( enable = "zkne" ) ]
48
+ #[ rustc_legacy_const_generics( 2 ) ]
39
49
#[ cfg_attr( test, assert_instr( aes32esi) ) ]
40
50
#[ inline]
41
- pub unsafe fn aes32esi ( rs1 : u32 , rs2 : u32 , bs : u8 ) -> u32 {
42
- macro_rules! aes32esi {
43
- ( $imm2: expr) => { {
44
- let value: u32 ;
45
- unsafe {
46
- asm!(
47
- concat!( "aes32esi {rd},{rs1},{rs2}," , $imm2) ,
48
- rd = lateout( reg) value,
49
- rs1 = in( reg) rs1,
50
- rs2 = in( reg) rs2,
51
- options( pure, nomem, nostack) ,
52
- ) ;
53
- }
54
- value
55
- } }
56
- }
57
- constify_imm2 ! ( bs, aes32esi)
51
+ pub unsafe fn aes32esi < const BS : u8 > ( rs1 : u32 , rs2 : u32 ) -> u32 {
52
+ static_assert_imm2 ! ( BS ) ;
53
+
54
+ _aes32esi ( rs1 as i32 , rs2 as i32 , BS as i32 ) as u32
58
55
}
59
56
60
57
/// AES middle round encryption instruction for RV32 with.
@@ -79,25 +76,13 @@ pub unsafe fn aes32esi(rs1: u32, rs2: u32, bs: u8) -> u32 {
79
76
///
80
77
/// This function is safe to use if the `zkne` target feature is present.
81
78
#[ target_feature( enable = "zkne" ) ]
79
+ #[ rustc_legacy_const_generics( 2 ) ]
82
80
#[ cfg_attr( test, assert_instr( aes32esmi) ) ]
83
81
#[ inline]
84
- pub unsafe fn aes32esmi ( rs1 : u32 , rs2 : u32 , bs : u8 ) -> u32 {
85
- macro_rules! aes32esmi {
86
- ( $imm2: expr) => { {
87
- let value: u32 ;
88
- unsafe {
89
- asm!(
90
- concat!( "aes32esmi {rd},{rs1},{rs2}," , $imm2) ,
91
- rd = lateout( reg) value,
92
- rs1 = in( reg) rs1,
93
- rs2 = in( reg) rs2,
94
- options( pure, nomem, nostack) ,
95
- ) ;
96
- }
97
- value
98
- } }
99
- }
100
- constify_imm2 ! ( bs, aes32esmi)
82
+ pub unsafe fn aes32esmi < const BS : u8 > ( rs1 : u32 , rs2 : u32 ) -> u32 {
83
+ static_assert_imm2 ! ( BS ) ;
84
+
85
+ _aes32esmi ( rs1 as i32 , rs2 as i32 , BS as i32 ) as u32
101
86
}
102
87
103
88
/// AES final round decryption instruction for RV32.
@@ -114,32 +99,20 @@ pub unsafe fn aes32esmi(rs1: u32, rs2: u32, bs: u8) -> u32 {
114
99
///
115
100
/// # Note
116
101
///
117
- /// The `bs ` parameter is expected to be a constant value and only the bottom 2 bits of `bs` are
102
+ /// The `BS ` parameter is expected to be a constant value and only the bottom 2 bits of `bs` are
118
103
/// used.
119
104
///
120
105
/// # Safety
121
106
///
122
107
/// This function is safe to use if the `zknd` target feature is present.
123
108
#[ target_feature( enable = "zknd" ) ]
109
+ #[ rustc_legacy_const_generics( 2 ) ]
124
110
#[ cfg_attr( test, assert_instr( aes32dsi) ) ]
125
111
#[ inline]
126
- pub unsafe fn aes32dsi ( rs1 : u32 , rs2 : u32 , bs : u8 ) -> u32 {
127
- macro_rules! aes32dsi {
128
- ( $imm2: expr) => { {
129
- let value: u32 ;
130
- unsafe {
131
- asm!(
132
- concat!( "aes32dsi {rd},{rs1},{rs2}," , $imm2) ,
133
- rd = lateout( reg) value,
134
- rs1 = in( reg) rs1,
135
- rs2 = in( reg) rs2,
136
- options( pure, nomem, nostack) ,
137
- ) ;
138
- }
139
- value
140
- } }
141
- }
142
- constify_imm2 ! ( bs, aes32dsi)
112
+ pub unsafe fn aes32dsi < const BS : u8 > ( rs1 : u32 , rs2 : u32 ) -> u32 {
113
+ static_assert_imm2 ! ( BS ) ;
114
+
115
+ _aes32dsi ( rs1 as i32 , rs2 as i32 , BS as i32 ) as u32
143
116
}
144
117
145
118
/// AES middle round decryption instruction for RV32.
@@ -157,32 +130,20 @@ pub unsafe fn aes32dsi(rs1: u32, rs2: u32, bs: u8) -> u32 {
157
130
///
158
131
/// # Note
159
132
///
160
- /// The `bs ` parameter is expected to be a constant value and only the bottom 2 bits of `bs` are
133
+ /// The `BS ` parameter is expected to be a constant value and only the bottom 2 bits of `bs` are
161
134
/// used.
162
135
///
163
136
/// # Safety
164
137
///
165
138
/// This function is safe to use if the `zknd` target feature is present.
166
139
#[ target_feature( enable = "zknd" ) ]
140
+ #[ rustc_legacy_const_generics( 2 ) ]
167
141
#[ cfg_attr( test, assert_instr( aes32dsmi) ) ]
168
142
#[ inline]
169
- pub unsafe fn aes32dsmi ( rs1 : u32 , rs2 : u32 , bs : u8 ) -> u32 {
170
- macro_rules! aes32dsmi {
171
- ( $imm2: expr) => { {
172
- let value: u32 ;
173
- unsafe {
174
- asm!(
175
- concat!( "aes32dsmi {rd},{rs1},{rs2}," , $imm2) ,
176
- rd = lateout( reg) value,
177
- rs1 = in( reg) rs1,
178
- rs2 = in( reg) rs2,
179
- options( pure, nomem, nostack) ,
180
- ) ;
181
- }
182
- value
183
- } }
184
- }
185
- constify_imm2 ! ( bs, aes32dsmi)
143
+ pub unsafe fn aes32dsmi < const BS : u8 > ( rs1 : u32 , rs2 : u32 ) -> u32 {
144
+ static_assert_imm2 ! ( BS ) ;
145
+
146
+ _aes32dsmi ( rs1 as i32 , rs2 as i32 , BS as i32 ) as u32
186
147
}
187
148
188
149
/// Place upper/lower halves of the source register into odd/even bits of the destination
0 commit comments