Skip to content

Commit

Permalink
[RISCV] Update the CORE-V C API headers to follow the RISC-V conventi…
Browse files Browse the repository at this point in the history
…ons.

This patch changes the C API headers that were added for the CORE-V ISA
extensions so that they follow the standard described for RISC-V here:

https://github.com/riscv-non-isa/riscv-c-api-doc/blob/master/riscv-c-api.md#intrinsic-functions

The C API headers must provide a uniform interface for instrinsic
functions that is compatible with any compiler that supports them.

In the wrapper of a C API intrinsic function definition any compiler
can implement the functionality differently, whether it is with an existing
instrinsic function, or with inline assembly etc...

The user code that adds a C API RISC-V header is agnostic to the
implementation and is then compatible with any compiler that has the header.

The RISC-V intrinsic functions have
- the common prefix '__riscv_' to avoid name collision with other targets.
- a vendor specific prefix if they belong to a vendor specific extension.
- the name of the function.
- the type of the function: this is typically used to distinguish
  different versions of the same intrinsic function that differ only for
  the type of one or more operands.

Another convention is to use the type 'long' instead of integer types
like int32_t or uint32_t as the type 'long' will be set to the
current XLEN.

According to these conventions the CORE-V intrinsic functions are
now redefined like:

long __riscv_cv_name_type(long op1, long op2) {
  return __builtin_riscv_cv_name_type(op1, op2);
}
  • Loading branch information
