xref: /freebsd/contrib/llvm-project/clang/lib/Headers/larchintrin.h (revision 439352ac8257c8419cb4a662abb7f260f31f9932)
1bdd1243dSDimitry Andric /*===------------ larchintrin.h - LoongArch intrinsics ---------------------===
2bdd1243dSDimitry Andric  *
3bdd1243dSDimitry Andric  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric  * See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric  *
7bdd1243dSDimitry Andric  *===-----------------------------------------------------------------------===
8bdd1243dSDimitry Andric  */
9bdd1243dSDimitry Andric 
10bdd1243dSDimitry Andric #ifndef _LOONGARCH_BASE_INTRIN_H
11bdd1243dSDimitry Andric #define _LOONGARCH_BASE_INTRIN_H
12bdd1243dSDimitry Andric 
13bdd1243dSDimitry Andric #ifdef __cplusplus
14bdd1243dSDimitry Andric extern "C" {
15bdd1243dSDimitry Andric #endif
16bdd1243dSDimitry Andric 
17bdd1243dSDimitry Andric typedef struct rdtime {
18bdd1243dSDimitry Andric   unsigned int value;
19bdd1243dSDimitry Andric   unsigned int timeid;
20bdd1243dSDimitry Andric } __rdtime_t;
21bdd1243dSDimitry Andric 
22bdd1243dSDimitry Andric #if __loongarch_grlen == 64
23bdd1243dSDimitry Andric typedef struct drdtime {
24bdd1243dSDimitry Andric   unsigned long dvalue;
25bdd1243dSDimitry Andric   unsigned long dtimeid;
26bdd1243dSDimitry Andric } __drdtime_t;
27bdd1243dSDimitry Andric 
28bdd1243dSDimitry Andric extern __inline __drdtime_t
29bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
30bdd1243dSDimitry Andric     __rdtime_d(void) {
31bdd1243dSDimitry Andric   __drdtime_t __drdtime;
32bdd1243dSDimitry Andric   __asm__ volatile(
33bdd1243dSDimitry Andric       "rdtime.d %[val], %[tid]\n\t"
34bdd1243dSDimitry Andric       : [val] "=&r"(__drdtime.dvalue), [tid] "=&r"(__drdtime.dtimeid));
35bdd1243dSDimitry Andric   return __drdtime;
36bdd1243dSDimitry Andric }
37bdd1243dSDimitry Andric #endif
38bdd1243dSDimitry Andric 
39bdd1243dSDimitry Andric extern __inline __rdtime_t
40bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
41bdd1243dSDimitry Andric     __rdtimeh_w(void) {
42bdd1243dSDimitry Andric   __rdtime_t __rdtime;
43bdd1243dSDimitry Andric   __asm__ volatile("rdtimeh.w %[val], %[tid]\n\t"
44bdd1243dSDimitry Andric                    : [val] "=&r"(__rdtime.value), [tid] "=&r"(__rdtime.timeid));
45bdd1243dSDimitry Andric   return __rdtime;
46bdd1243dSDimitry Andric }
47bdd1243dSDimitry Andric 
48bdd1243dSDimitry Andric extern __inline __rdtime_t
49bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
50bdd1243dSDimitry Andric     __rdtimel_w(void) {
51bdd1243dSDimitry Andric   __rdtime_t __rdtime;
52bdd1243dSDimitry Andric   __asm__ volatile("rdtimel.w %[val], %[tid]\n\t"
53bdd1243dSDimitry Andric                    : [val] "=&r"(__rdtime.value), [tid] "=&r"(__rdtime.timeid));
54bdd1243dSDimitry Andric   return __rdtime;
55bdd1243dSDimitry Andric }
56bdd1243dSDimitry Andric 
57bdd1243dSDimitry Andric #if __loongarch_grlen == 64
58bdd1243dSDimitry Andric extern __inline int
59bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
60bdd1243dSDimitry Andric     __crc_w_b_w(char _1, int _2) {
61bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crc_w_b_w((char)_1, (int)_2);
62bdd1243dSDimitry Andric }
63bdd1243dSDimitry Andric 
64bdd1243dSDimitry Andric extern __inline int
65bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
66bdd1243dSDimitry Andric     __crc_w_h_w(short _1, int _2) {
67bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crc_w_h_w((short)_1, (int)_2);
68bdd1243dSDimitry Andric }
69bdd1243dSDimitry Andric 
70bdd1243dSDimitry Andric extern __inline int
71bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
72bdd1243dSDimitry Andric     __crc_w_w_w(int _1, int _2) {
73bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crc_w_w_w((int)_1, (int)_2);
74bdd1243dSDimitry Andric }
75bdd1243dSDimitry Andric 
76bdd1243dSDimitry Andric extern __inline int
77bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
78bdd1243dSDimitry Andric     __crc_w_d_w(long int _1, int _2) {
79bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crc_w_d_w((long int)_1, (int)_2);
80bdd1243dSDimitry Andric }
81bdd1243dSDimitry Andric 
82bdd1243dSDimitry Andric extern __inline int
83bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
84bdd1243dSDimitry Andric     __crcc_w_b_w(char _1, int _2) {
85bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crcc_w_b_w((char)_1, (int)_2);
86bdd1243dSDimitry Andric }
87bdd1243dSDimitry Andric 
88bdd1243dSDimitry Andric extern __inline int
89bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
90bdd1243dSDimitry Andric     __crcc_w_h_w(short _1, int _2) {
91bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crcc_w_h_w((short)_1, (int)_2);
92bdd1243dSDimitry Andric }
93bdd1243dSDimitry Andric 
94bdd1243dSDimitry Andric extern __inline int
95bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
96bdd1243dSDimitry Andric     __crcc_w_w_w(int _1, int _2) {
97bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crcc_w_w_w((int)_1, (int)_2);
98bdd1243dSDimitry Andric }
99bdd1243dSDimitry Andric 
100bdd1243dSDimitry Andric extern __inline int
101bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
102bdd1243dSDimitry Andric     __crcc_w_d_w(long int _1, int _2) {
103bdd1243dSDimitry Andric   return (int)__builtin_loongarch_crcc_w_d_w((long int)_1, (int)_2);
104bdd1243dSDimitry Andric }
105bdd1243dSDimitry Andric #endif
106bdd1243dSDimitry Andric 
107bdd1243dSDimitry Andric #define __break(/*ui15*/ _1) __builtin_loongarch_break((_1))
108bdd1243dSDimitry Andric 
109bdd1243dSDimitry Andric #if __loongarch_grlen == 32
110bdd1243dSDimitry Andric #define __cacop_w(/*uimm5*/ _1, /*unsigned int*/ _2, /*simm12*/ _3)            \
111bdd1243dSDimitry Andric   ((void)__builtin_loongarch_cacop_w((_1), (unsigned int)(_2), (_3)))
112bdd1243dSDimitry Andric #endif
113bdd1243dSDimitry Andric 
114bdd1243dSDimitry Andric #if __loongarch_grlen == 64
115bdd1243dSDimitry Andric #define __cacop_d(/*uimm5*/ _1, /*unsigned long int*/ _2, /*simm12*/ _3)       \
116bdd1243dSDimitry Andric   ((void)__builtin_loongarch_cacop_d((_1), (unsigned long int)(_2), (_3)))
117bdd1243dSDimitry Andric #endif
118bdd1243dSDimitry Andric 
119bdd1243dSDimitry Andric #define __dbar(/*ui15*/ _1) __builtin_loongarch_dbar((_1))
120bdd1243dSDimitry Andric 
121bdd1243dSDimitry Andric #define __ibar(/*ui15*/ _1) __builtin_loongarch_ibar((_1))
122bdd1243dSDimitry Andric 
123bdd1243dSDimitry Andric #define __movfcsr2gr(/*ui5*/ _1) __builtin_loongarch_movfcsr2gr((_1));
124bdd1243dSDimitry Andric 
125bdd1243dSDimitry Andric #define __movgr2fcsr(/*ui5*/ _1, _2)                                           \
126bdd1243dSDimitry Andric   __builtin_loongarch_movgr2fcsr((_1), (unsigned int)_2);
127bdd1243dSDimitry Andric 
128bdd1243dSDimitry Andric #define __syscall(/*ui15*/ _1) __builtin_loongarch_syscall((_1))
129bdd1243dSDimitry Andric 
130bdd1243dSDimitry Andric #define __csrrd_w(/*ui14*/ _1) ((unsigned int)__builtin_loongarch_csrrd_w((_1)))
131bdd1243dSDimitry Andric 
132bdd1243dSDimitry Andric #define __csrwr_w(/*unsigned int*/ _1, /*ui14*/ _2)                            \
133bdd1243dSDimitry Andric   ((unsigned int)__builtin_loongarch_csrwr_w((unsigned int)(_1), (_2)))
134bdd1243dSDimitry Andric 
135bdd1243dSDimitry Andric #define __csrxchg_w(/*unsigned int*/ _1, /*unsigned int*/ _2, /*ui14*/ _3)     \
136bdd1243dSDimitry Andric   ((unsigned int)__builtin_loongarch_csrxchg_w((unsigned int)(_1),             \
137bdd1243dSDimitry Andric                                                (unsigned int)(_2), (_3)))
138bdd1243dSDimitry Andric 
139bdd1243dSDimitry Andric #if __loongarch_grlen == 64
140bdd1243dSDimitry Andric #define __csrrd_d(/*ui14*/ _1)                                                 \
141bdd1243dSDimitry Andric   ((unsigned long int)__builtin_loongarch_csrrd_d((_1)))
142bdd1243dSDimitry Andric 
143bdd1243dSDimitry Andric #define __csrwr_d(/*unsigned long int*/ _1, /*ui14*/ _2)                       \
144bdd1243dSDimitry Andric   ((unsigned long int)__builtin_loongarch_csrwr_d((unsigned long int)(_1),     \
145bdd1243dSDimitry Andric                                                   (_2)))
146bdd1243dSDimitry Andric 
147bdd1243dSDimitry Andric #define __csrxchg_d(/*unsigned long int*/ _1, /*unsigned long int*/ _2,        \
148bdd1243dSDimitry Andric                     /*ui14*/ _3)                                               \
149bdd1243dSDimitry Andric   ((unsigned long int)__builtin_loongarch_csrxchg_d(                           \
150bdd1243dSDimitry Andric       (unsigned long int)(_1), (unsigned long int)(_2), (_3)))
151bdd1243dSDimitry Andric #endif
152bdd1243dSDimitry Andric 
153bdd1243dSDimitry Andric extern __inline unsigned char
154bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
155bdd1243dSDimitry Andric     __iocsrrd_b(unsigned int _1) {
156bdd1243dSDimitry Andric   return (unsigned char)__builtin_loongarch_iocsrrd_b((unsigned int)_1);
157bdd1243dSDimitry Andric }
158bdd1243dSDimitry Andric 
159*439352acSDimitry Andric extern __inline unsigned short
160bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
161bdd1243dSDimitry Andric     __iocsrrd_h(unsigned int _1) {
162bdd1243dSDimitry Andric   return (unsigned short)__builtin_loongarch_iocsrrd_h((unsigned int)_1);
163bdd1243dSDimitry Andric }
164bdd1243dSDimitry Andric 
165bdd1243dSDimitry Andric extern __inline unsigned int
166bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
167bdd1243dSDimitry Andric     __iocsrrd_w(unsigned int _1) {
168bdd1243dSDimitry Andric   return (unsigned int)__builtin_loongarch_iocsrrd_w((unsigned int)_1);
169bdd1243dSDimitry Andric }
170bdd1243dSDimitry Andric 
171bdd1243dSDimitry Andric #if __loongarch_grlen == 64
172bdd1243dSDimitry Andric extern __inline unsigned long int
173bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
174bdd1243dSDimitry Andric     __iocsrrd_d(unsigned int _1) {
175bdd1243dSDimitry Andric   return (unsigned long int)__builtin_loongarch_iocsrrd_d((unsigned int)_1);
176bdd1243dSDimitry Andric }
177bdd1243dSDimitry Andric #endif
178bdd1243dSDimitry Andric 
179bdd1243dSDimitry Andric extern __inline void
180bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
181bdd1243dSDimitry Andric     __iocsrwr_b(unsigned char _1, unsigned int _2) {
182bdd1243dSDimitry Andric   __builtin_loongarch_iocsrwr_b((unsigned char)_1, (unsigned int)_2);
183bdd1243dSDimitry Andric }
184bdd1243dSDimitry Andric 
185bdd1243dSDimitry Andric extern __inline void
186bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
187bdd1243dSDimitry Andric     __iocsrwr_h(unsigned short _1, unsigned int _2) {
188bdd1243dSDimitry Andric   __builtin_loongarch_iocsrwr_h((unsigned short)_1, (unsigned int)_2);
189bdd1243dSDimitry Andric }
190bdd1243dSDimitry Andric 
191bdd1243dSDimitry Andric extern __inline void
192bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
193bdd1243dSDimitry Andric     __iocsrwr_w(unsigned int _1, unsigned int _2) {
194bdd1243dSDimitry Andric   __builtin_loongarch_iocsrwr_w((unsigned int)_1, (unsigned int)_2);
195bdd1243dSDimitry Andric }
196bdd1243dSDimitry Andric 
197bdd1243dSDimitry Andric extern __inline unsigned int
198bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
199bdd1243dSDimitry Andric     __cpucfg(unsigned int _1) {
200bdd1243dSDimitry Andric   return (unsigned int)__builtin_loongarch_cpucfg((unsigned int)_1);
201bdd1243dSDimitry Andric }
202bdd1243dSDimitry Andric 
203bdd1243dSDimitry Andric #if __loongarch_grlen == 64
204bdd1243dSDimitry Andric extern __inline void
205bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
206bdd1243dSDimitry Andric     __iocsrwr_d(unsigned long int _1, unsigned int _2) {
207bdd1243dSDimitry Andric   __builtin_loongarch_iocsrwr_d((unsigned long int)_1, (unsigned int)_2);
208bdd1243dSDimitry Andric }
209bdd1243dSDimitry Andric 
210bdd1243dSDimitry Andric extern __inline void
211bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
212bdd1243dSDimitry Andric     __asrtgt_d(long int _1, long int _2) {
213bdd1243dSDimitry Andric   __builtin_loongarch_asrtgt_d((long int)_1, (long int)_2);
214bdd1243dSDimitry Andric }
215bdd1243dSDimitry Andric 
216bdd1243dSDimitry Andric extern __inline void
217bdd1243dSDimitry Andric     __attribute__((__gnu_inline__, __always_inline__, __artificial__))
218bdd1243dSDimitry Andric     __asrtle_d(long int _1, long int _2) {
219bdd1243dSDimitry Andric   __builtin_loongarch_asrtle_d((long int)_1, (long int)_2);
220bdd1243dSDimitry Andric }
221bdd1243dSDimitry Andric #endif
222bdd1243dSDimitry Andric 
223bdd1243dSDimitry Andric #if __loongarch_grlen == 64
224bdd1243dSDimitry Andric #define __lddir_d(/*long int*/ _1, /*ui5*/ _2)                                 \
225bdd1243dSDimitry Andric   ((long int)__builtin_loongarch_lddir_d((long int)(_1), (_2)))
226bdd1243dSDimitry Andric 
227bdd1243dSDimitry Andric #define __ldpte_d(/*long int*/ _1, /*ui5*/ _2)                                 \
228bdd1243dSDimitry Andric   ((void)__builtin_loongarch_ldpte_d((long int)(_1), (_2)))
229bdd1243dSDimitry Andric #endif
230bdd1243dSDimitry Andric 
2317a6dacacSDimitry Andric #define __frecipe_s(/*float*/ _1)                                              \
2327a6dacacSDimitry Andric   (float)__builtin_loongarch_frecipe_s((float)_1)
2337a6dacacSDimitry Andric 
2347a6dacacSDimitry Andric #define __frecipe_d(/*double*/ _1)                                             \
2357a6dacacSDimitry Andric   (double)__builtin_loongarch_frecipe_d((double)_1)
2367a6dacacSDimitry Andric 
2377a6dacacSDimitry Andric #define __frsqrte_s(/*float*/ _1)                                              \
2387a6dacacSDimitry Andric   (float)__builtin_loongarch_frsqrte_s((float)_1)
2397a6dacacSDimitry Andric 
2407a6dacacSDimitry Andric #define __frsqrte_d(/*double*/ _1)                                             \
2417a6dacacSDimitry Andric   (double)__builtin_loongarch_frsqrte_d((double)_1)
2427a6dacacSDimitry Andric 
243bdd1243dSDimitry Andric #ifdef __cplusplus
244bdd1243dSDimitry Andric }
245bdd1243dSDimitry Andric #endif
246bdd1243dSDimitry Andric #endif /* _LOONGARCH_BASE_INTRIN_H */
247