Skip to content

Commit 6d40e82

Browse files
committed
Added C11 <complex.h>, along with CMPLX creal cimag conj
1 parent 5833aa0 commit 6d40e82

14 files changed

+602
-0
lines changed

src/libc/cimagf.src

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _cimagf, _cimag
6+
7+
; float cimagf(float _Complex)
8+
_cimagf:
9+
_cimag:
10+
ld iy, 0
11+
add iy, sp
12+
ld sp, iy
13+
ld hl, (iy + 7)
14+
ld e, (iy + 10)
15+
ret

src/libc/cimagl.src

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _cimagl
6+
7+
; long double cimagl(long double _Complex)
8+
_cimagl:
9+
ld iy, 0
10+
add iy, sp
11+
ld sp, iy
12+
ld hl, (iy + 11)
13+
ld de, (iy + 14)
14+
ld bc, (iy + 17)
15+
ret

src/libc/cmplxf.src

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _CMPLXF, _CMPLX
6+
7+
; float _Complex CMPLXF(float x, float y) /* struct ABI */
8+
_CMPLXF:
9+
_CMPLX:
10+
pop iy, de
11+
or a, a
12+
sbc hl, hl
13+
add hl, sp
14+
push de ; ZDS II sret
15+
ld bc, 4 ; sizeof(float _Complex)
16+
ldir
17+
; skip over the padding
18+
inc hl
19+
inc hl
20+
ld c, 4
21+
ldir
22+
ex (sp), hl ; ZDS II sret
23+
jp (iy)

src/libc/cmplxl.src

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _CMPLXL
6+
7+
; long double _Complex CMPLXL(long double x, long double y) /* struct ABI */
8+
_CMPLXL:
9+
pop iy, de
10+
or a, a
11+
sbc hl, hl
12+
add hl, sp
13+
push de ; ZDS II sret
14+
ld bc, 8 ; sizeof(long double _Complex)
15+
ldir
16+
; skip over the padding
17+
inc hl
18+
ld c, 8
19+
ldir
20+
ex (sp), hl ; ZDS II sret
21+
jp (iy)

src/libc/conjf.src

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _conjf, _conj
6+
7+
; enable if fpneg is required
8+
if 1
9+
10+
; float _Complex conjf(float _Complex) /* struct ABI */
11+
_conjf:
12+
_conj:
13+
pop iy, de
14+
ld hl, 7
15+
add hl, sp
16+
17+
; negate the complex part
18+
ld a, (hl)
19+
or a, a
20+
jr nz, .do_fpneg
21+
; test if value is +0.0f
22+
push hl
23+
dec hl
24+
dec hl
25+
dec hl
26+
ld bc, (hl)
27+
sbc hl, hl
28+
sbc hl, bc
29+
pop hl
30+
jr z, .skip_fpneg
31+
.do_fpneg:
32+
xor a, $80
33+
ld (hl), a
34+
.skip_fpneg:
35+
36+
ld bc, 8 ; sizeof(float _Complex)
37+
sbc hl, bc
38+
inc hl ; hl = sp + 0
39+
push de ; ZDS II sret
40+
ldir
41+
ex de, hl
42+
ex (sp), hl ; ZDS II sret
43+
jp (iy)
44+
45+
else
46+
47+
; float _Complex conjf(float _Complex) /* struct ABI */
48+
_conjf:
49+
_conj:
50+
pop iy, de
51+
ld hl, 7
52+
add hl, sp
53+
ld a, (hl)
54+
xor a, $80 ; negate imag-part
55+
ld (hl), a
56+
57+
ld bc, 8 ; sizeof(float _Complex)
58+
sbc hl, bc
59+
inc hl ; hl = sp + 0
60+
push de ; ZDS II sret
61+
ldir
62+
ex de, hl
63+
ex (sp), hl ; ZDS II sret
64+
jp (iy)
65+
66+
end if