PaoloS02 committed Nov 10, 2023
1 parent f152674 commit c727260
Show file tree
Hide file tree
Showing 5 changed files with 828 additions and 195 deletions.
106 changes: 85 additions & 21 deletions clang/lib/Headers/riscv_corev_alu.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,91 @@ extern "C" {
#endif

#if defined(__riscv_xcvalu)
int __builtin_abs (int j);
int __builtin_riscv_cv_alu_slet (int32_t i, int32_t j);
int __builtin_riscv_cv_alu_sletu (uint32_t i, uint32_t j);
int32_t __builtin_riscv_cv_alu_min (int32_t i, int32_t j);
uint32_t __builtin_riscv_cv_alu_minu (uint32_t i, uint32_t j);
int32_t __builtin_riscv_cv_alu_max (int32_t i, int32_t j);
uint32_t __builtin_riscv_cv_alu_maxu (uint32_t i, uint32_t j);
int32_t __builtin_riscv_cv_alu_exths (int16_t i);
uint32_t __builtin_riscv_cv_alu_exthz (uint16_t i);
int32_t __builtin_riscv_cv_alu_extbs (int8_t i);
uint32_t __builtin_riscv_cv_alu_extbz (uint8_t);
int32_t __builtin_riscv_cv_alu_clip (int32_t i, uint32_t j);
uint32_t __builtin_riscv_cv_alu_clipu (uint32_t i, uint32_t j);
int32_t __builtin_riscv_cv_alu_addN (int32_t x, int32_t y, uint8_t shft);
uint32_t __builtin_riscv_cv_alu_adduN (uint32_t x, uint32_t y, uint8_t shft);
int32_t __builtin_riscv_cv_alu_addRN (int32_t x, int32_t y, uint8_t shft);
uint32_t __builtin_riscv_cv_alu_adduRN (uint32_t x, uint32_t y, uint8_t shft);
int32_t __builtin_riscv_cv_alu_subN (int32_t x, int32_t y, uint8_t shft);
uint32_t __builtin_riscv_cv_alu_subuN (uint32_t x, uint32_t y, uint8_t shft);
int32_t __builtin_riscv_cv_alu_subRN (int32_t x, int32_t y, uint8_t shft);
uint32_t __builtin_riscv_cv_alu_subuRN (uint32_t x, uint32_t y, uint8_t shft);

#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))

long __riscv_cv_abs(long a) { return __builtin_abs(a); }

long __riscv_cv_alu_slet(long a, long b) {
return __builtin_riscv_cv_alu_slet(a, b);
}

long __riscv_cv_alu_sletu(unsigned long a, unsigned long b) {
return __builtin_riscv_cv_alu_sletu(a, b);
}

long __riscv_cv_alu_min(long a, long b) {
return __builtin_riscv_cv_alu_min(a, b);
}

unsigned long __riscv_cv_alu_minu(unsigned long a, unsigned long b) {
return __builtin_riscv_cv_alu_minu(a, b);
}

long __riscv_cv_alu_max(long a, long b) {
return __builtin_riscv_cv_alu_max(a, b);
}

unsigned long __riscv_cv_alu_maxu(unsigned long a, unsigned long b) {
return __builtin_riscv_cv_alu_maxu(a, b);
}

long __riscv_cv_alu_exths(int16_t a) { return __builtin_riscv_cv_alu_exths(a); }

unsigned long __riscv_cv_alu_exthz(uint16_t a) {
return __builtin_riscv_cv_alu_exthz(a);
}

long __riscv_cv_alu_extbs(int8_t a) { return __builtin_riscv_cv_alu_extbs(a); }

unsigned long __riscv_cv_alu_extbz(uint8_t a) {
return __builtin_riscv_cv_alu_extbz(a);
}

long __riscv_cv_alu_clip(long a, unsigned long b) {
return __builtin_riscv_cv_alu_clip(a, b);
}

unsigned long __riscv_cv_alu_clipu(unsigned long a, unsigned long b) {
return __builtin_riscv_cv_alu_clipu(a, b);
}

long __riscv_cv_alu_addN(long a, long b, uint8_t shft) {
return __builtin_riscv_cv_alu_addN(a, b, shft);
}

unsigned long __riscv_cv_alu_adduN(unsigned long a, unsigned long b,
uint8_t shft) {
return __builtin_riscv_cv_alu_adduN(a, b, shft);
}

long __riscv_cv_alu_addRN(long a, long b, uint8_t shft) {
return __builtin_riscv_cv_alu_addRN(a, b, shft);
}

unsigned long __riscv_cv_alu_adduRN(unsigned long a, unsigned long b,
uint8_t shft) {
return __builtin_riscv_cv_alu_adduRN(a, b, shft);
}

long __riscv_cv_alu_subN(long a, long b, uint8_t shft) {
return __builtin_riscv_cv_alu_subN(a, b, shft);
}

unsigned long __riscv_cv_alu_subuN(unsigned long a, unsigned long b,
uint8_t shft) {
return __builtin_riscv_cv_alu_subuN(a, b, shft);
}

long __riscv_cv_alu_subRN(long a, long b, uint8_t shft) {
return __builtin_riscv_cv_alu_subRN(a, b, shft);
}

unsigned long __riscv_cv_alu_subuRN(unsigned long a, unsigned long b,
uint8_t shft) {
return __builtin_riscv_cv_alu_subuRN(a, b, shft);
}

#endif // defined(__riscv_xcvalu)

#if defined(__cplusplus)
Expand Down
60 changes: 49 additions & 11 deletions clang/lib/Headers/riscv_corev_bitmanip.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,55 @@ extern "C" {
#endif

#if defined(__riscv_xcvbitmanip)
int32_t __builtin_riscv_cv_bitmanip_extract (int32_t i, uint16_t range);
uint32_t __builtin_riscv_cv_bitmanip_extractu (uint32_t i, uint16_t range);
uint32_t __builtin_riscv_cv_bitmanip_insert (uint32_t i, uint16_t range, uint32_t k);
uint32_t __builtin_riscv_cv_bitmanip_bclr (uint32_t i, uint16_t range);
uint32_t __builtin_riscv_cv_bitmanip_bset (uint32_t i, uint16_t range);
uint8_t __builtin_riscv_cv_bitmanip_ff1 (uint32_t i);
uint8_t __builtin_riscv_cv_bitmanip_fl1 (uint32_t i);
uint8_t __builtin_riscv_cv_bitmanip_clb (uint32_t i);
uint8_t __builtin_riscv_cv_bitmanip_cnt (uint32_t i);
uint32_t __builtin_riscv_cv_bitmanip_ror (uint32_t i, uint32_t j);
uint32_t __builtin_riscv_cv_bitmanip_bitrev (uint32_t i, uint8_t pts, uint8_t radix);

#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))

long __riscv_cv_bitmanip_extract(long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_extract(a, range);
}

unsigned long __riscv_cv_bitmanip_extractu(unsigned long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_extractu(a, range);
}

unsigned long __riscv_cv_bitmanip_insert(unsigned long a, uint16_t range,
unsigned long k) {
return __builtin_riscv_cv_bitmanip_insert(a, range, k);
}

unsigned long __riscv_cv_bitmanip_bclr(unsigned long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_bclr(a, range);
}

unsigned long __riscv_cv_bitmanip_bset(unsigned long a, uint16_t range) {
return __builtin_riscv_cv_bitmanip_bset(a, range);
}

uint8_t __riscv_cv_bitmanip_ff1(unsigned long a) {
return __builtin_riscv_cv_bitmanip_ff1(a);
}

uint8_t __riscv_cv_bitmanip_fl1(unsigned long a) {
return __builtin_riscv_cv_bitmanip_fl1(a);
}

uint8_t __riscv_cv_bitmanip_clb(unsigned long a) {
return __builtin_riscv_cv_bitmanip_clb(a);
}

uint8_t __riscv_cv_bitmanip_cnt(unsigned long a) {
return __builtin_riscv_cv_bitmanip_cnt(a);
}

unsigned long __riscv_cv_bitmanip_ror(unsigned long a, unsigned long b) {
return __builtin_riscv_cv_bitmanip_ror(a, b);
}

unsigned long __riscv_cv_bitmanip_bitrev(unsigned long a, uint8_t pts,
uint8_t radix) {
return __builtin_riscv_cv_bitmanip_bitrev(a, pts, radix);
}

#endif // defined(__riscv_xcvbitmanip)

#if defined(__cplusplus)
Expand Down
8 changes: 7 additions & 1 deletion clang/lib/Headers/riscv_corev_elw.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ extern "C" {
#endif

#if defined(__riscv_xcvelw)
uint32_t __builtin_riscv_cv_elw_elw (void *loc);

#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))

unsigned long __riscv_cv_elw_elw(void *loc) {
return __builtin_riscv_cv_elw_elw(loc);
}

#endif // defined(__riscv_xcvelw)

#if defined(__cplusplus)
Expand Down
109 changes: 91 additions & 18 deletions clang/lib/Headers/riscv_corev_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,97 @@ extern "C" {
#endif

#if defined(__riscv_xcvmac)
int32_t __builtin_riscv_cv_mac_mac (int32_t x, int32_t y, int32_t z);
int32_t __builtin_riscv_cv_mac_msu (int32_t x, int32_t y, int32_t z);
uint32_t __builtin_riscv_cv_mac_muluN (uint32_t x, uint32_t y, const uint8_t shft);
uint32_t __builtin_riscv_cv_mac_mulhhuN (uint32_t x, uint32_t y, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_mulsN (uint32_t x, uint32_t y, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_mulhhsN (uint32_t x, uint32_t y, const uint8_t shft);
uint32_t __builtin_riscv_cv_mac_muluRN (uint32_t x, uint32_t y, const uint8_t shft);
uint32_t __builtin_riscv_cv_mac_mulhhuRN (uint32_t x, uint32_t y, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_mulsRN (uint32_t x, uint32_t y, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_mulhhsRN (uint32_t x, uint32_t y, const uint8_t shft);
uint32_t __builtin_riscv_cv_mac_macuN (uint32_t x, uint32_t y, uint32_t z, const uint8_t shft);
uint32_t __builtin_riscv_cv_mac_machhuN (uint32_t x, uint32_t y, uint32_t z, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_macsN (uint32_t x, uint32_t y, int32_t z, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_machhsN (uint32_t x, uint32_t y, int32_t z, const uint8_t shft);
uint32_t __builtin_riscv_cv_mac_macuRN (uint32_t x, uint32_t y, uint32_t z, const uint8_t shft);
uint32_t __builtin_riscv_cv_mac_machhuRN (uint32_t x, uint32_t y, uint32_t z, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_macsRN (uint32_t x, uint32_t y, int32_t z, const uint8_t shft);
int32_t __builtin_riscv_cv_mac_machhsRN (uint32_t x, uint32_t y, int32_t z, const uint8_t shft);

#define __DEFAULT_FN_ATTRS __attribute__((__always_inline__, __nodebug__))

long __riscv_cv_mac_mac(long a, long b, long c) {
return __builtin_riscv_cv_mac_mac(a, b, c);
}

long __riscv_cv_mac_msu(long a, long b, long c) {
return __builtin_riscv_cv_mac_msu(a, b, c);
}

unsigned long __riscv_cv_mac_muluN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_muluN(a, b, shft);
}

unsigned long __riscv_cv_mac_mulhhuN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_mulhhuN(a, b, shft);
}

long __riscv_cv_mac_mulsN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_mulsN(a, b, shft);
}

long __riscv_cv_mac_mulhhsN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_mulhhsN(a, b, shft);
}

unsigned long __riscv_cv_mac_muluRN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_muluRN(a, b, shft);
}

unsigned long __riscv_cv_mac_mulhhuRN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_mulhhuRN(a, b, shft);
}

long __riscv_cv_mac_mulsRN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_mulsRN(a, b, shft);
}

long __riscv_cv_mac_mulhhsRN(unsigned long a, unsigned long b,
const uint8_t shft) {
return __builtin_riscv_cv_mac_mulhhsRN(a, b, shft);
}

unsigned long __riscv_cv_mac_macuN(unsigned long a, unsigned long b,
unsigned long c, const uint8_t shft) {
return __builtin_riscv_cv_mac_macuN(a, b, c, shft);
}

unsigned long __riscv_cv_mac_machhuN(unsigned long a, unsigned long b,
unsigned long c, const uint8_t shft) {
return __builtin_riscv_cv_mac_machhuN(a, b, c, shft);
}

long __riscv_cv_mac_macsN(unsigned long a, unsigned long b, long c,
const uint8_t shft) {
return __builtin_riscv_cv_mac_macsN(a, b, c, shft);
}

long __riscv_cv_mac_machhsN(unsigned long a, unsigned long b, long c,
const uint8_t shft) {
return __builtin_riscv_cv_mac_machhsN(a, b, c, shft);
}

unsigned long __riscv_cv_mac_macuRN(unsigned long a, unsigned long b,
unsigned long c, const uint8_t shft) {
return __builtin_riscv_cv_mac_macuRN(a, b, c, shft);
}

unsigned long __riscv_cv_mac_machhuRN(unsigned long a, unsigned long b,
unsigned long c, const uint8_t shft) {
return __builtin_riscv_cv_mac_machhuRN(a, b, c, shft);
}

long __riscv_cv_mac_macsRN(unsigned long a, unsigned long b, long c,
const uint8_t shft) {
return __builtin_riscv_cv_mac_macsRN(a, b, c, shft);
}

long __riscv_cv_mac_machhsRN(unsigned long a, unsigned long b, long c,
const uint8_t shft) {
return __builtin_riscv_cv_mac_machhsRN(a, b, c, shft);
}

#endif // defined(__riscv_xcvmac)

#if defined(__cplusplus)
Expand Down
Loading

0 comments on commit c727260

Please sign in to comment.