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

fixed ultof and added tests #564

Merged
merged 2 commits into from
Apr 5, 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
2 changes: 0 additions & 2 deletions src/crt/os.src
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ __sshru_b := 000254h
__stoi := 000260h
public __stoiu
__stoiu := 000264h
public __ultof
__ultof := 000280h
54 changes: 54 additions & 0 deletions src/crt/ultof.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
assume adl=1

section .text

public __ultof

if 0

; round to nearest ties to even
__ultof:
bit 7, a
jp z, __ltof ; common case
; A:UBC > INT32_MAX
res 7, a ; sets the LSB of the exponent
sla c ; C = Round, NZ = Sticky
; A:UBC >>= 8
push af
inc sp
push bc
inc sp
pop bc
inc sp
ld a, $4F ; sets the exponent
ret nc ; round down
inc bc
ret nz ; round up (this will not overflow because bit 23 is 0)
; round to even
res 0, c
ret

else

; round to nearest ties away from zero (to match __ltof behaviour)
__ultof:
bit 7, a
jp z, __ltof ; common case
; A:UBC > INT32_MAX
res 7, a ; sets the LSB of the exponent
; A:UBC >>= 8
push af
or a, c ; M = Round
inc sp
push bc
inc sp
pop bc
inc sp
ld a, $4F ; sets the exponent
ret p ; round down
inc bc
ret ; round up (this will not overflow because bit 23 is 0)

end if

extern __ltof
2 changes: 1 addition & 1 deletion src/include/ti84pceg.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2282,7 +2282,7 @@ namespace ti?
?_fcmp := 0000274h
?_fdiv := 0000278h
?_ftol := 000027Ch
?_ultof := 0000280h
;?_ultof := 0000280h
?_ltof := 0000284h
?_fmul := 0000288h
?_fneg := 000028Ch
Expand Down
43 changes: 43 additions & 0 deletions test/floating_point/ultof/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|300",
"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/ultof/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 -Wfloat-conversion -Wimplicit-float-conversion -Wimplicit-int-float-conversion -O0
CXXFLAGS = -Wall -Wextra -Wshadow -Wfloat-conversion -Wimplicit-float-conversion -Wimplicit-int-float-conversion -O0

PREFER_OS_LIBC = NO

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

include $(shell cedev-config --makefile)
76 changes: 76 additions & 0 deletions test/floating_point/ultof/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <ti/screen.h>
#include <ti/getcsc.h>
#include <sys/util.h>

#include "ultof_lut.h"

typedef union F32_pun {
float flt;
uint32_t bin;
} F32_pun;

#define ARRAY_LENGTH(x) (sizeof(x) / sizeof(x[0]))

void print_failed(uint32_t input, uint32_t guess, uint32_t truth) {
printf(
"I: %lu\nU: %08lX -->\nG: %08lX !=\nT: %08lX\n",
input, input, guess, truth
);
}

size_t run_test(void) {
typedef uint32_t input_t;
typedef F32_pun output_t;

const size_t length = ARRAY_LENGTH(ultof_LUT_input);
const input_t *input = (const input_t* )((const void*)ultof_LUT_input );
const output_t *output = (const output_t*)((const void*)ultof_LUT_output);

for (size_t i = 0; i < length; i++) {
F32_pun result;

result.flt = (float)input[i];
if (result.bin != output[i].bin) {
// ignore round to maximum magnitude errors from __ltof
#if 0
bool ignore_ltof_failure =
(input[i] <= INT32_MAX) &&
(result.bin == output[i].bin + 1);
if (ignore_ltof_failure == false) {
print_failed(input[i], result.bin, output[i].bin);
return i;
}
#else
// round to nearest ties away from zero to match __ltof behaviour
bool ignore_ltof_failure =
(result.bin == output[i].bin + 1);
if (ignore_ltof_failure == false) {
print_failed(input[i], result.bin, output[i].bin);
return i;
}
#endif
}
}

/* passed all */
return SIZE_MAX;
}

int main(void) {
os_ClrHome();
size_t fail_index = run_test();
if (fail_index == SIZE_MAX) {
printf("All tests passed");
} else {
printf("Failed test: %zu", fail_index);
}

while (!os_GetCSC());

return 0;
}
Loading
Loading