src/libc/conjl.src

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _conjl
6+
7+
; long double _Complex conjl(long double _Complex) /* struct ABI */
8+
_conjl:
9+
pop iy, de
10+
ld hl, 15
11+
add hl, sp
12+
ld a, (hl)
13+
xor a, $80 ; negate imag-part
14+
ld (hl), a
15+
16+
ld bc, 16 ; sizeof(long double _Complex)
17+
sbc hl, bc
18+
inc hl ; hl = sp + 0
19+
push de ; ZDS II sret
20+
ldir
21+
ex de, hl
22+
ex (sp), hl ; ZDS II sret
23+
jp (iy)

src/libc/crealf.src

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _crealf, _creal
6+
7+
; float crealf(float _Complex)
8+
_crealf:
9+
_creal:
10+
pop bc, hl, de
11+
push de, hl, bc
12+
ret

src/libc/creall.src

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
assume adl=1
2+
3+
section .text
4+
5+
public _creall
6+
7+
; long double creall(long double _Complex)
8+
_creall:
9+
pop iy, hl, de, bc
10+
push bc, de, hl
11+
jp (iy)

src/libc/header_test.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <assert.h>
33
#include <byteswap.h>
44
#include <cdefs.h>
5+
#include <complex.h>
56
#include <ctype.h>
67
#include <errno.h>
78
#include <fenv.h>

