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

Fpu tests #5

Merged
merged 9 commits into from
May 11, 2024
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
3 changes: 2 additions & 1 deletion .github/workflows/ops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: exe/ops test suite
on:
push:
paths:
- 'exe/ops/**'
- 'exe/ops/ops.exe'
- 'exe/ops/out.txt'
- '.github/workflows/ops.yml'
branches-ignore:
- main
Expand Down
307 changes: 300 additions & 7 deletions exe/ops/fpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,314 @@ extern "C" int _fltused = 0;

namespace {

void print_fpu_stack(int depth) {
double stack[8] = {};
for (int i = 0; i < depth; i++) {
__asm {
mov eax, i
fstp qword ptr [stack + eax*8]
}
}
for (int i = depth; i > 0; i--) {
printv(" %f", stack[i - 1]);
}
print("\n");
}

void test_fld_constants() {
double out = 0;
__asm {
fld1
fldz
fldpi
fldl2e
faddp
faddp
faddp
fstp [out]
}
printv("1+0+pi+l2e = %f\n", out);
printv("fld 1,0,pi,l2e =>");
print_fpu_stack(4);
}

void test_fld() {
float f32 = 1.1;
double f64 = 2.2;
__asm {
fld f32
fld f64
fld st(1)
}
printv("fld =>");
print_fpu_stack(3);
}

void test_fld_neg() {
float f32 = -1.1;
double f64 = -2.2;
__asm {
fld f32
fld f64
fld st(1)
}
printv("fld negative =>");
print_fpu_stack(3);
}

void test_fild() {
uint16_t i16 = 4321;
uint32_t i32 = 44321;
uint64_t i64 = 454321;
__asm {
fild i16
fild i32
fild i64
}
printv("fild =>");
print_fpu_stack(3);
}

void test_fild_neg() {
uint16_t i16 = -4321;
uint32_t i32 = -44321;
uint64_t i64 = -454321;
__asm {
fild i16
fild i32
fild i64
}
printv("fild neg =>");
print_fpu_stack(3);
}

void test_fst() {
float f32;
double f64;
__asm {
fldpi
fstp f32
fldpi
fstp f64
}
printv("fst => %f %f\n", f32, f64);
}

void test_fst_neg() {
float f32;
double f64;
__asm {
fldpi
fchs
fstp f32
fldpi
fchs
fstp f64
}
printv("fst neg => %f %f\n", f32, f64);
}

void test_fist() {
uint16_t i16;
uint32_t i32;
uint64_t i64;
__asm {
fldpi
fistp word ptr [i16]
fldpi
fistp dword ptr [i32]
fldpi
fistp qword ptr [i64]
}
printv("fist => %x %x %x\n", i16, i32, i64);
}

void test_fist_neg() {
uint16_t i16;
uint32_t i32;
uint64_t i64;
__asm {
fldpi
fchs
fistp word ptr [i16]
fldpi
fchs
fistp dword ptr [i32]
fldpi
fchs
fistp qword ptr [i64]
}
printv("fist neg => %x %x %x\n", i16, i32, i64);
}

void test_fchs() {
__asm {
fldpi
fchs

fld st(0) ; dup
fchs
}
print("fchs =>");
print_fpu_stack(2);
}

void test_fabs() {
__asm {
fldpi
fchs

fld st(0) ; dup
fabs
}
print("fabs =>");
print_fpu_stack(2);
}

void test_trig() {
__asm {
fld1
fsin

fld1
fcos

fld1
fsincos

fldpi
fldl2e
fpatan
}
print("trig =>");
print_fpu_stack(5);
}

void test_fadd_st() {
__asm {
fldpi
fldl2e
fadd st(0), st(1)

fldpi
fldl2e
fadd st(1), st(0)

fldpi
fldl2e
faddp
}
print("fadd st =>");
print_fpu_stack(5);
}

void test_fadd_mem() {
float f32 = 43.21;
double f64 = 432.1;
__asm {
fldpi
fadd f32

fldpi
fadd f64
}
print("fadd mem =>");
print_fpu_stack(2);
}

void test_fadd_mem_neg() {
float f32 = -43.21;
double f64 = -432.1;
__asm {
fldpi
fadd f32

fldpi
fadd f64
}
print("fadd mem neg =>");
print_fpu_stack(2);
}

void test_fiadd() {
uint16_t i16 = 43;
uint32_t i32 = 44;
__asm {
fldpi
fiadd i16

fldpi
fiadd i32
}
print("fiadd =>");
print_fpu_stack(2);
}

void test_fiadd_neg() {
uint16_t i16 = -43;
uint32_t i32 = -44;
__asm {
fldpi
fiadd i16

fldpi
fiadd i32
}
print("fiadd neg =>");
print_fpu_stack(2);
}

void test_fsub_mem() {
float f32 = 43.21;
double f64 = 432.1;
__asm {
fldpi
fsub f32
fsub f64
}
print("fsub mem =>");
print_fpu_stack(1);
}

void test_f2xm1() {
// Input must be in range -1..1.
float neg7 = -0.7;
float pos7 = 0.7;
__asm {
fld neg7
f2xm1
fld pos7
f2xm1
}
print("f2xm1 =>");
print_fpu_stack(2);
}

void test_fscale() {
__asm {
fldpi
fldl2e
fscale
}
print("fscale =>");
print_fpu_stack(1);
}

} // anonymous namespace

