Merge fix16.cpp and fix16_str.cpp

git-svn-id: https://svn.eduke32.com/eduke32@8709 1a8010ca-5511-0410-912e-c29ae57300e0

# Conflicts:
#	GNUmakefile
#	platform/Windows/build.vcxproj
#	platform/Windows/build.vcxproj.filters
#	source/thirdparty/src/fix16_str.cpp
This commit is contained in:
terminx 2020-03-12 00:57:25 +00:00 committed by Christoph Oelckers
parent 88502f9962
commit 05b59467f5
2 changed files with 255 additions and 259 deletions

View file

@ -252,3 +252,113 @@ fix16_t fix16_lerp32(fix16_t inArg0, fix16_t inArg1, uint32_t inFract)
tempOut >>= 32; tempOut >>= 32;
return (fix16_t)tempOut; return (fix16_t)tempOut;
} }
static const uint32_t scales[8] = {
/* 5 decimals is enough for full fix16_t precision */
1, 10, 100, 1000, 10000, 100000, 100000, 100000
};
static char *itoa_loop(char *buf, uint32_t scale, uint32_t value, bool skip)
{
while (scale)
{
unsigned digit = (value / scale);
if (!skip || digit || scale == 1)
{
skip = false;
*buf++ = '0' + digit;
value %= scale;
}
scale /= 10;
}
return buf;
}
void fix16_to_str(fix16_t value, char *buf, int decimals)
{
uint32_t uvalue = (value >= 0) ? value : -value;
if (value < 0)
*buf++ = '-';
/* Separate the integer and decimal parts of the value */
unsigned intpart = uvalue >> 16;
uint32_t fracpart = uvalue & 0xFFFF;
uint32_t scale = scales[decimals & 7];
fracpart = fix16_mul(fracpart, scale);
if (fracpart >= scale)
{
/* Handle carry from decimal part */
intpart++;
fracpart -= scale;
}
/* Format integer part */
buf = itoa_loop(buf, 10000, intpart, true);
/* Format decimal part (if any) */
if (scale != 1)
{
*buf++ = '.';
buf = itoa_loop(buf, scale / 10, fracpart, false);
}
*buf = '\0';
}
fix16_t fix16_from_str(const char *buf)
{
while (isspace(*buf))
buf++;
/* Decode the sign */
bool negative = (*buf == '-');
if (*buf == '+' || *buf == '-')
buf++;
/* Decode the integer part */
uint32_t intpart = 0;
int count = 0;
while (isdigit(*buf))
{
intpart *= 10;
intpart += *buf++ - '0';
count++;
}
if (count == 0 || count > 5
|| intpart > 32768 || (!negative && intpart > 32767))
return FIX16_OVERFLOW;
fix16_t value = intpart << 16;
/* Decode the decimal part */
if (*buf == '.' || *buf == ',')
{
buf++;
uint32_t fracpart = 0;
uint32_t scale = 1;
while (isdigit(*buf) && scale < 100000)
{
scale *= 10;
fracpart *= 10;
fracpart += *buf++ - '0';
}
value += fix16_div(fracpart, scale);
}
/* Verify that there is no garbage left over */
while (*buf != '\0')
{
if (!isdigit(*buf) && !isspace(*buf))
return FIX16_OVERFLOW;
buf++;
}
return negative ? -value : value;
}

View file

@ -1,114 +0,0 @@
#include "fix16.h"
#include <stdbool.h>
#include <ctype.h>
static const uint32_t scales[8] = {
/* 5 decimals is enough for full fix16_t precision */
1, 10, 100, 1000, 10000, 100000, 100000, 100000
};
static char *itoa_loop(char *buf, uint32_t scale, uint32_t value, bool skip)
{
while (scale)
{
unsigned digit = (value / scale);
if (!skip || digit || scale == 1)
{
skip = false;
*buf++ = '0' + digit;
value %= scale;
}
scale /= 10;
}
return buf;
}
void fix16_to_str(fix16_t value, char *buf, int decimals)
{
uint32_t uvalue = (value >= 0) ? value : -value;
if (value < 0)
*buf++ = '-';
/* Separate the integer and decimal parts of the value */
unsigned intpart = uvalue >> 16;
uint32_t fracpart = uvalue & 0xFFFF;
uint32_t scale = scales[decimals & 7];
fracpart = fix16_mul(fracpart, scale);
if (fracpart >= scale)
{
/* Handle carry from decimal part */
intpart++;
fracpart -= scale;
}
/* Format integer part */
buf = itoa_loop(buf, 10000, intpart, true);
/* Format decimal part (if any) */
if (scale != 1)
{
*buf++ = '.';
buf = itoa_loop(buf, scale / 10, fracpart, false);
}
*buf = '\0';
}
fix16_t fix16_from_str(const char *buf)
{
while (isspace(*buf))
buf++;
/* Decode the sign */
bool negative = (*buf == '-');
if (*buf == '+' || *buf == '-')
buf++;
/* Decode the integer part */
uint32_t intpart = 0;
int count = 0;
while (isdigit(*buf))
{
intpart *= 10;
intpart += *buf++ - '0';
count++;
}
if (count == 0 || count > 5
|| intpart > 32768 || (!negative && intpart > 32767))
return FIX16_OVERFLOW;
fix16_t value = intpart << 16;
/* Decode the decimal part */
if (*buf == '.' || *buf == ',')
{
buf++;
uint32_t fracpart = 0;
uint32_t scale = 1;
while (isdigit(*buf) && scale < 100000)
{
scale *= 10;
fracpart *= 10;
fracpart += *buf++ - '0';
}
value += fix16_div(fracpart, scale);
}
/* Verify that there is no garbage left over */
while (*buf != '\0')
{
if (!isdigit(*buf) && !isspace(*buf))
return FIX16_OVERFLOW;
buf++;
}
return negative ? -value : value;
}