Skip to main content

core/stdarch/crates/core_arch/src/loongarch64/
mod.rs

1//! `LoongArch64` intrinsics
2
3mod lasx;
4mod lsx;
5mod simd;
6
7#[unstable(feature = "stdarch_loongarch", issue = "117427")]
8pub use self::lasx::*;
9#[unstable(feature = "stdarch_loongarch", issue = "117427")]
10pub use self::lsx::*;
11
12use crate::arch::asm;
13
14/// Reads the 64-bit stable counter value and the counter ID
15#[inline(always)]
16#[unstable(feature = "stdarch_loongarch", issue = "117427")]
17pub fn rdtime_d() -> (i64, isize) {
18    let (val, tid): (i64, isize);
19    unsafe { asm!("rdtime.d {}, {}", out(reg) val, out(reg) tid, options(readonly, nostack)) };
20    (val, tid)
21}
22
23#[allow(improper_ctypes)]
24unsafe extern "unadjusted" {
25    #[link_name = "llvm.loongarch.crc.w.d.w"]
26    fn __crc_w_d_w(a: i64, b: i32) -> i32;
27    #[link_name = "llvm.loongarch.crcc.w.d.w"]
28    fn __crcc_w_d_w(a: i64, b: i32) -> i32;
29    #[link_name = "llvm.loongarch.cacop.d"]
30    fn __cacop(a: i64, b: i64, c: i64);
31    #[link_name = "llvm.loongarch.csrrd.d"]
32    fn __csrrd(a: i32) -> i64;
33    #[link_name = "llvm.loongarch.csrwr.d"]
34    fn __csrwr(a: i64, b: i32) -> i64;
35    #[link_name = "llvm.loongarch.csrxchg.d"]
36    fn __csrxchg(a: i64, b: i64, c: i32) -> i64;
37    #[link_name = "llvm.loongarch.iocsrrd.d"]
38    fn __iocsrrd_d(a: i32) -> i64;
39    #[link_name = "llvm.loongarch.iocsrwr.d"]
40    fn __iocsrwr_d(a: i64, b: i32);
41    #[link_name = "llvm.loongarch.asrtle.d"]
42    fn __asrtle(a: i64, b: i64);
43    #[link_name = "llvm.loongarch.asrtgt.d"]
44    fn __asrtgt(a: i64, b: i64);
45    #[link_name = "llvm.loongarch.lddir.d"]
46    fn __lddir(a: i64, b: i64) -> i64;
47    #[link_name = "llvm.loongarch.ldpte.d"]
48    fn __ldpte(a: i64, b: i64);
49}
50
51/// Calculate the CRC value using the IEEE 802.3 polynomial (0xEDB88320)
52#[inline(always)]
53#[unstable(feature = "stdarch_loongarch", issue = "117427")]
54pub fn crc_w_d_w(a: i64, b: i32) -> i32 {
55    unsafe { __crc_w_d_w(a, b) }
56}
57
58/// Calculate the CRC value using the Castagnoli polynomial (0x82F63B78)
59#[inline(always)]
60#[unstable(feature = "stdarch_loongarch", issue = "117427")]
61pub fn crcc_w_d_w(a: i64, b: i32) -> i32 {
62    unsafe { __crcc_w_d_w(a, b) }
63}
64
65/// Generates the cache operation instruction
66#[inline(always)]
67#[unstable(feature = "stdarch_loongarch", issue = "117427")]
68pub unsafe fn cacop<const IMM5: i64, const IMM_S12: i64>(b: i64) {
69    static_assert_uimm_bits!(IMM5, 5);
70    static_assert_simm_bits!(IMM_S12, 12);
71    __cacop(IMM5, b, IMM_S12);
72}
73
74/// Reads the CSR
75#[inline(always)]
76#[unstable(feature = "stdarch_loongarch", issue = "117427")]
77pub unsafe fn csrrd<const IMM14: i32>() -> i64 {
78    static_assert_uimm_bits!(IMM14, 14);
79    __csrrd(IMM14)
80}
81
82/// Writes the CSR
83#[inline(always)]
84#[unstable(feature = "stdarch_loongarch", issue = "117427")]
85pub unsafe fn csrwr<const IMM14: i32>(a: i64) -> i64 {
86    static_assert_uimm_bits!(IMM14, 14);
87    __csrwr(a, IMM14)
88}
89
90/// Exchanges the CSR
91#[inline(always)]
92#[unstable(feature = "stdarch_loongarch", issue = "117427")]
93pub unsafe fn csrxchg<const IMM14: i32>(a: i64, b: i64) -> i64 {
94    static_assert_uimm_bits!(IMM14, 14);
95    __csrxchg(a, b, IMM14)
96}
97
98/// Reads the 64-bit IO-CSR
99#[inline(always)]
100#[unstable(feature = "stdarch_loongarch", issue = "117427")]
101pub unsafe fn iocsrrd_d(a: i32) -> i64 {
102    __iocsrrd_d(a)
103}
104
105/// Writes the 64-bit IO-CSR
106#[inline(always)]
107#[unstable(feature = "stdarch_loongarch", issue = "117427")]
108pub unsafe fn iocsrwr_d(a: i64, b: i32) {
109    __iocsrwr_d(a, b)
110}
111
112/// Generates the less-than-or-equal asseration instruction
113#[inline(always)]
114#[unstable(feature = "stdarch_loongarch", issue = "117427")]
115pub unsafe fn asrtle(a: i64, b: i64) {
116    __asrtle(a, b);
117}
118
119/// Generates the greater-than asseration instruction
120#[inline(always)]
121#[unstable(feature = "stdarch_loongarch", issue = "117427")]
122pub unsafe fn asrtgt(a: i64, b: i64) {
123    __asrtgt(a, b);
124}
125
126/// Loads the page table directory entry
127#[inline(always)]
128#[rustc_legacy_const_generics(1)]
129#[unstable(feature = "stdarch_loongarch", issue = "117427")]
130pub unsafe fn lddir<const IMM8: i64>(a: i64) -> i64 {
131    static_assert_uimm_bits!(IMM8, 8);
132    __lddir(a, IMM8)
133}
134
135/// Loads the page table entry
136#[inline(always)]
137#[rustc_legacy_const_generics(1)]
138#[unstable(feature = "stdarch_loongarch", issue = "117427")]
139pub unsafe fn ldpte<const IMM8: i64>(a: i64) {
140    static_assert_uimm_bits!(IMM8, 8);
141    __ldpte(a, IMM8)
142}