Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C11 <complex.h>, along with CMPLX creal cimag conj #557

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libc/cimagf.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
assume adl=1

section .text

public _cimagf, _cimag

; float cimagf(float _Complex)
_cimagf:
_cimag:
ld iy, 0
add iy, sp
ld sp, iy
ld hl, (iy + 7)
ld e, (iy + 10)
ret
15 changes: 15 additions & 0 deletions src/libc/cimagl.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
assume adl=1

section .text

public _cimagl

; long double cimagl(long double _Complex)
_cimagl:
ld iy, 0
add iy, sp
ld sp, iy
ld hl, (iy + 11)
ld de, (iy + 14)
ld bc, (iy + 17)
ret
23 changes: 23 additions & 0 deletions src/libc/cmplxf.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
assume adl=1

section .text

public _CMPLXF, _CMPLX

; float _Complex CMPLXF(float x, float y) /* struct ABI */
_CMPLXF:
_CMPLX:
pop iy, de
or a, a
sbc hl, hl
add hl, sp
push de ; ZDS II sret
ld bc, 4 ; sizeof(float _Complex)
ldir
; skip over the padding
inc hl
inc hl
ld c, 4
ldir
ex (sp), hl ; ZDS II sret
jp (iy)
21 changes: 21 additions & 0 deletions src/libc/cmplxl.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
assume adl=1

section .text

public _CMPLXL

; long double _Complex CMPLXL(long double x, long double y) /* struct ABI */
_CMPLXL:
pop iy, de
or a, a
sbc hl, hl
add hl, sp
push de ; ZDS II sret
ld bc, 8 ; sizeof(long double _Complex)
ldir
; skip over the padding
inc hl
ld c, 8
ldir
ex (sp), hl ; ZDS II sret
jp (iy)
66 changes: 66 additions & 0 deletions src/libc/conjf.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
assume adl=1

section .text

public _conjf, _conj

; enable if fpneg is required
if 1

; float _Complex conjf(float _Complex) /* struct ABI */
_conjf:
_conj:
pop iy, de
ld hl, 7
add hl, sp

; negate the complex part
ld a, (hl)
or a, a
jr nz, .do_fpneg
; test if value is +0.0f
push hl
dec hl
dec hl
dec hl
ld bc, (hl)
sbc hl, hl
sbc hl, bc
pop hl
jr z, .skip_fpneg
.do_fpneg:
xor a, $80
ld (hl), a
.skip_fpneg:

ld bc, 8 ; sizeof(float _Complex)
sbc hl, bc
inc hl ; hl = sp + 0
push de ; ZDS II sret
ldir
ex de, hl
ex (sp), hl ; ZDS II sret
jp (iy)

else

; float _Complex conjf(float _Complex) /* struct ABI */
_conjf:
_conj:
pop iy, de
ld hl, 7
add hl, sp
ld a, (hl)
xor a, $80 ; negate imag-part
ld (hl), a

ld bc, 8 ; sizeof(float _Complex)
sbc hl, bc
inc hl ; hl = sp + 0
push de ; ZDS II sret
ldir
ex de, hl
ex (sp), hl ; ZDS II sret
jp (iy)

end if
23 changes: 23 additions & 0 deletions src/libc/conjl.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
assume adl=1

section .text

public _conjl

; long double _Complex conjl(long double _Complex) /* struct ABI */
_conjl:
pop iy, de
ld hl, 15
add hl, sp
ld a, (hl)
xor a, $80 ; negate imag-part
ld (hl), a

ld bc, 16 ; sizeof(long double _Complex)
sbc hl, bc
inc hl ; hl = sp + 0
push de ; ZDS II sret
ldir
ex de, hl
ex (sp), hl ; ZDS II sret
jp (iy)
12 changes: 12 additions & 0 deletions src/libc/crealf.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
assume adl=1

section .text

public _crealf, _creal

; float crealf(float _Complex)
_crealf:
_creal:
pop bc, hl, de
push de, hl, bc
ret
11 changes: 11 additions & 0 deletions src/libc/creall.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
assume adl=1

section .text

public _creall

; long double creall(long double _Complex)
_creall:
pop iy, hl, de, bc
push bc, de, hl
jp (iy)
1 change: 1 addition & 0 deletions src/libc/header_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <assert.h>
#include <byteswap.h>
#include <cdefs.h>
#include <complex.h>
#include <ctype.h>
#include <errno.h>
#include <fenv.h>
Expand Down
124 changes: 124 additions & 0 deletions src/libc/include/complex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#ifndef _COMPLEX_H
#define _COMPLEX_H

#ifndef __FAST_MATH__
#warning "-ffast-math is required for complex multiplication and division to work properly at this time"
#endif

#ifndef __cplusplus
#define complex _Complex
#endif

#ifdef _Imaginary
#define imaginary _Imaginary
#endif

#ifndef I
# ifdef _Imaginary_I
# define I _Imaginary_I
# else
# define I _Complex_I
# endif
#endif

