riscv_arch.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_CRYPTO_RISCV_ARCH_H
  10. # define OSSL_CRYPTO_RISCV_ARCH_H
  11. # include <ctype.h>
  12. # include <stdint.h>
  13. # define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
  14. extern uint32_t OPENSSL_riscvcap_P[ ((
  15. # include "riscv_arch.def"
  16. ) + sizeof(uint32_t) - 1) / sizeof(uint32_t) ];
  17. # ifdef OPENSSL_RISCVCAP_IMPL
  18. # define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
  19. uint32_t OPENSSL_riscvcap_P[ ((
  20. # include "riscv_arch.def"
  21. ) + sizeof(uint32_t) - 1) / sizeof(uint32_t) ];
  22. # endif
  23. # define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) \
  24. static inline int RISCV_HAS_##NAME(void) \
  25. { \
  26. return (OPENSSL_riscvcap_P[INDEX] & (1 << BIT_INDEX)) != 0; \
  27. }
  28. # include "riscv_arch.def"
  29. struct RISCV_capability_s {
  30. const char *name;
  31. size_t index;
  32. size_t bit_offset;
  33. };
  34. # define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
  35. extern const struct RISCV_capability_s RISCV_capabilities[
  36. # include "riscv_arch.def"
  37. ];
  38. # ifdef OPENSSL_RISCVCAP_IMPL
  39. # define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) \
  40. { #NAME, INDEX, BIT_INDEX },
  41. const struct RISCV_capability_s RISCV_capabilities[] = {
  42. # include "riscv_arch.def"
  43. };
  44. # endif
  45. # define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
  46. static const size_t kRISCVNumCaps =
  47. # include "riscv_arch.def"
  48. ;
  49. /* Extension combination tests. */
  50. #define RISCV_HAS_ZBB_AND_ZBC() (RISCV_HAS_ZBB() && RISCV_HAS_ZBC())
  51. #define RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE() (RISCV_HAS_ZBKB() && RISCV_HAS_ZKND() && RISCV_HAS_ZKNE())
  52. #define RISCV_HAS_ZKND_AND_ZKNE() (RISCV_HAS_ZKND() && RISCV_HAS_ZKNE())
  53. /*
  54. * The ZVBB is the superset of ZVKB extension. We use macro here to replace the
  55. * `RISCV_HAS_ZVKB()` with `RISCV_HAS_ZVBB() || RISCV_HAS_ZVKB()`.
  56. */
  57. #define RISCV_HAS_ZVKB() (RISCV_HAS_ZVBB() || RISCV_HAS_ZVKB())
  58. #define RISCV_HAS_ZVKB_AND_ZVKNHA() (RISCV_HAS_ZVKB() && RISCV_HAS_ZVKNHA())
  59. #define RISCV_HAS_ZVKB_AND_ZVKNHB() (RISCV_HAS_ZVKB() && RISCV_HAS_ZVKNHB())
  60. #define RISCV_HAS_ZVKB_AND_ZVKSED() (RISCV_HAS_ZVKB() && RISCV_HAS_ZVKSED())
  61. #define RISCV_HAS_ZVKB_AND_ZVKSH() (RISCV_HAS_ZVKB() && RISCV_HAS_ZVKSH())
  62. /*
  63. * Get the size of a vector register in bits (VLEN).
  64. * If RISCV_HAS_V() is false, then this returns 0.
  65. */
  66. size_t riscv_vlen(void);
  67. #endif