This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
fpc.c
243 lines (225 loc) · 6.26 KB
/
fpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "fpc.h"
#define LENGTH(x) (sizeof(x)/sizeof(x[0]))
static
int floor_log2l(long double x) {
int exp;
frexpl(x, &exp);
return exp - 1;
}
static
unsigned int int_log2(unsigned int x) {
return x <= 1 ? 0 : (8 * sizeof(x)) - __builtin_clz(x - 1);
}
static
int clz128(int128_t x) {
unsigned int y;
unsigned int shift = sizeof(y) * 8;
int i, n = sizeof(x) / sizeof(y);
for(i = 0; i < n; i++) {
y = x >> (shift * (n - 1 - i));
if(y) return __builtin_clz(y) + shift * i;
}
return shift * n;
}
static
unsigned int int128_log2(int128_t x) {
return x <= 1 ? 0 : 128 - clz128(x - 1);
}
bool fpc_calculate(struct fpc_parameters *param) {
if(param->max < param->min + param->precision) {
param->error = "max < min + precision";
return false;
}
if(param->precision <= 0.0L) {
param->error = "zero or negative precision";
return false;
}
param->fractional_bits = -floor_log2l(param->precision);
param->lower_bound = ceill(ldexpl(param->min - param->precision / 2, param->fractional_bits));
param->upper_bound = floorl(ldexpl(param->max + param->precision / 2, param->fractional_bits));
param->fixed_encoding_width = int128_log2(param->upper_bound - param->lower_bound + 1);
param->integer_bits = param->fixed_encoding_width - param->fractional_bits;
if(param->fixed_encoding_width > 64) {
param->error = "fixed_encoding_width > 64";
return false;
}
if(param->fixed_encoding_width < 8) { // could disable this lower bound to support packed formats
param->fixed_encoding_width = 8;
} else {
param->fixed_encoding_width = 1 << int_log2(param->fixed_encoding_width);
}
param->offset = param->lower_bound;
param->large_offset = false;
if(param->offset > (((int128_t)1) << 63) - 1 ||
param->offset < -(((int128_t)1) << 63)) {
param->large_offset = true;
}
param->use_signed = false;
if(param->min < 0.0L) {
if(param->upper_bound <= (((int128_t)1) << (param->fixed_encoding_width - 1)) - 1 &&
param->lower_bound >= (((int128_t)-1) << (param->fixed_encoding_width - 1))) {
param->offset = 0;
param->use_signed = true;
}
} else {
if(param->upper_bound <= (((int128_t)1) << param->fixed_encoding_width) - 1) {
param->offset = 0;
}
}
return true;
}
static
const char *get_op(char c) {
static const char *ops = "+a-a*b/b^c(())";
const char *op = ops;
while(*op) {
if(*op == c) {
return op;
}
op += 2;
}
return NULL;
}
static char vars[16];
static long double values[sizeof(vars)];
static unsigned int n_vars = 0;
void fpc_set_var(char c, long double x) {
if(n_vars < sizeof(vars)) {
int n = n_vars++;
vars[n] = c;
values[n] = x;
}
}
long double *fpc_get_var(char c) {
char *v = strchr(vars, c);
return v ? &values[v - vars] : NULL;
}
static
bool do_op(char op, long double *arg) {
switch(op) {
case '+': arg[0] += arg[1]; break;
case '-': arg[0] -= arg[1]; break;
case '*': arg[0] *= arg[1]; break;
case '/': arg[0] /= arg[1]; break;
case '^': arg[0] = powl(arg[0], arg[1]); break;
case '(': return false;
default: arg[0] = NAN; break;
}
return true;
}
static
long double parse_num(char **pstr) {
char *p = *pstr;
long double val = strtold(*pstr, pstr);
if(*pstr != p) return val;
(*pstr)++;
long double *var = fpc_get_var(*p);
return var ? *var : NAN;
}
/* expression parser based on the shunting-yard algorithm */
long double fpc_eval_expr(char *str) {
long double args[32];
const char *ops[32];
unsigned int arg_top = 0;
unsigned int op_top = 0;
bool expect_num = true;
char *p = str;
const char *op;
while(*p) {
while(*p == ' ') p++;
op = get_op(*p);
while(*p == ' ') p++;
if(!op) {
if(!expect_num) return NAN;
if(arg_top >= LENGTH(args)) return NAN;
args[arg_top++] = parse_num(&p);
expect_num = false;
} else if(*op == '(') {
p++;
if(!expect_num) return NAN;
ops[op_top++] = op;
} else {
p++;
if(expect_num) {
if(*op == '-') {
args[arg_top++] = -1.0L;
op += 2;
goto checks;
} else return NAN;
}
while(op_top &&
op[1] <= (ops[op_top - 1])[1] &&
do_op(*(ops[op_top - 1]), &args[arg_top - 2])) {
arg_top--;
op_top--;
}
checks:
if(*op == ')') {
if(!op_top) return NAN;
op_top--;
expect_num = false;
} else {
if(op_top >= LENGTH(ops)) return NAN;
ops[op_top++] = op;
expect_num = true;
}
}
}
if(expect_num) return NAN;
while(op_top &&
do_op(*(ops[op_top - 1]), &args[arg_top - 2])) {
arg_top--;
op_top--;
}
if(op_top) return NAN;
return args[0];
}
bool fpc_calculate_from_strings(char *min,
char *max,
char *precision,
struct fpc_parameters *param) {
struct entry {
char *expr;
long double *dest;
char var;
bool complete;
} entries[] = {
{ .expr = min, .dest = ¶m->min, .var = 'l'},
{ .expr = max, .dest = ¶m->max, .var = 'h'},
{ .expr = precision, .dest = ¶m->precision, .var = 'p'}
};
unsigned int i, left = LENGTH(entries);
bool progress;
do {
progress = false;
for(i = 0; i < LENGTH(entries); i++) {
struct entry *e = &entries[i];
if(e->complete) continue;
long double x = fpc_eval_expr(e->expr);
if(!isnan(x)) {
progress = true;
left--;
e->complete = true;
*(e->dest) = x;
fpc_set_var(e->var, x);
}
}
} while(left && progress);
return fpc_calculate(param);
}