Skip to content

Commit fbe34cc

Browse files
committed
Add benchmark for not-quite-correct “fake SIMD” make_ascii_uppercase
1 parent ce933f7 commit fbe34cc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/libcore/benches/ascii_case.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,52 @@ benches! {
8787
fn bench06_libcore(bytes: &mut [u8]) {
8888
bytes.make_ascii_uppercase()
8989
}
90+
91+
fn bench07_fake_simd_u32(bytes: &mut [u8]) {
92+
let (before, aligned, after) = unsafe {
93+
bytes.align_to_mut::<u32>()
94+
};
95+
for byte in before {
96+
*byte = branchless_to_ascii_upper_case(*byte)
97+
}
98+
for word in aligned {
99+
// FIXME: this is incorrect for some byte values:
100+
// addition within a byte can carry/overflow into the next byte.
101+
// Test case: b"\xFFz "
102+
*word &= !(
103+
(
104+
word.wrapping_add(0x1f1f1f1f) &
105+
!word.wrapping_add(0x05050505) &
106+
0x80808080
107+
) >> 2
108+
)
109+
}
110+
for byte in after {
111+
*byte = branchless_to_ascii_upper_case(*byte)
112+
}
113+
}
114+
115+
fn bench08_fake_simd_u64(bytes: &mut [u8]) {
116+
let (before, aligned, after) = unsafe {
117+
bytes.align_to_mut::<u64>()
118+
};
119+
for byte in before {
120+
*byte = branchless_to_ascii_upper_case(*byte)
121+
}
122+
for word in aligned {
123+
// FIXME: like above, this is incorrect for some byte values.
124+
*word &= !(
125+
(
126+
word.wrapping_add(0x1f1f1f1f_1f1f1f1f) &
127+
!word.wrapping_add(0x05050505_05050505) &
128+
0x80808080_80808080
129+
) >> 2
130+
)
131+
}
132+
for byte in after {
133+
*byte = branchless_to_ascii_upper_case(*byte)
134+
}
135+
}
90136
}
91137

92138
macro_rules! repeat {

0 commit comments

Comments
 (0)