Skip to content

Commit 3b0741f

Browse files
committed
Fixed the abs overloads for integer and float types; Fixed fmaxl and fminl linking; Cleaned up strtof/strtold; Added strtoimax/strtoumax
1 parent 8bb23f8 commit 3b0741f

File tree

15 files changed

+207
-101
lines changed

15 files changed

+207
-101
lines changed

src/libc/difftime.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <time.h>
2-
#include <stdio.h>
32

43
double difftime(time_t end, time_t beginning)
54
{

src/libc/fmax.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ float fmaxf(float x, float y) {
1313

1414
double fmax(double, double) __attribute__((alias("fmaxf")));
1515

16+
#ifdef fmaxl
17+
#undef fmaxl
18+
#endif
19+
1620
long double fmaxl(long double x, long double y) {
1721
return
1822
isless(x, y) ? y :
@@ -23,3 +27,5 @@ long double fmaxl(long double x, long double y) {
2327
/* arguments are equal or signed zero */
2428
signbit(x) ? y : x;
2529
}
30+
31+
long double _debug_fmaxl(long double, long double) __attribute__((alias("fmaxl")));

src/libc/fmin.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ float fminf(float x, float y) {
1313

1414
double fmin(double, double) __attribute__((alias("fminf")));
1515

16+
#ifdef fminl
17+
#undef fminl
18+
#endif
19+
1620
long double fminl(long double x, long double y) {
1721
return
1822
isless(x, y) ? x :
@@ -23,3 +27,5 @@ long double fminl(long double x, long double y) {
2327
/* arguments are equal or signed zero */
2428
signbit(x) ? x : y;
2529
}
30+
31+
long double _debug_fminl(long double, long double) __attribute__((alias("fminl")));

src/libc/include/inttypes.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,31 @@
170170
#define SCNuPTR __UINTPTR_FMTu__
171171
#define SCNxPTR __UINTPTR_FMTx__
172172

173+
typedef struct {
174+
intmax_t rem;
175+
intmax_t quot;
176+
} imaxdiv_t;
177+
173178
__BEGIN_DECLS
174179

175180
extern intmax_t imaxabs(intmax_t __n)
176181
__NOEXCEPT __attribute__((__const__));
177182

178-
typedef struct {
179-
intmax_t rem;
180-
intmax_t quot;
181-
} imaxdiv_t;
182183
extern imaxdiv_t imaxdiv(intmax_t __numer, intmax_t __denom)
183184
__NOEXCEPT __attribute__((__const__));
184185

186+
intmax_t strtoimax(
187+
const char *__restrict nptr,
188+
char **__restrict endptr,
189+
int base
190+
) __attribute__((nonnull(1)));
191+
192+
uintmax_t strtoumax(
193+
const char *__restrict nptr,
194+
char **__restrict endptr,
195+
int base
196+
) __attribute__((nonnull(1)));
197+
185198
__END_DECLS
186199

187200
#endif /* _INTTYPES_H */

src/libc/include/math.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,12 @@ double expm1(double);
175175
float expm1f(float);
176176
long double expm1l(long double);
177177

178+
#ifndef _ABS_FLOAT_DEFINED
179+
#define _ABS_FLOAT_DEFINED
178180
double fabs(double);
179181
float fabsf(float);
180182
long double fabsl(long double);
183+
#endif /* _ABS_FLOAT_DEFINED */
181184

182185
double fdim(double, double);
183186
float fdimf(float, float);
@@ -353,6 +356,10 @@ long double _debug_fabsl(long double x);
353356
#define fabsl _debug_fabsl
354357
long double _debug_copysignl(long double x, long double y);
355358
#define copysignl _debug_copysignl
359+
long double _debug_fmaxl(long double x, long double y);
360+
#define fmaxl _debug_fmaxl
361+
long double _debug_fminl(long double x, long double y);
362+
#define fminl _debug_fminl
356363
long double _debug_truncl(long double x);
357364
#define truncl _debug_truncl
358365
long double _debug_floorl(long double x);

src/libc/include/stdlib.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ void quick_exit(int) __NOEXCEPT __attribute__((noreturn));
9393

9494
void _Exit(int) __NOEXCEPT __attribute__((noreturn));
9595

96+
#ifndef _ABS_INT_DEFINED
97+
#define _ABS_INT_DEFINED
9698
int abs(int n);
97-
9899
long labs(long n);
99-
100100
long long llabs(long long n);
101+
#endif /* _ABS_INT_DEFINED */
101102

102103
div_t div(int numer, int denom);
103104

src/libc/sqrtl.src

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
assume adl=1
2+
23
section .text
34

45
public _sqrtl

src/libc/strtof.c

Lines changed: 85 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
/* San Jose, California */
77
/* */
88
/************************************************************************/
9+
10+
#include <stdbool.h>
11+
#include <stdint.h>
912
#include <stdlib.h>
1013
#include <ctype.h>
1114
#include <stdio.h>
@@ -14,6 +17,11 @@
1417
#include <math.h>
1518
#include <errno.h>
1619

20+
typedef union F32_pun {
21+
float flt;
22+
uint32_t bin;
23+
} F32_pun;
24+
1725
/*************************************************
1826
*
1927
* strtod - string to double conversion
@@ -27,94 +35,98 @@
2735
* the value of the number
2836
*
2937
*************************************************/
30-
float _strtof_c(const char *__restrict nptr,
31-
char **__restrict endptr)
38+
39+
/**
40+
* @remarks `*str >= '0' && *str <= '9'` is smaller than calls to `isdigit(*str)`
41+
* @todo Add support for INF INFINITY NAN NAN(...)
42+
*/
43+
float _strtof_c(const char *__restrict nptr, char **__restrict endptr)
3244
{
33-
union
34-
{
35-
float d;
36-
unsigned short s[2];
37-
}val;
38-
int frac = 0;
39-
int exp = 0;
40-
signed char sign = 1;
41-
signed char exp_sign = 1;
42-
char *str = (char*)nptr;
45+
F32_pun val;
46+
int frac = 0;
47+
int exp = 0;
48+
bool sign = false;
49+
bool exp_sign = false;
50+
char *str = (char*)nptr;
4351

44-
while (isspace(*str))
45-
++str;
52+
while (isspace(*str)) {
53+
++str;
54+
}
4655

47-
if (*str == '-') {
48-
sign = -1;
49-
++str;
50-
}
51-
else if (*str == '+')
52-
++str;
56+
if (*str == '-') {
57+
sign = true;
58+
++str;
59+
} else if (*str == '+') {
60+
++str;
61+
}
5362

54-
val.d = 0;
55-
while (*str >= '0' && *str <= '9') {
56-
val.d = val.d * 10 + (*str - '0');
57-
++str;
58-
}
63+
val.flt = 0.0f;
5964

60-
if (*str == '.') {
61-
++str;
6265
while (*str >= '0' && *str <= '9') {
63-
val.d = val.d * 10 + (*str - '0');
64-
++frac;
65-
++str;
66+
val.flt = val.flt * 10.0f + (float)(*str - '0');
67+
++str;
6668
}
67-
}
6869

69-
if (*str == 'e' || *str == 'E') {
70-
++str;
71-
if (*str == '-') {
72-
exp_sign = -1;
73-
++str;
70+
if (*str == '.') {
71+
++str;
72+
while (*str >= '0' && *str <= '9') {
73+
val.flt = val.flt * 10.0f + (float)(*str - '0');
74+
++frac;
75+
++str;
76+
}
7477
}
75-
else if (*str == '+') {
76-
exp_sign = 1;
77-
++str;
78-
}
79-
while (*str >= '0' && *str <= '9') {
80-
exp = exp * 10 + (*str - '0');
81-
++str;
78+
79+
if (*str == 'e' || *str == 'E') {
80+
++str;
81+
if (*str == '-') {
82+
exp_sign = true;
83+
++str;
84+
} else if (*str == '+') {
85+
exp_sign = false;
86+
++str;
87+
}
88+
while (*str >= '0' && *str <= '9') {
89+
exp = exp * 10 + (*str - '0');
90+
++str;
91+
}
8292
}
83-
}
8493

85-
if (endptr)
86-
*endptr = (char*)str;
94+
if (endptr) {
95+
*endptr = (char*)str;
96+
}
8797

88-
if (exp_sign < 0 )
89-
exp = -exp;
90-
exp -= frac;
91-
if (val.d != 0)
92-
{
93-
while (exp > 0 )
94-
{
95-
val.d *= 10.0;
96-
if (val.s[1] == 0x7f80)
97-
{
98-
errno = ERANGE;
99-
val.d = HUGE_VAL;
100-
break;
101-
}
102-
--exp;
98+
if (exp_sign) {
99+
exp = -exp;
103100
}
104-
while (exp < 0 )
101+
exp -= frac;
102+
if (val.bin != 0)
105103
{
106-
val.d *= .1;
107-
if (val.s[1] == 0)
108-
{
109-
errno = ERANGE;
110-
break;
111-
}
112-
++exp;
104+
while (exp > 0 )
105+
{
106+
val.flt *= 10.0f;
107+
if (!isfinite(val.flt))
108+
{
109+
errno = ERANGE;
110+
val.flt = HUGE_VALF;
111+
break;
112+
}
113+
--exp;
114+
}
115+
while (exp < 0 )
116+
{
117+
val.flt /= 10.0f;
118+
if (val.bin == 0)
119+
{
120+
errno = ERANGE;
121+
break;
122+
}
123+
++exp;
124+
}
125+
}
126+
if (sign) {
127+
val.flt = -val.flt;
113128
}
114-
if (sign < 0 )
115-
val.s[1] |= 0x8000;
116-
}
117-
return val.d;
129+
return val.flt;
118130
}
119131

120132
double _strtod_c(const char *, char **) __attribute__((alias("_strtof_c")));

0 commit comments

Comments
 (0)