Skip to content

Commit e2f835b

Browse files
committed
Added std:: for some math.h macros, did minor bug fixes, and attempted to fix the autotests
1 parent c9be0f1 commit e2f835b

File tree

24 files changed

+7051
-11105
lines changed

24 files changed

+7051
-11105
lines changed

src/crt/drem.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ __drem:
1010
ld iy, 9
1111
add iy, sp
1212
push iy, bc, de, hl
13-
call _f64_drem
13+
call _f64_rem
1414
pop af, af, af, af, iy, af
1515
ret
1616

src/crt/lltof.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
float _lltof_c(long long x)
66
{
77
uint8_t exponent = x ? __builtin_clrsbll(x) : LLONG_WIDTH - 1;
8-
if (exponent >= LLONG_WIDTH - LONG_WIDTH) return (long)x;
8+
if (exponent >= LLONG_WIDTH - LONG_WIDTH) {
9+
return (float)((long)x);
10+
}
911
exponent = LLONG_WIDTH - LONG_WIDTH - exponent;
10-
return ldexpf((long)(x >> exponent), exponent);
12+
return ldexpf((float)((long)(x >> exponent)), exponent);
1113
}

src/crt/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include $(CURDIR)/../common.mk
1818

1919
BUILD_SRC := $(patsubst %,build/%.src,$(wildcard *.c *.cpp))
2020

21-
EZCFLAGS := -S -ffreestanding -Wall -Wextra -Oz
21+
EZCFLAGS := -S -ffreestanding -Wall -Wextra -Wimplicit-float-conversion -Wimplicit-int-float-conversion -Oz
2222
EZCFLAGS += -D_EZ80 -isystem ../libc/include -mllvm -profile-guided-section-prefix=false
2323
EZCXXFLAGS := $(EZCFLAGS) -fno-exceptions -fno-rtti
2424
EZCXXFLAGS += -isystem ../libc/include/c++

src/crt/ulltof.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
float _ulltof_c(unsigned long long x)
66
{
77
uint8_t exponent = x ? __builtin_clzll(x) : ULLONG_WIDTH;
8-
if (exponent >= ULLONG_WIDTH - ULONG_WIDTH) return (unsigned long)x;
8+
if (exponent >= ULLONG_WIDTH - ULONG_WIDTH) {
9+
return (float)((unsigned long)x);
10+
}
911
exponent = ULLONG_WIDTH - ULONG_WIDTH - exponent;
10-
return ldexpf((unsigned long)(x >> exponent), exponent);
12+
return ldexpf((float)((unsigned long)(x >> exponent)), exponent);
1113
}

src/libc/fmal.src

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
assume adl=1
2+
23
section .text
34

4-
public _fmal
5+
public _fmal, __debug_fmal
6+
57
_fmal := _softfloat_mulAddF64
8+
__debug_fmal := _softfloat_mulAddF64
69
; flags handled by softfloat
710

811
extern _softfloat_mulAddF64

src/libc/include/math.h

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,13 @@ extern "C" {
3838
#define FP_NAN 0x3
3939
#define FP_NORMAL 0x4
4040

41-
#if 0
42-
/* disabled until builtin is optimized */
43-
#define signbit(x) __builtin_signbit(x)
44-
45-
#else
46-
bool _signbitf(float x);
47-
bool _signbitl(long double x);
48-
#define signbit(x) ( \
49-
sizeof((x)) == sizeof(float) ? _signbitf((x)) : \
50-
sizeof((x)) == sizeof(long double) ? _signbitl((x)) : \
51-
(x) < 0)
52-
53-
#endif
54-
5541
#define isgreater(x, y) __builtin_isgreater(x, y)
5642
#define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
5743
#define isless(x, y) __builtin_isless(x, y)
5844
#define islessequal(x, y) __builtin_islessequal(x, y)
5945
#define islessgreater(x, y) __builtin_islessgreater(x, y)
6046
#define isunordered(x, y) __builtin_isunordered(x, y)
6147

