mirror of
https://github.com/openhwgroup/cvw
synced 2025-02-11 06:05:49 +00:00
update fpcalc.c to match book and lex 0x or x prefix for inputs. Also improve the output for f128 to match half/single/double
This commit is contained in:
parent
37d7305fef
commit
d714a52cb6
@ -224,8 +224,15 @@ __uint128_t strtoul128(char *num, int base) {
|
||||
}
|
||||
|
||||
__uint128_t parseNum(char *num) {
|
||||
// uint64_t result;
|
||||
// uint64_t result;
|
||||
__uint128_t result;
|
||||
|
||||
// Ensure input starts with "0x" or "x"
|
||||
if (!(num[0] == '0' && num[1] == 'x') && !(num[0] == 'x')) {
|
||||
printf("Error: Input must start with '0x' or 'x'\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int size; // size of operands in bytes (2= half, 4=single, 8 = double)
|
||||
if (strlen(num) < 8) size = 2;
|
||||
else if (strlen(num) < 16) size = 4;
|
||||
@ -244,7 +251,34 @@ __uint128_t parseNum(char *num) {
|
||||
opSize = size;
|
||||
//printf ("Operand size is %d\n", opSize);
|
||||
}
|
||||
result = (__uint128_t)strtoul128(num, 16);
|
||||
|
||||
// Add to handle either input preference
|
||||
// Strip the "x" or "0x" prefixes if present
|
||||
if (num[0] == 'x' || (num[0] == '0' && num[1] == 'x')) {
|
||||
num += (num[0] == 'x') ? 1 : 2;
|
||||
}
|
||||
|
||||
if (strlen(num) <= 16) {
|
||||
result = (__uint128_t)strtoull(num, NULL, 16);
|
||||
}
|
||||
else {
|
||||
while (*num) {
|
||||
int value;
|
||||
if (*num >= '0' && *num <= '9')
|
||||
value = *num - '0';
|
||||
else if (*num >= 'a' && *num <= 'f')
|
||||
value = *num - 'a' + 10;
|
||||
else if (*num >= 'A' && *num <= 'F')
|
||||
value = *num - 'A' + 10;
|
||||
else {
|
||||
printf("Error: invalid character in input\n");
|
||||
exit(1);
|
||||
}
|
||||
result = (result << 4) | value;
|
||||
num++;
|
||||
}
|
||||
}
|
||||
|
||||
//printf("Parsed %s as 0x%lx\n", num, result);
|
||||
return result;
|
||||
}
|
||||
@ -387,8 +421,10 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
printF128("X", x); printF128("Y", y);
|
||||
//sprintf(cmd, "0x%016lx %c 0x%016lx", x.v, op1, y.v);
|
||||
printf("0x%016" PRIx64 "_%016" PRIx64 " %c ", x.v[1], x.v[0], op1);
|
||||
printf("0x%016" PRIx64 "_%016" PRIx64 " ", y.v[1], y.v[0]);
|
||||
printF128(cmd, r); printFlags();
|
||||
printF128val(r);
|
||||
// printF128val(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user