ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
cadical_util.cpp
Go to the documentation of this file.
1#include "global.h"
2
3#include "internal.hpp"
4
6
7namespace CaDiCaL {
8
9/*------------------------------------------------------------------------*/
10
11bool parse_int_str (const char *val_str, int &val) {
12 if (!strcmp (val_str, "true"))
13 val = 1;
14 else if (!strcmp (val_str, "false"))
15 val = 0;
16 else {
17 const char *p = val_str;
18 int sign;
19
20 if (*p == '-')
21 sign = -1, p++;
22 else
23 sign = 1;
24
25 int ch;
26 if (!isdigit ((ch = *p++)))
27 return false;
28
29 const int64_t bound = -(int64_t) INT_MIN;
30 int64_t mantissa = ch - '0';
31
32 while (isdigit (ch = *p++)) {
33 if (bound / 10 < mantissa)
34 mantissa = bound;
35 else
36 mantissa *= 10;
37 const int digit = ch - '0';
38 if (bound - digit < mantissa)
39 mantissa = bound;
40 else
41 mantissa += digit;
42 }
43
44 int exponent = 0;
45 if (ch == 'e') {
46 while (isdigit ((ch = *p++)))
47 exponent = exponent ? 10 : ch - '0';
48 if (ch)
49 return false;
50 } else if (ch)
51 return false;
52
53 CADICAL_assert (exponent <= 10);
54 int64_t val64 = mantissa;
55 for (int i = 0; i < exponent; i++)
56 val64 *= 10;
57
58 if (sign < 0) {
59 val64 = -val64;
60 if (val64 < INT_MIN)
61 val64 = INT_MIN;
62 } else {
63 if (val64 > INT_MAX)
64 val64 = INT_MAX;
65 }
66
67 CADICAL_assert (INT_MIN <= val64);
68 CADICAL_assert (val64 <= INT_MAX);
69
70 val = val64;
71 }
72 return true;
73}
74
75/*------------------------------------------------------------------------*/
76
77bool has_suffix (const char *str, const char *suffix) {
78 size_t k = strlen (str), l = strlen (suffix);
79 return k > l && !strcmp (str + k - l, suffix);
80}
81
82bool has_prefix (const char *str, const char *prefix) {
83 for (const char *p = str, *q = prefix; *q; q++, p++)
84 if (*q != *p)
85 return false;
86 return true;
87}
88
89/*------------------------------------------------------------------------*/
90
91bool is_color_option (const char *arg) {
92 return !strcmp (arg, "--color") || !strcmp (arg, "--colors") ||
93 !strcmp (arg, "--colour") || !strcmp (arg, "--colours") ||
94 !strcmp (arg, "--color=1") || !strcmp (arg, "--colors=1") ||
95 !strcmp (arg, "--colour=1") || !strcmp (arg, "--colours=1") ||
96 !strcmp (arg, "--color=true") || !strcmp (arg, "--colors=true") ||
97 !strcmp (arg, "--colour=true") || !strcmp (arg, "--colours=true");
98}
99
100bool is_no_color_option (const char *arg) {
101 return !strcmp (arg, "--no-color") || !strcmp (arg, "--no-colors") ||
102 !strcmp (arg, "--no-colour") || !strcmp (arg, "--no-colours") ||
103 !strcmp (arg, "--color=0") || !strcmp (arg, "--colors=0") ||
104 !strcmp (arg, "--colour=0") || !strcmp (arg, "--colours=0") ||
105 !strcmp (arg, "--color=false") ||
106 !strcmp (arg, "--colors=false") ||
107 !strcmp (arg, "--colour=false") ||
108 !strcmp (arg, "--colours=false");
109}
110
111/*------------------------------------------------------------------------*/
112
113static uint64_t primes[] = {
114 1111111111111111111lu, 2222222222222222249lu, 3333333333333333347lu,
115 4444444444444444537lu, 5555555555555555621lu, 6666666666666666677lu,
116 7777777777777777793lu, 8888888888888888923lu, 9999999999999999961lu,
117};
118
119uint64_t hash_string (const char *str) {
120 const unsigned size = sizeof primes / sizeof *primes;
121 uint64_t res = 0;
122 unsigned char ch;
123 unsigned i = 0;
124 for (const char *p = str; (ch = *p); p++) {
125 res += ch;
126 res *= primes[i++];
127 if (i == size)
128 i = 0;
129 }
130 return res;
131}
132
133} // namespace CaDiCaL
134
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
#define CADICAL_assert(ignore)
Definition global.h:14
Cube * p
Definition exorList.c:222
bool has_suffix(const char *str, const char *suffix)
int sign(int lit)
Definition util.hpp:22
bool is_color_option(const char *arg)
bool parse_int_str(const char *val_str, int &val)
bool has_prefix(const char *str, const char *prefix)
uint64_t hash_string(const char *str)
bool is_no_color_option(const char *arg)
int strlen()
int strcmp()