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 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 #if __riscv_xlen == 64 26 # define SLL32 sllw 27 # define STORE sd 28 # define LOAD ld 29 # define LWU lwu 30 # define LOG_REGBYTES 3 31 #else 32 # define SLL32 sll 33 # define STORE sw 34 # define LOAD lw 35 # define LWU lw 36 # define LOG_REGBYTES 2 37 #endif /* __riscv_xlen */ 38 39 #define REGBYTES (1 << LOG_REGBYTES) 40 41 #if __riscv_flen == 64 42 # define FPSTORE fsd 43 # define FPLOAD fld 44 # define LOG_FPREGBYTES 3 45 #else 46 # define FPSTORE fsw 47 # define FPLOAD flw 48 # define LOG_FPREGBYTES 2 49 #endif /* __riscv_flen */ 50 #define FPREGBYTES (1 << LOG_FPREGBYTES) 51 52 #define __rv_likely(x) __builtin_expect((x), 1) 53 #define __rv_unlikely(x) __builtin_expect((x), 0) 54 55 #define __RV_ROUNDUP(a, b) ((((a)-1)/(b)+1)*(b)) 56 #define __RV_ROUNDDOWN(a, b) ((a)/(b)*(b)) 57 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) MIN(MAX(a, lo), hi) 61 62 #define __RV_EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1))) 63 #define __RV_INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1)))) 64 65 #ifdef __ASSEMBLY__ 66 #define _AC(X,Y) X 67 #define _AT(T,X) X 68 #else 69 #define __AC(X,Y) (X##Y) 70 #define _AC(X,Y) __AC(X,Y) 71 #define _AT(T,X) ((T)(X)) 72 #endif /* __ASSEMBLY__ */ 73 74 #define _UL(x) (_AC(x, UL)) 75 #define _ULL(x) (_AC(x, ULL)) 76 77 #define _BITUL(x) (_UL(1) << (x)) 78 #define _BITULL(x) (_ULL(1) << (x)) 79 80 #define UL(x) (_UL(x)) 81 #define ULL(x) (_ULL(x)) 82 83 #define STR(x) XSTR(x) 84 #define XSTR(x) #x 85 #define __STR(s) #s 86 #define STRINGIFY(s) __STR(s) 87 88 #ifdef __cplusplus 89 } 90 #endif 91 92 #endif /** __RISCV_BITS_H__ */ 93