update fpcalc to recognize 0x, x, or just hex

This commit is contained in:
James Stine 2024-10-01 15:46:35 -05:00
parent df2dd3f887
commit bcd6f3da16

View File

@ -224,20 +224,20 @@ __uint128_t strtoul128(char *num, int base) {
} }
__uint128_t parseNum(char *num) { __uint128_t parseNum(char *num) {
// uint64_t result; __uint128_t result = 0;
__uint128_t result;
// Ensure input starts with "0x" or "x" // Ensure input is in correct form
if (!(num[0] == '0' && num[1] == 'x') && !(num[0] == 'x')) { if (num[0] == '0' && num[1] == 'x') {
printf("Error: Input must start with '0x' or 'x'\n"); num += 2; // Skip "0x"
exit(1); } else if (num[0] == 'x') {
num += 1; // Skip "x"
} }
int size; // size of operands in bytes (2= half, 4=single, 8 = double) int size; // size of operands in bytes (2= half, 4=single, 8 = double)
if (strlen(num) < 8) size = 2; if (strlen(num) < 8) size = 2;
else if (strlen(num) < 16) size = 4; else if (strlen(num) < 16) size = 4;
else if (strlen(num) < 32) size = 8; else if (strlen(num) < 32) size = 8;
else if (strlen(num) < 35) size = 16; // *** will need to increase else if (strlen(num) < 35) size = 16; // *** will need to increase
else { else {
printf("Error: only half, single, double, or quad precision supported"); printf("Error: only half, single, double, or quad precision supported");
exit(1); exit(1);
@ -246,17 +246,11 @@ __uint128_t parseNum(char *num) {
if (size != opSize) { if (size != opSize) {
printf("Error: inconsistent operand sizes %d and %d\n", size, opSize); printf("Error: inconsistent operand sizes %d and %d\n", size, opSize);
exit(1); exit(1);
} }
} else { } else {
opSize = size; opSize = size;
} }
// 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) { if (strlen(num) <= 16) {
result = (__uint128_t)strtoull(num, NULL, 16); result = (__uint128_t)strtoull(num, NULL, 16);
} }
@ -302,8 +296,7 @@ char parseRound(char *rnd) {
} }
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{
//uint64_t xn, yn, zn; //uint64_t xn, yn, zn;
__uint128_t xn, yn, zn; __uint128_t xn, yn, zn;
char op1, op2; char op1, op2;