62-
typedef float float_t;
63-
typedef double double_t;
64-
6548
int _isinff(float n);
6649
int _isnanf(float n);
6750
int _isnormalf(float n);
@@ -78,6 +61,21 @@ int _iszerol(long double n);
7861
int _issubnormall(long double n);
7962
int _fpclassifyl(long double n);
8063

64+
#if 0
65+
/* disabled until builtin is optimized */
66+
#define _signbitf(x) __builtin_signbit(x)
67+
#define _signbitl(x) __builtin_signbit(x)
68+
#else
69+
bool _signbitf(float x);
70+
bool _signbitl(long double x);
71+
#endif
72+
73+
#ifndef __cplusplus
74+
75+
#define signbit(x) ( \
76+
sizeof((x)) == sizeof(float) ? _signbitf((x)) : \
77+
sizeof((x)) == sizeof(long double) ? _signbitl((x)) : \
78+
(x) < 0)
8179
#define isinf(x) ( \
8280
sizeof((x)) == sizeof(float) ? _isinff((x)) : \
8381
sizeof((x)) == sizeof(long double) ? _isinfl((x)) : \
@@ -107,6 +105,49 @@ int _fpclassifyl(long double n);
107105
sizeof((x)) == sizeof(long double) ? _fpclassifyl((x)) : \
108106
0)
109107

108+
#else
109+
110+
extern "C++" {
111+
112+
inline bool signbit(float __x) { return _signbitf(__x); }
113+
inline bool signbit(double __x) { return _signbitf(__x); }
114+
inline bool signbit(long double __x) { return _signbitl(__x); }
115+
116+
inline bool isinf(float __x) { return _isinff(__x); }
117+
inline bool isinf(double __x) { return _isinff(__x); }
118+
inline bool isinf(long double __x) { return _isinfl(__x); }
119+
120+
inline bool isnan(float __x) { return _isnanf(__x); }
121+
inline bool isnan(double __x) { return _isnanf(__x); }
122+
inline bool isnan(long double __x) { return _isnanl(__x); }
123+
124+
inline bool isnormal(float __x) { return _isnormalf(__x); }
125+
inline bool isnormal(double __x) { return _isnormalf(__x); }
126+
inline bool isnormal(long double __x) { return _isnormall(__x); }
127+
128+
inline bool isfinite(float __x) { return _isfinitef(__x); }
129+
inline bool isfinite(double __x) { return _isfinitef(__x); }
130+
inline bool isfinite(long double __x) { return _isfinitel(__x); }
131+
132+
inline bool iszero(float __x) { return _iszerof(__x); }
133+
inline bool iszero(double __x) { return _iszerof(__x); }
134+
inline bool iszero(long double __x) { return _iszerol(__x); }
135+
136+
inline bool issubnormal(float __x) { return _issubnormalf(__x); }
137+
inline bool issubnormal(double __x) { return _issubnormalf(__x); }
138+
inline bool issubnormal(long double __x) { return _issubnormall(__x); }
139+
140+
inline bool fpclassify(float __x) { return _fpclassifyf(__x); }
141+
inline bool fpclassify(double __x) { return _fpclassifyf(__x); }
142+
inline bool fpclassify(long double __x) { return _fpclassifyl(__x); }
143+
144+
} /* extern "C++" */
145+
146+
#endif
147+
148+
typedef float float_t;
149+
typedef double double_t;
150+
110151
double acos(double);
111152
float acosf(float);
112153
long double acosl(long double);
@@ -352,26 +393,28 @@ long double truncl(long double);
352393

353394
/* aliases */
354395

