ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
format.c
Go to the documentation of this file.
1#include "format.h"
2
3#include <assert.h>
4#include <inttypes.h>
5#include <limits.h>
6#include <math.h>
7#include <stdio.h>
8#include <string.h>
9
11
14 char *res = format->str[format->pos++];
15 if (format->pos == NUM_FORMAT_STRINGS)
16 format->pos = 0;
17 return res;
18}
19
20static void format_count (char *res, uint64_t w) {
21 if (w >= 128 && kissat_is_power_of_two (w)) {
22 unsigned l;
23 for (l = 0; ((uint64_t) 1 << l) != w; l++)
24 KISSAT_assert (l + 1 < 8 * sizeof (word));
25 sprintf (res, "2^%u", l);
26 } else if (w >= 1000 && !(w % 1000)) {
27 unsigned l;
28 for (l = 0; !(w % 10); l++)
29 w /= 10;
30 sprintf (res, "%" PRIu64 "e%u", w, l);
31 } else
32 sprintf (res, "%" PRIu64, w);
33}
34
35const char *kissat_format_count (kormat *format, uint64_t w) {
36 char *res = kissat_next_format_string (format);
37 format_count (res, w);
38 return res;
39}
40
41const char *kissat_format_value (kormat *format, bool boolean, int value) {
42 if (boolean && value)
43 return "true";
44 if (boolean && !value)
45 return "false";
46 if (value == INT_MAX)
47 return "INT_MAX";
48 if (value == INT_MIN)
49 return "INT_MIN";
50 char *res = kissat_next_format_string (format);
51 if (value < 0) {
52 *res = '-';
53 format_count (res + 1, ABS (value));
54 } else
55 format_count (res, value);
56 return res;
57}
58
59const char *kissat_format_bytes (kormat *format, uint64_t bytes) {
60 char *res = kissat_next_format_string (format);
61 if (bytes < (1u << 10))
62 sprintf (res, "%" PRIu64 " bytes", bytes);
63 else if (bytes < (1u << 20))
64 sprintf (res, "%" PRIu64 " bytes (%" PRIu64 " KB)", bytes,
65 (bytes + (1 << 9)) >> 10);
66 else if (bytes < (1u << 30))
67 sprintf (res, "%" PRIu64 " bytes (%" PRIu64 " MB)", bytes,
68 (bytes + (1u << 19)) >> 20);
69 else
70 sprintf (res, "%" PRIu64 " bytes (%" PRIu64 " GB)", bytes,
71 (bytes + (1u << 29)) >> 30);
72 return res;
73}
74
75const char *kissat_format_time (kormat *format, double seconds) {
76 if (!seconds)
77 return "0s";
78 char *res = kissat_next_format_string (format);
79 uint64_t rounded = round (seconds);
80 uint64_t minutes = rounded / 60;
81 rounded %= 60;
82 uint64_t hours = minutes / 60;
83 minutes %= 60;
84 uint64_t days = hours / 24;
85 hours %= 24;
86 char *tmp = res;
87 if (days) {
88 sprintf (res, "%" PRIu64 "d", days);
89 tmp += strlen (res);
90 }
91 if (hours) {
92 if (tmp != res)
93 *tmp++ = ' ';
94 sprintf (tmp, "%" PRIu64 "h", hours);
95 tmp += strlen (tmp);
96 }
97 if (minutes) {
98 if (tmp != res)
99 *tmp++ = ' ';
100 sprintf (tmp, "%" PRIu64 "m", minutes);
101 tmp += strlen (tmp);
102 }
103 if (rounded) {
104 if (tmp != res)
105 *tmp++ = ' ';
106 sprintf (tmp, "%" PRIu64 "s", rounded);
107 }
108 return res;
109}
110
111const char *kissat_format_signs (kormat *format, unsigned size,
112 word signs) {
113 char *res = kissat_next_format_string (format);
115 char *p = res;
116 word bit = 1;
117 for (unsigned i = 0; i < size; i++, bit <<= 1)
118 *p++ = (bit & signs) ? '1' : '0';
119 *p = 0;
120 return res;
121}
122
123const char *kissat_format_ordinal (kormat *format, uint64_t ordinal) {
124 char const *suffix;
125 unsigned mod100 = ordinal % 100;
126 if (10 <= mod100 && mod100 <= 19)
127 suffix = "th";
128 else {
129 switch (mod100 % 10) {
130 case 1:
131 suffix = "st";
132 break;
133 case 2:
134 suffix = "nd";
135 break;
136 case 3:
137 suffix = "rd";
138 break;
139 default:
140 suffix = "th";
141 break;
142 }
143 }
144 char *res = kissat_next_format_string (format);
145 sprintf (res, "%" PRIu64 "%s", ordinal, suffix);
146 return res;
147}
148
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
ABC_NAMESPACE_IMPL_START typedef signed char value
Cube * p
Definition exorList.c:222
const char * kissat_format_value(kormat *format, bool boolean, int value)
Definition format.c:41
const char * kissat_format_signs(kormat *format, unsigned size, word signs)
Definition format.c:111
const char * kissat_format_time(kormat *format, double seconds)
Definition format.c:75
const char * kissat_format_count(kormat *format, uint64_t w)
Definition format.c:35
const char * kissat_format_ordinal(kormat *format, uint64_t ordinal)
Definition format.c:123
const char * kissat_format_bytes(kormat *format, uint64_t bytes)
Definition format.c:59
ABC_NAMESPACE_IMPL_START char * kissat_next_format_string(kormat *format)
Definition format.c:12
#define NUM_FORMAT_STRINGS
Definition format.h:12
#define FORMAT_STRING_SIZE
Definition format.h:13
#define KISSAT_assert(ignore)
Definition global.h:13
unsigned __int64 word
DECLARATIONS ///.
Definition kitPerm.c:36
unsigned pos
Definition format.h:18
char str[NUM_FORMAT_STRINGS][FORMAT_STRING_SIZE]
Definition format.h:19
int strlen()
char * sprintf()
#define ABS(a)
Definition util_old.h:250