1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/libc/stdlib/strtof.c

200 lines
4.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2016 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* stdlib/strtof.c
* Converts floating numbers represented as strings to binary representation.
*/
2013-03-23 14:20:44 -04:00
#ifndef STRTOF
#define STRTOF_FLOAT float
2013-03-23 14:20:44 -04:00
#define STRTOF strtof
#define STRTOF_CHAR char
2016-02-27 08:12:36 -05:00
#define STRTOF_CTYPE_CHAR unsigned char
#define STRTOF_L(x) x
2016-02-27 08:12:36 -05:00
#define STRTOF_ISSPACE isspace
#define STRTOF_STRNCASECMP strncasecmp
#define STRTOF_POW powf
2013-03-23 14:20:44 -04:00
#endif
2016-02-27 08:12:36 -05:00
#include <ctype.h>
#include <errno.h>
#include <math.h>
2016-02-28 06:11:02 -05:00
#include <stdbool.h>
2013-03-23 14:20:44 -04:00
#include <stdlib.h>
2016-02-27 08:12:36 -05:00
#include <string.h>
#include <wchar.h>
2016-02-27 08:12:36 -05:00
#include <wctype.h>
2013-03-23 14:20:44 -04:00
2016-02-27 08:12:36 -05:00
// TODO: This is an imperfect implementation that doesn't try to minimize loss
// and explicitly handle overflow conditions. It behaves well enough on
// simple common inputs to do for now.
2013-03-23 14:20:44 -04:00
2016-02-28 06:11:02 -05:00
#ifndef __is_sortix_libk
2016-02-27 08:12:36 -05:00
static inline size_t nan_parameter(const STRTOF_CHAR* str)
{
size_t index = 0;
if ( str[index++] != STRTOF_L('(') )
return 0;
// TODO: What exactly does digit and nondigit mean here in POSIX?
if ( !str[index] || str[index] == STRTOF_L(')') )
return 0;
while ( str[index] && str[index] != STRTOF_L(')') )
index++;
if ( str[index++] != STRTOF_L(')') )
return 0;
return index;
}
static inline bool is_hexadecimal_float(const STRTOF_CHAR* str)
{
if ( *str++ != '0' )
return false;
if ( *str != 'x' && *str != 'X' )
return false;
str++;
if ( *str == '.' )
str++;
return isxdigit((STRTOF_CTYPE_CHAR) *str);
}
static inline bool is_exponent(const STRTOF_CHAR* str,
STRTOF_CHAR elc,
STRTOF_CHAR euc)
{
if ( *str != elc && *str != euc )
return false;
str++;
if ( *str == '-' || *str == '+' )
str++;
return STRTOF_L('0') <= *str && *str <= STRTOF_L('9');
}
static inline bool parse_digit(const STRTOF_CHAR* str,
int base,
STRTOF_FLOAT* out)
{
if ( STRTOF_L('0') <= *str && *str <= STRTOF_L('9') )
return *out = *str - STRTOF_L('0') , true;
if ( base == 16 )
{
if ( STRTOF_L('a') <= *str && *str <= STRTOF_L('f') )
return *out = 10 + *str - STRTOF_L('a') , true;
if ( STRTOF_L('A') <= *str && *str <= STRTOF_L('F') )
return *out = 10 + *str - STRTOF_L('A') , true;
}
return false;
}
2016-02-28 06:11:02 -05:00
STRTOF_FLOAT STRTOF(const STRTOF_CHAR* str, STRTOF_CHAR** nptr)
2013-03-23 14:20:44 -04:00
{
2016-02-27 08:12:36 -05:00
if ( nptr )
*nptr = (STRTOF_CHAR*) str;
2013-03-23 14:20:44 -04:00
2016-02-27 08:12:36 -05:00
while ( *str && STRTOF_ISSPACE((STRTOF_CTYPE_CHAR) *str) )
str++;
if ( !STRTOF_STRNCASECMP(str, STRTOF_L("INF"), 3) )
2013-03-23 14:20:44 -04:00
{
2016-02-27 08:12:36 -05:00
str += 3;
if ( !STRTOF_STRNCASECMP(str, STRTOF_L("INITY"), 5) )
str += 5;
2013-03-23 14:20:44 -04:00
if ( nptr )
*nptr = (STRTOF_CHAR*) str;
2016-02-27 08:12:36 -05:00
return INFINITY;
2013-03-23 14:20:44 -04:00
}
2016-02-27 08:12:36 -05:00
if ( !STRTOF_STRNCASECMP(str, STRTOF_L("NAN"), 3) )
{
str += 3;
str += nan_parameter(str);
if ( nptr )
*nptr = (STRTOF_CHAR*) str;
return NAN;
}
bool negative = *str == STRTOF_L('-');
if ( *str == STRTOF_L('-') )
str++;
else if ( *str == STRTOF_L('+') )
str++;
int base = 10;
STRTOF_CHAR elc = 'e';
STRTOF_CHAR euc = 'E';
if ( is_hexadecimal_float(str) )
2013-03-23 14:20:44 -04:00
{
str += 2;
2016-02-27 08:12:36 -05:00
base = 16;
elc = 'p';
euc = 'P';
2013-03-23 14:20:44 -04:00
}
2016-02-27 08:12:36 -05:00
bool any_digits = false;
STRTOF_FLOAT result = 0.0;
STRTOF_FLOAT add;
while ( parse_digit(str, base, &add) )
{
str++;
result = base * result + add;
any_digits = true;
}
if ( *str == STRTOF_L('.' ) )
2013-03-23 14:20:44 -04:00
{
2016-02-27 08:12:36 -05:00
str++;
STRTOF_FLOAT magnitude = 1.0;
while ( parse_digit(str, base, &add) )
2013-03-23 14:20:44 -04:00
{
2016-02-27 08:12:36 -05:00
str++;
magnitude /= base;
result += add * magnitude;
any_digits = true;
2013-03-23 14:20:44 -04:00
}
}
2016-02-27 08:12:36 -05:00
if ( !any_digits )
return errno = EINVAL, 0;
2013-03-23 14:20:44 -04:00
2016-02-27 08:12:36 -05:00
if ( is_exponent(str, elc, euc) )
2013-03-23 14:20:44 -04:00
{
2016-02-27 08:12:36 -05:00
str++;
bool exponent_negative = *str == STRTOF_L('-');
if ( *str == STRTOF_L('-') )
str++;
else if ( *str == STRTOF_L('+') )
str++;
STRTOF_FLOAT exponent = 0.0;
while ( parse_digit(str, 10, &add) )
{
str++;
exponent = 10.0 * exponent + add;
any_digits = true;
}
if ( exponent_negative )
exponent = -exponent;
result *= STRTOF_POW(base == 16 ? 2 : base, exponent);
2013-03-23 14:20:44 -04:00
}
2016-02-27 08:12:36 -05:00
if ( negative )
result = -result;
if ( nptr )
*nptr = (STRTOF_CHAR*) str;
return result;
2013-03-23 14:20:44 -04:00
}
#endif