riscv_bits.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2019 Nuclei Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef __RISCV_BITS_H__
  19. #define __RISCV_BITS_H__
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #if __riscv_xlen == 64
  24. # define SLL32 sllw
  25. # define STORE sd
  26. # define LOAD ld
  27. # define LWU lwu
  28. # define LOG_REGBYTES 3
  29. #else
  30. # define SLL32 sll
  31. # define STORE sw
  32. # define LOAD lw
  33. # define LWU lw
  34. # define LOG_REGBYTES 2
  35. #endif /* __riscv_xlen */
  36. #define REGBYTES (1 << LOG_REGBYTES)
  37. #if defined(__riscv_flen)
  38. #if __riscv_flen == 64
  39. # define FPSTORE fsd
  40. # define FPLOAD fld
  41. # define LOG_FPREGBYTES 3
  42. #else
  43. # define FPSTORE fsw
  44. # define FPLOAD flw
  45. # define LOG_FPREGBYTES 2
  46. #endif /* __riscv_flen == 64 */
  47. #define FPREGBYTES (1 << LOG_FPREGBYTES)
  48. #endif /* __riscv_flen */
  49. #ifdef __GNUC__
  50. #define __rv_likely(x) __builtin_expect((x), 1)
  51. #define __rv_unlikely(x) __builtin_expect((x), 0)
  52. #else
  53. #define __rv_likely(x) (x)
  54. #define __rv_unlikely(x) (x)
  55. #endif
  56. #define __RV_ROUNDUP(a, b) ((((a)-1)/(b)+1)*(b))
  57. #define __RV_ROUNDDOWN(a, b) ((a)/(b)*(b))
  58. #define __RV_MAX(a, b) ((a) > (b) ? (a) : (b))
  59. #define __RV_MIN(a, b) ((a) < (b) ? (a) : (b))
  60. #define __RV_CLAMP(a, lo, hi) __RV_MIN(__RV_MAX(a, lo), hi)
  61. #define __RV_EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1)))
  62. #define __RV_INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))))
  63. #ifdef __ASSEMBLY__
  64. #define _AC(X,Y) X
  65. #define _AT(T,X) X
  66. #else
  67. #define __AC(X,Y) (X##Y)
  68. #define _AC(X,Y) __AC(X,Y)
  69. #define _AT(T,X) ((T)(X))
  70. #endif /* __ASSEMBLY__ */
  71. #define _UL(x) (_AC(x, UL))
  72. #define _ULL(x) (_AC(x, ULL))
  73. #define _BITUL(x) (_UL(1) << (x))
  74. #define _BITULL(x) (_ULL(1) << (x))
  75. #define UL(x) (_UL(x))
  76. #define ULL(x) (_ULL(x))
  77. #define STR(x) XSTR(x)
  78. #define XSTR(x) #x
  79. #define __STR(s) #s
  80. #define STRINGIFY(s) __STR(s)
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. #endif /* __RISCV_BITS_H__ */