NASM 2.13rc16
[nasm.git] / include / iflag.h
blob02787de9ef28589b6a80038b7e9727b1fdb410b0
1 #ifndef NASM_IFLAG_H
2 #define NASM_IFLAG_H
4 #include <string.h>
6 #include "compiler.h"
8 int ilog2_32(uint32_t v);
10 #include "iflaggen.h"
12 #define IF_GENBIT(bit) (UINT32_C(1) << (bit))
14 static inline unsigned int iflag_test(const iflag_t *f, unsigned int bit)
16 unsigned int index = bit / 32;
17 return f->field[index] & (UINT32_C(1) << (bit - (index * 32)));
20 static inline void iflag_set(iflag_t *f, unsigned int bit)
22 unsigned int index = bit / 32;
23 f->field[index] |= (UINT32_C(1) << (bit - (index * 32)));
26 static inline void iflag_clear(iflag_t *f, unsigned int bit)
28 unsigned int index = bit / 32;
29 f->field[index] &= ~(UINT32_C(1) << (bit - (index * 32)));
32 static inline void iflag_clear_all(iflag_t *f)
34 memset(f, 0, sizeof(*f));
37 static inline void iflag_set_all(iflag_t *f)
39 memset(f, 0xff, sizeof(*f));
42 static inline int iflag_cmp(const iflag_t *a, const iflag_t *b)
44 int i;
46 for (i = sizeof(a->field) / sizeof(a->field[0]) - 1; i >= 0; i--) {
47 if (a->field[i] == b->field[i])
48 continue;
50 return (a->field[i] > b->field[i]) ? 1 : -1;
53 return 0;
56 static inline int iflag_cmp_cpu(const iflag_t *a, const iflag_t *b)
58 if (a->field[3] < b->field[3])
59 return -1;
60 else if (a->field[3] > b->field[3])
61 return 1;
62 return 0;
65 static inline unsigned int iflag_ffs(const iflag_t *a)
67 unsigned int i;
69 for (i = 0; i < sizeof(a->field) / sizeof(a->field[0]); i++) {
70 if (a->field[i])
71 return ilog2_32(a->field[i]) + (i * 32);
74 return 0;
77 #define IF_GEN_HELPER(name, op) \
78 static inline iflag_t iflag_##name(const iflag_t *a, const iflag_t *b) \
79 { \
80 unsigned int i; \
81 iflag_t res; \
83 for (i = 0; i < sizeof(a->field) / sizeof(a->field[0]); i++) \
84 res.field[i] = a->field[i] op b->field[i]; \
86 return res; \
89 IF_GEN_HELPER(xor, ^)
92 /* Use this helper to test instruction template flags */
93 #define itemp_has(itemp, bit) iflag_test(&insns_flags[(itemp)->iflag_idx], bit)
96 /* Maximum processor level at moment */
97 #define IF_PLEVEL IF_IA64
98 /* Some helpers which are to work with predefined masks */
99 #define IF_SMASK \
100 (IF_GENBIT(IF_SB) |\
101 IF_GENBIT(IF_SW) |\
102 IF_GENBIT(IF_SD) |\
103 IF_GENBIT(IF_SQ) |\
104 IF_GENBIT(IF_SO) |\
105 IF_GENBIT(IF_SY) |\
106 IF_GENBIT(IF_SZ) |\
107 IF_GENBIT(IF_SIZE))
108 #define IF_ARMASK \
109 (IF_GENBIT(IF_AR0) |\
110 IF_GENBIT(IF_AR1) |\
111 IF_GENBIT(IF_AR2) |\
112 IF_GENBIT(IF_AR3) |\
113 IF_GENBIT(IF_AR4))
115 #define _itemp_smask(idx) (insns_flags[(idx)].field[0] & IF_SMASK)
116 #define _itemp_armask(idx) (insns_flags[(idx)].field[0] & IF_ARMASK)
117 #define _itemp_arg(idx) ((_itemp_armask(idx) >> IF_AR0) - 1)
119 #define itemp_smask(itemp) _itemp_smask((itemp)->iflag_idx)
120 #define itemp_arg(itemp) _itemp_arg((itemp)->iflag_idx)
121 #define itemp_armask(itemp) _itemp_armask((itemp)->iflag_idx)
123 static inline int iflag_cmp_cpu_level(const iflag_t *a, const iflag_t *b)
125 iflag_t v1 = *a;
126 iflag_t v2 = *b;
128 iflag_clear(&v1, IF_CYRIX);
129 iflag_clear(&v1, IF_AMD);
131 iflag_clear(&v2, IF_CYRIX);
132 iflag_clear(&v2, IF_AMD);
134 if (v1.field[3] < v2.field[3])
135 return -1;
136 else if (v1.field[3] > v2.field[3])
137 return 1;
139 return 0;
142 static inline iflag_t _iflag_pfmask(const iflag_t *a)
144 iflag_t r;
146 iflag_clear_all(&r);
148 if (iflag_test(a, IF_CYRIX))
149 iflag_set(&r, IF_CYRIX);
150 if (iflag_test(a, IF_AMD))
151 iflag_set(&r, IF_AMD);
153 return r;
156 #define iflag_pfmask(itemp) _iflag_pfmask(&insns_flags[(itemp)->iflag_idx])
158 #endif /* NASM_IFLAG_H */