Skip to content

Commit 995221d

Browse files
authored
Merge pull request #2148 from folkertdev/loongarch64-crc32-zero-extend
use zero-extension in the loongarch64 crc32 implementation
2 parents 05ced9d + 6c45d7e commit 995221d

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

crates/core_arch/src/arm_shared/neon/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,6 +5326,23 @@ mod tests {
53265326
test_vld3q_lane_f16(f16, 24, 7, float16x8x3_t, vst3q_lane_f16, vld3q_lane_f16);
53275327
test_vld4q_lane_f16(f16, 32, 7, float16x8x4_t, vst4q_lane_f16, vld4q_lane_f16);
53285328
}
5329+
5330+
// FIXME: simd_test (which uses is_arm_feature_detected) does not support the v8
5331+
// feature that we need on arm.
5332+
#[cfg(any(target_arch = "aarch64", target_arch = "arm64ec"))]
5333+
#[simd_test(enable = "crc")]
5334+
fn crc32() {
5335+
assert_eq!(__crc32b(u32::MAX, u8::MAX), 16777215);
5336+
assert_eq!(__crc32h(u32::MAX, u16::MAX), 65535);
5337+
assert_eq!(__crc32w(u32::MAX, u32::MAX), 0);
5338+
5339+
assert_eq!(__crc32cb(u32::MAX, u8::MAX), 16777215);
5340+
assert_eq!(__crc32ch(u32::MAX, u16::MAX), 65535);
5341+
assert_eq!(__crc32cw(u32::MAX, u32::MAX), 0);
5342+
5343+
assert_eq!(__crc32d(u32::MAX, u64::MAX), 3736805603);
5344+
assert_eq!(__crc32cd(u32::MAX, u64::MAX), 3080238136);
5345+
}
53295346
}
53305347

53315348
#[cfg(all(test, target_arch = "arm"))]

crates/core_arch/src/loongarch64/mod.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ unsafe extern "unadjusted" {
6464
#[inline(always)]
6565
#[unstable(feature = "stdarch_loongarch", issue = "117427")]
6666
pub fn crc_w_b_w(a: i8, b: i32) -> i32 {
67-
unsafe { __crc_w_b_w(a as i32, b) }
67+
unsafe { __crc_w_b_w(a.cast_unsigned() as i32, b) }
6868
}
6969

7070
/// Calculate the CRC value using the IEEE 802.3 polynomial (0xEDB88320)
7171
#[inline(always)]
7272
#[unstable(feature = "stdarch_loongarch", issue = "117427")]
7373
pub fn crc_w_h_w(a: i16, b: i32) -> i32 {
74-
unsafe { __crc_w_h_w(a as i32, b) }
74+
unsafe { __crc_w_h_w(a.cast_unsigned() as i32, b) }
7575
}
7676

7777
/// Calculate the CRC value using the IEEE 802.3 polynomial (0xEDB88320)
@@ -92,14 +92,14 @@ pub fn crc_w_d_w(a: i64, b: i32) -> i32 {
9292
#[inline(always)]
9393
#[unstable(feature = "stdarch_loongarch", issue = "117427")]
9494
pub fn crcc_w_b_w(a: i8, b: i32) -> i32 {
95-
unsafe { __crcc_w_b_w(a as i32, b) }
95+
unsafe { __crcc_w_b_w(a.cast_unsigned() as i32, b) }
9696
}
9797

9898
/// Calculate the CRC value using the Castagnoli polynomial (0x82F63B78)
9999
#[inline(always)]
100100
#[unstable(feature = "stdarch_loongarch", issue = "117427")]
101101
pub fn crcc_w_h_w(a: i16, b: i32) -> i32 {
102-
unsafe { __crcc_w_h_w(a as i32, b) }
102+
unsafe { __crcc_w_h_w(a.cast_unsigned() as i32, b) }
103103
}
104104

105105
/// Calculate the CRC value using the Castagnoli polynomial (0x82F63B78)
@@ -163,14 +163,14 @@ pub unsafe fn iocsrwr_d(a: i64, b: i32) {
163163
__iocsrwr_d(a, b)
164164
}
165165

166-
/// Generates the less-than-or-equal asseration instruction
166+
/// Generates the less-than-or-equal assertion instruction
167167
#[inline(always)]
168168
#[unstable(feature = "stdarch_loongarch", issue = "117427")]
169169
pub unsafe fn asrtle(a: i64, b: i64) {
170170
__asrtle(a, b);
171171
}
172172

173-
/// Generates the greater-than asseration instruction
173+
/// Generates the greater-than assertion instruction
174174
#[inline(always)]
175175
#[unstable(feature = "stdarch_loongarch", issue = "117427")]
176176
pub unsafe fn asrtgt(a: i64, b: i64) {
@@ -194,3 +194,21 @@ pub unsafe fn ldpte<const IMM8: i64>(a: i64) {
194194
static_assert_uimm_bits!(IMM8, 8);
195195
__ldpte(a, IMM8)
196196
}
197+
198+
#[cfg(test)]
199+
mod tests {
200+
use super::*;
201+
202+
#[test]
203+
fn crc32() {
204+
assert_eq!(crc_w_b_w(-1, -1), 16777215);
205+
assert_eq!(crc_w_h_w(-1, -1), 65535);
206+
assert_eq!(crc_w_w_w(-1, -1), 0);
207+
assert_eq!(crc_w_d_w(-1, -1), 3736805603u32.cast_signed());
208+
209+
assert_eq!(crcc_w_b_w(-1, -1), 16777215);
210+
assert_eq!(crcc_w_h_w(-1, -1), 65535);
211+
assert_eq!(crcc_w_w_w(-1, -1), 0);
212+
assert_eq!(crcc_w_d_w(-1, -1), 3080238136u32.cast_signed());
213+
}
214+
}

0 commit comments

Comments
 (0)