1 /*
2     Copyright (c) 2013, The Regents of the University of California (Regents).
3     All Rights Reserved.
4 
5     Redistribution and use in source and binary forms, with or without
6     modification, are permitted provided that the following conditions are met:
7     1. Redistributions of source code must retain the above copyright
8        notice, this list of conditions and the following disclaimer.
9     2. Redistributions in binary form must reproduce the above copyright
10        notice, this list of conditions and the following disclaimer in the
11        documentation and/or other materials provided with the distribution.
12     3. Neither the name of the Regents nor the
13        names of its contributors may be used to endorse or promote products
14        derived from this software without specific prior written permission.
15 
16     IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
17     SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
18     OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
19     BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 
21     REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22     THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23     PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
24     HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
25     MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 */
27 
28 /***********************************************************************************
29  * Record of Microchip changes
30  */
31 #ifndef RISCV_BITS_H
32 #define RISCV_BITS_H
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #define likely(x) __builtin_expect((x), 1)
39 #define unlikely(x) __builtin_expect((x), 0)
40 
41 #define ROUNDUP(a, b) ((((a)-1)/(b)+1)*(b))
42 #define ROUNDDOWN(a, b) ((a)/(b)*(b))
43 
44 #define MAX(a, b) ((a) > (b) ? (a) : (b))
45 #define MIN(a, b) ((a) < (b) ? (a) : (b))
46 #define CLAMP(a, lo, hi) MIN(MAX(a, lo), hi)
47 
48 #define EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1)))
49 #define INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1))))
50 
51 #define STR(x) XSTR(x)
52 #define XSTR(x) #x
53 
54 #if __riscv_xlen == 64
55 # define SLL32    sllw
56 # define STORE    sd
57 # define LOAD     ld
58 # define LWU      lwu
59 # define LOG_REGBYTES 3
60 #else
61 # define SLL32    sll
62 # define STORE    sw
63 # define LOAD     lw
64 # define LWU      lw
65 # define LOG_REGBYTES 2
66 #endif
67 #define REGBYTES (1 << LOG_REGBYTES)
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 #endif //RISCV_BITS_H
74