void fpu_tests() { test_fld_constants(); }
void fpu_tests() {
test_fld_constants();
test_fld();
test_fld_neg();
test_fild();
test_fild_neg();
test_fst();
test_fst_neg();
test_fist();
test_fist_neg();
test_fchs();
test_fabs();
test_trig();
test_fadd_st();
test_fadd_mem();
test_fadd_mem_neg();
test_fiadd();
test_fiadd_neg();
test_fsub_mem();
test_f2xm1();
test_fscale();
}
Binary file modified exe/ops/ops.exe
Binary file not shown.
21 changes: 20 additions & 1 deletion exe/ops/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,23 @@ ror 2,1 => 1
ror 2,2 => 80 CF
ror 6,1 => 3
ror 6,2 => 81 CF
1+0+pi+l2e = 5.584
fld 1,0,pi,l2e => 1.000 0 3.141 1.442
fld => 1.100 2.200 1.100
fld negative => -1.100 -2.200 -1.100
fild => 4321.000 44321.000 454321.000
fild neg => -4321.000 -44321.000 -454321.000
fst => 3.141 3.141
fst neg => -3.141 -3.141
fist => 3 3 3
fist neg => fffd fffffffd fffffffd
fchs => -3.141 3.141
fabs => -3.141 3.141
trig => 0.841 0.540 0.841 0.540 1.140
fadd st => 3.141 4.584 4.584 1.442 4.584
fadd mem => 46.351 435.241
fadd mem neg => -40.068 -428.958
fiadd => 46.141 47.141
fiadd neg => -39.858 -40.858
fsub mem => -472.168
f2xm1 => -0.384 0.624
fscale => 11.541
15 changes: 7 additions & 8 deletions exe/ops/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ void print(uint32_t x) {
void print(double f) {
// To print a float, we multiply by 1000 and print as decimal.

// TODO: retrowin32 doesn't support the fpu op that "<" needs.
// bool neg = f < 0;
// if (f < 0)
// f = -f;
bool neg = f < 0;
if (f < 0)
f = -f;
uint32_t x = (uint32_t)(f * 1000.0);
char buf[64];
size_t i = sizeof(buf);
if (x == 0) {
buf[--i] = '0';
} else {
while (x > 0 && i > (sizeof(buf) - 5)) {
while (x > 0 || i > (sizeof(buf) - 5)) {
buf[--i] = '0' + (x % 10);
x /= 10;
if (i == sizeof(buf) - 3) {
Expand All @@ -48,9 +47,9 @@ void print(double f) {
}
}
}
// if (neg) {
// buf[--i] = '-';
// }
if (neg) {
buf[--i] = '-';
}
print(std::string_view(&buf[i], sizeof(buf) - i));
}

Expand Down
Loading
Loading