ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
utilities.h
Go to the documentation of this file.
1#ifndef _utilities_h_INCLUDED
2#define _utilities_h_INCLUDED
3
4#include <assert.h>
5#include <stdbool.h>
6#include <stdint.h>
7#include <stdlib.h>
8
9#ifdef _MSC_VER
10#include <intrin.h>
11static inline int __builtin_clz(unsigned x) {
12 unsigned long r;
13 _BitScanReverse(&r, x);
14 return (int)(r ^ 31);
15}
16
17static inline int __builtin_clzll(unsigned long long x) {
18#if defined(_WIN64)
19 unsigned long r;
20 _BitScanReverse64(&r, x);
21 return (int)(r ^ 63);
22#else
23 int l = __builtin_clz((unsigned)x) + 32;
24 int h = __builtin_clz((unsigned)(x >> 32));
25 return !!((unsigned)(x >> 32)) ? h : l;
26#endif
27}
28
29static inline int __builtin_clzl(unsigned long x) {
30 return sizeof(x) == 8 ? __builtin_clzll(x) : __builtin_clz((unsigned)x);
31}
32#endif
33
34#include "global.h"
36
37//typedef uintptr_t word;
38typedef uintptr_t w2rd[2];
39
40#define WORD_ALIGNMENT_MASK (sizeof (word) - 1)
41#define W2RD_ALIGNMENT_MASK (sizeof (w2rd) - 1)
42
43#define WORD_FORMAT PRIuPTR
44
45#define MAX_SIZE_T (~(size_t) 0)
46
47#define ASSUMED_LD_CACHE_LINE_BYTES 7u
48
49static inline word kissat_cache_lines (word n, size_t size) {
50 if (!n)
51 return 0;
52#ifdef KISSAT_NDEBUG
53 (void) size;
54#endif
55 KISSAT_assert (size == 4);
57 const unsigned shift = ASSUMED_LD_CACHE_LINE_BYTES - 2u;
58 const word mask = (((word) 1) << shift) - 1;
59 const word masked = n + mask;
60 const word res = masked >> shift;
61 return res;
62}
63
64static inline double kissat_average (double a, double b) {
65 return b ? a / b : 0.0;
66}
67
68static inline double kissat_percent (double a, double b) {
69 return kissat_average (100.0 * a, b);
70}
71
72static inline bool kissat_aligned_word (word word) {
73 return !(word & WORD_ALIGNMENT_MASK);
74}
75
76static inline bool kissat_aligned_pointer (const void *p) {
77 return kissat_aligned_word ((word) p);
78}
79
80static inline word kissat_align_word (word w) {
81 word res = w;
82 if (res & WORD_ALIGNMENT_MASK)
83 res = 1 + (res | WORD_ALIGNMENT_MASK);
84 return res;
85}
86
87static inline word kissat_align_w2rd (word w) {
88 word res = w;
89 if (res & W2RD_ALIGNMENT_MASK)
90 res = 1 + (res | W2RD_ALIGNMENT_MASK);
91 return res;
92}
93
94bool kissat_has_suffix (const char *str, const char *suffix);
95
96static inline bool kissat_is_power_of_two (uint64_t w) {
97 return w && !(w & (w - 1));
98}
99
100static inline bool kissat_is_zero_or_power_of_two (word w) {
101 return !(w & (w - 1));
102}
103
104static inline unsigned kissat_leading_zeroes_of_unsigned (unsigned x) {
105 return x ? __builtin_clz (x) : sizeof (unsigned) * 8;
106}
107
108static inline unsigned kissat_leading_zeroes_of_word (word x) {
109 if (!x)
110 return sizeof (word) * 8;
111 if (sizeof (word) == sizeof (unsigned long long))
112 return __builtin_clzll (x);
113 if (sizeof (word) == sizeof (unsigned long))
114 return __builtin_clzl (x);
115 return __builtin_clz (x);
116}
117
118static inline unsigned kissat_log2_floor_of_word (word x) {
119 return x ? sizeof (word) * 8 - 1 - kissat_leading_zeroes_of_word (x) : 0;
120}
121
122static inline unsigned kissat_log2_ceiling_of_word (word x) {
123 if (!x)
124 return 0;
125 unsigned tmp = kissat_log2_floor_of_word (x);
126 return tmp + !!(x ^ (((word) 1) << tmp));
127}
128
129static inline unsigned kissat_leading_zeroes_of_uint64 (uint64_t x) {
130 if (!x)
131 return sizeof (uint64_t) * 8;
132 if (sizeof (uint64_t) == sizeof (unsigned long long))
133 return __builtin_clzll (x);
134 if (sizeof (uint64_t) == sizeof (unsigned long))
135 return __builtin_clzl (x);
136 return __builtin_clz (x);
137}
138
139static inline unsigned kissat_log2_floor_of_uint64 (uint64_t x) {
140 return x ? sizeof (uint64_t) * 8 - 1 - kissat_leading_zeroes_of_uint64 (x)
141 : 0;
142}
143
144static inline unsigned kissat_log2_ceiling_of_uint64 (uint64_t x) {
145 if (!x)
146 return 0;
147 unsigned tmp = kissat_log2_floor_of_uint64 (x);
148 return tmp + !!(x ^ (((uint64_t) 1) << tmp));
149}
150
151#define SWAP(TYPE, A, B) \
152 do { \
153 TYPE TMP_SWAP = (A); \
154 (A) = (B); \
155 (B) = (TMP_SWAP); \
156 } while (0)
157
158#define MIN(A, B) ((A) > (B) ? (B) : (A))
159
160#define MAX(A, B) ((A) < (B) ? (B) : (A))
161
162#define ABS(A) (KISSAT_assert ((int) (A) != INT_MIN), (A) < 0 ? -(A) : (A))
163
165
166#endif
#define ABC_NAMESPACE_HEADER_END
#define ABC_NAMESPACE_HEADER_START
NAMESPACES ///.
Cube * p
Definition exorList.c:222
#define KISSAT_assert(ignore)
Definition global.h:13
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
ABC_NAMESPACE_HEADER_START typedef uintptr_t w2rd[2]
Definition utilities.h:38
#define WORD_ALIGNMENT_MASK
Definition utilities.h:40
#define W2RD_ALIGNMENT_MASK
Definition utilities.h:41
#define ASSUMED_LD_CACHE_LINE_BYTES
Definition utilities.h:47
bool kissat_has_suffix(const char *str, const char *suffix)
Definition utilities.c:7