double _Complex CMPLX(double x, double y);
float _Complex CMPLXF(float x, float y);
long double _Complex CMPLXL(long double x, long double y);

#ifdef _Imaginary_I

#define CMPLX(x, y) ((double _Complex)((double)(x) + _Imaginary_I * (double)(y)))
#define CMPLXF(x, y) ((float _Complex)((float)(x) + _Imaginary_I * (float)(y)))
#define CMPLXL(x, y) ((long double _Complex)((long double)(x) + _Imaginary_I * (long double)(y)))

#endif /* _Imaginary_I */

double creal(double _Complex);
float crealf(float _Complex);
long double creall(long double _Complex);

double cimag(double _Complex);
float cimagf(float _Complex);
long double cimagl(long double _Complex);

double cabs(double _Complex);
float cabsf(float _Complex);
long double cabsl(long double _Complex);

double carg(double _Complex);
float cargf(float _Complex);
long double cargl(long double _Complex);

double _Complex conj(double _Complex);
float _Complex conjf(float _Complex);
long double _Complex conjl(long double _Complex);

double _Complex cproj(double _Complex);
float _Complex cprojf(float _Complex);
long double _Complex cprojl(long double _Complex);

double _Complex csqrt(double _Complex);
float _Complex csqrtf(float _Complex);
long double _Complex csqrtl(long double _Complex);

double _Complex cexp(double _Complex);
float _Complex cexpf(float _Complex);
long double _Complex cexpl(long double _Complex);

double _Complex clog(double _Complex);
float _Complex clogf(float _Complex);
long double _Complex clogl(long double _Complex);

double _Complex cpow(double _Complex, double _Complex);
float _Complex cpowf(float _Complex, float _Complex);
long double _Complex cpowl(long double _Complex, long double _Complex);

double _Complex csin(double _Complex);
float _Complex csinf(float _Complex);
long double _Complex csinl(long double _Complex);

double _Complex ccos(double _Complex);
float _Complex ccosf(float _Complex);
long double _Complex ccosl(long double _Complex);

double _Complex ctan(double _Complex);
float _Complex ctanf(float _Complex);
long double _Complex ctanl(long double _Complex);

double _Complex casin(double _Complex);
float _Complex casinf(float _Complex);
long double _Complex casinl(long double _Complex);

double _Complex cacos(double _Complex);
float _Complex cacosf(float _Complex);
long double _Complex cacosl(long double _Complex);

double _Complex catan(double _Complex);
float _Complex catanf(float _Complex);
long double _Complex catanl(long double _Complex);

double _Complex csinh(double _Complex);
float _Complex csinhf(float _Complex);
long double _Complex csinhl(long double _Complex);

double _Complex ccosh(double _Complex);
float _Complex ccoshf(float _Complex);
long double _Complex ccoshl(long double _Complex);

double _Complex ctanh(double _Complex);
float _Complex ctanhf(float _Complex);
long double _Complex ctanhl(long double _Complex);

double _Complex casinh(double _Complex);
float _Complex casinhf(float _Complex);
long double _Complex casinhl(long double _Complex);

double _Complex cacosh(double _Complex);
float _Complex cacoshf(float _Complex);
long double _Complex cacoshl(long double _Complex);

double _Complex catanh(double _Complex);
float _Complex catanhf(float _Complex);
long double _Complex catanhl(long double _Complex);

#endif /* _COMPLEX_H */
43 changes: 43 additions & 0 deletions test/floating_point/complex/autotest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"transfer_files": [
"bin/DEMO.8xp"
],
"target": {
"name": "DEMO",
"isASM": true
},
"sequence": [
"action|launch",
"delay|1000",
"hashWait|1",
"key|enter",
"delay|200",
"hashWait|2"
],
"hashes": {
"1": {
"description": "All tests passed or GDB1 error",
"timeout": 5000,
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"38E2AD5A",
"2C812DC2"
]
},
"2": {
"description": "Exit or GDB1 error",
"start": "vram_start",
"size": "vram_16_size",
"expected_CRCs": [
"FFAF89BA",
"101734A5",
"9DA19F44",
"A32840C8",
"349F4775",
"271A9FBF",
"82FD0B1E"
]
}
}
}
17 changes: 17 additions & 0 deletions test/floating_point/complex/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ----------------------------
# Makefile Options
# ----------------------------

NAME = DEMO
ICON = icon.png
DESCRIPTION = "CE C Toolchain Demo"
COMPRESSED = NO

CFLAGS = -Wall -Wextra -Wshadow -Wimplicit-float-conversion -Wimplicit-int-float-conversion -std=c17 -Ofast
CXXFLAGS = -Wall -Wextra -Wshadow -Wimplicit-float-conversion -Wimplicit-int-float-conversion -std=c++20 -Ofast

PREFER_OS_LIBC = NO

# ----------------------------

include $(shell cedev-config --makefile)
Loading
Loading