src/libc/include/complex.h

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#ifndef _COMPLEX_H
2+
#define _COMPLEX_H
3+
4+
#ifndef __FAST_MATH__
5+
#warning "-ffast-math is required for complex multiplication and division to work properly at this time"
6+
#endif
7+
8+
#ifndef __cplusplus
9+
#define complex _Complex
10+
#endif
11+
12+
#ifdef _Imaginary
13+
#define imaginary _Imaginary
14+
#endif
15+
16+
#ifndef I
17+
# ifdef _Imaginary_I
18+
# define I _Imaginary_I
19+
# else
20+
# define I _Complex_I
21+
# endif
22+
#endif
23+
24+
double _Complex CMPLX(double x, double y);
25+
float _Complex CMPLXF(float x, float y);
26+
long double _Complex CMPLXL(long double x, long double y);
27+
28+
#ifdef _Imaginary_I
29+
30+
#define CMPLX(x, y) ((double _Complex)((double)(x) + _Imaginary_I * (double)(y)))
31+
#define CMPLXF(x, y) ((float _Complex)((float)(x) + _Imaginary_I * (float)(y)))
32+
#define CMPLXL(x, y) ((long double _Complex)((long double)(x) + _Imaginary_I * (long double)(y)))
33+
34+
#endif /* _Imaginary_I */
35+
36+
double creal(double _Complex);
37+
float crealf(float _Complex);
38+
long double creall(long double _Complex);
39+
40+
double cimag(double _Complex);
41+
float cimagf(float _Complex);
42+
long double cimagl(long double _Complex);
43+
44+
double cabs(double _Complex);
45+
float cabsf(float _Complex);
46+
long double cabsl(long double _Complex);
47+
48+
double carg(double _Complex);
49+
float cargf(float _Complex);
50+
long double cargl(long double _Complex);
51+
52+
double _Complex conj(double _Complex);
53+
float _Complex conjf(float _Complex);
54+
long double _Complex conjl(long double _Complex);
55+
56+
double _Complex cproj(double _Complex);
57+
float _Complex cprojf(float _Complex);
58+
long double _Complex cprojl(long double _Complex);
59+
60+
double _Complex csqrt(double _Complex);
61+
float _Complex csqrtf(float _Complex);
62+
long double _Complex csqrtl(long double _Complex);
63+
64+
double _Complex cexp(double _Complex);
65+
float _Complex cexpf(float _Complex);
66+
long double _Complex cexpl(long double _Complex);
67+
68+
double _Complex clog(double _Complex);
69+
float _Complex clogf(float _Complex);
70+
long double _Complex clogl(long double _Complex);
71+
72+
double _Complex cpow(double _Complex, double _Complex);
73+
float _Complex cpowf(float _Complex, float _Complex);
74+
long double _Complex cpowl(long double _Complex, long double _Complex);
75+
76+
double _Complex csin(double _Complex);
77+
float _Complex csinf(float _Complex);
78+
long double _Complex csinl(long double _Complex);
79+
80+
double _Complex ccos(double _Complex);
81+
float _Complex ccosf(float _Complex);
82+
long double _Complex ccosl(long double _Complex);
83+
84+
double _Complex ctan(double _Complex);
85+
float _Complex ctanf(float _Complex);
86+
long double _Complex ctanl(long double _Complex);
87+
88+
double _Complex casin(double _Complex);
89+
float _Complex casinf(float _Complex);
90+
long double _Complex casinl(long double _Complex);
91+
92+
double _Complex cacos(double _Complex);
93+
float _Complex cacosf(float _Complex);
94+
long double _Complex cacosl(long double _Complex);
95+
96+
double _Complex catan(double _Complex);
97+
float _Complex catanf(float _Complex);
98+
long double _Complex catanl(long double _Complex);
99+
100+
double _Complex csinh(double _Complex);
101+
float _Complex csinhf(float _Complex);
102+
long double _Complex csinhl(long double _Complex);
103+
104+
double _Complex ccosh(double _Complex);
105+
float _Complex ccoshf(float _Complex);
106+
long double _Complex ccoshl(long double _Complex);
107+
108+
double _Complex ctanh(double _Complex);
109+
float _Complex ctanhf(float _Complex);
110+
long double _Complex ctanhl(long double _Complex);
111+
112+
double _Complex casinh(double _Complex);
113+
float _Complex casinhf(float _Complex);
114+
long double _Complex casinhl(long double _Complex);
115+
116+
double _Complex cacosh(double _Complex);
117+
float _Complex cacoshf(float _Complex);
118+
long double _Complex cacoshl(long double _Complex);
119+
120+
double _Complex catanh(double _Complex);
121+
float _Complex catanhf(float _Complex);
122+
long double _Complex catanhl(long double _Complex);
123+
124+
#endif /* _COMPLEX_H */
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"transfer_files": [
3+
"bin/DEMO.8xp"
4+
],
5+
"target": {
6+
"name": "DEMO",
7+
"isASM": true
8+
},
9+
"sequence": [
10+
"action|launch",
11+
"delay|1000",
12+
"hashWait|1",
13+
"key|enter",
14+
"delay|200",
15+
"hashWait|2"
16+
],
17+
"hashes": {
18+
"1": {
19+
"description": "All tests passed or GDB1 error",
20+
"timeout": 5000,
21+
"start": "vram_start",
22+
"size": "vram_16_size",
23+
"expected_CRCs": [
24+
"38E2AD5A",
25+
"2C812DC2"
26+
]
27+
},
28+
"2": {
29+
"description": "Exit or GDB1 error",
30+
"start": "vram_start",
31+
"size": "vram_16_size",
32+
"expected_CRCs": [
33+
"FFAF89BA",
34+
"101734A5",
35+
"9DA19F44",
36+
"A32840C8",
37+
"349F4775",
38+
"271A9FBF",
39+
"82FD0B1E"
40+
]
41+
}
42+
}
43+
}

test/floating_point/complex/makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
10+
CFLAGS = -Wall -Wextra -Wshadow -Wimplicit-float-conversion -Wimplicit-int-float-conversion -std=c17 -Ofast
11+
CXXFLAGS = -Wall -Wextra -Wshadow -Wimplicit-float-conversion -Wimplicit-int-float-conversion -std=c++20 -Ofast
12+
13+
PREFER_OS_LIBC = NO
14+
15+
# ----------------------------
16+
17+
include $(shell cedev-config --makefile)

0 commit comments

Comments
 (0)