355-
long double _debug_fabsl(long double x);
396+
long double _debug_fabsl(long double);
356397
#define fabsl _debug_fabsl
357-
long double _debug_copysignl(long double x, long double y);
398+
long double _debug_copysignl(long double, long double);
358399
#define copysignl _debug_copysignl
359-
long double _debug_fmaxl(long double x, long double y);
400+
long double _debug_fmaxl(long double, long double);
360401
#define fmaxl _debug_fmaxl
361-
long double _debug_fminl(long double x, long double y);
402+
long double _debug_fminl(long double, long double);
362403
#define fminl _debug_fminl
363-
long double _debug_truncl(long double x);
404+
long double _debug_truncl(long double);
364405
#define truncl _debug_truncl
365-
long double _debug_floorl(long double x);
406+
long double _debug_floorl(long double);
366407
#define floorl _debug_floorl
367-
long double _debug_ceill(long double x);
408+
long double _debug_ceill(long double);
368409
#define ceill _debug_ceill
369-
long double _debug_roundl(long double x);
410+
long double _debug_roundl(long double);
370411
#define roundl _debug_roundl
371-
long double _debug_nearbyintl(long double x);
412+
long double _debug_nearbyintl(long double);
372413
#define nearbyintl _debug_nearbyintl
373-
long double _debug_rintl(long double x);
414+
long double _debug_rintl(long double);
374415
#define rintl _debug_rintl
416+
long double _debug_fmal(long double, long double, long double);
417+
#define fmal _debug_fmal
375418

376419
#ifdef __cplusplus
377420
}

src/libc/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include $(CURDIR)/../common.mk
1818

1919
BUILD_SRC := $(patsubst %,build/%.src,$(wildcard *.c *.cpp))
2020

21-
EZCFLAGS := -S -ffreestanding -Wall -Wextra -Wshadow -Wimplicit-float-conversion -Wimplicit-int-float-conversion -Oz
21+
EZCFLAGS := -S -ffreestanding -Wall -Wextra -Wimplicit-float-conversion -Wimplicit-int-float-conversion -Oz
2222
EZCFLAGS += -D_EZ80 -isystem ../libc/include -isystem ../ce/include -isystem ../fileioc -mllvm -profile-guided-section-prefix=false
2323
EZCXXFLAGS := $(EZCFLAGS) -fno-exceptions -fno-rtti
2424
EZCXXFLAGS += -isystem ../libcxx/include

src/libc/roundf.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#include <math.h>
22

3+
/**
4+
* @remarks This function has a bug. Because `fabsf(x) + 0.5f` uses
5+
* round-to-nearest ties-to-even, `roundf(0.4999999702f)` will
6+
* return 1.0f instead of 0.0f
7+
*/
38
float roundf(float x)
49
{
5-
return copysignf(truncf(fabs(x) + .5f), x);
10+
return copysignf(truncf(fabsf(x) + .5f), x);
611
}
712

813
double round(double) __attribute__((alias("roundf")));

src/libcxx/include/cmath

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ namespace std {
1212
using ::float_t;
1313
using ::double_t;
1414

15+
using ::signbit;
16+
using ::isinf;
17+
using ::isnan;
18+
using ::isnormal;
19+
using ::isfinite;
20+
using ::iszero;
21+
using ::issubnormal;
22+
using ::fpclassify;
23+
1524
using ::acos;
1625
using ::acosf;
1726
using ::acosl;

src/libcxx/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ include $(CURDIR)/../common.mk
1818

1919
BUILD_SRC := $(patsubst %,build/%.src,$(wildcard *.c *.cpp))
2020

21-
EZCFLAGS := -S -ffreestanding -Wall -Wextra -Oz
21+
EZCFLAGS := -S -ffreestanding -Wall -Wextra -Wimplicit-float-conversion -Wimplicit-int-float-conversion -Oz
2222
EZCFLAGS += -D_EZ80 -isystem ../libc/include -isystem ../ce/include -isystem ../fileioc -mllvm -profile-guided-section-prefix=false
2323
EZCXXFLAGS := $(EZCFLAGS) -fno-exceptions -fno-rtti
2424
EZCXXFLAGS += -isystem ../libcxx/include

0 commit comments

Comments
 (0)