1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-17 16:29:32 +00:00

beginning of a reference libm in C

This commit is contained in:
mrdudz 2023-08-31 23:07:15 +02:00
parent 86d0859d8d
commit 013ef1553a
12 changed files with 169 additions and 23 deletions

View File

@ -37,6 +37,9 @@ float __fastcall__ roundf(float x); /* C99 */
/* double trunc(double x); */ /* C99 */
float __fastcall__ truncf(float x); /* C99 */
/* double ceil(double x) */
float ceilf(float x); /* C99 */
/* beware, this is not standard */
#ifndef M_PI
#define M_PI 3.14159265358979323846f

View File

@ -176,7 +176,8 @@ endif
SRCDIRS = $(SRCDIR)
SRCDIRS += float/softfloat
SRCDIRS += float/softfloat \
float/softmath
ifeq ($(TARGET),$(filter $(TARGET),$(CBMS)))
SRCDIRS += cbm

View File

@ -91,7 +91,7 @@ Related to fp stuff are:
### Roadmap
- Test/Fix using the Softfloat lib some more
- Find some generic math functions that we can use
- Find some more generic math functions that we can use in softmath
- When all obvious tests have been created and work OK, we can merge
After the merge, the following things can be done more or less independent from
@ -140,6 +140,14 @@ NOT WORKING YET:
- float values written as "12.34f" work, but "12.34" does not - should it?
TODO:
- more tests are needed
- warnings
- errors
- register variables
- more/all tests should check local/global/register variables
### Files & Functions
#### assignment.c
@ -256,27 +264,28 @@ DoConversion Emit code to convert the given expression to a new type
### variants
The floating point support calls library functions for any operations on non
constant values
constant values.
#### softfloat
This is a Port of "Berkeley SoftFloat Release 2c". It is currently used by
default for all targets.
- missing are all math functions (we might port another existing lib for that)
#### softmath
Contains a collection of math functions, hopefully some day enough to completely
implement math.h in C. This is currently used by default for all targets.
#### cbmkernal
This is a wrapper to the CBM kernal functions.
- this one is fairly complete, including math functions
- the only missing function is ftosrsubeax
- WANTED: which can be easily added once testcode is found that actually triggers it :)
This is a wrapper to the CBM kernal functions. This is fairly complete,
including math functions. To use this, link against c64-fp754kernal.o. The c64
samples will do this by default.
#### ieee754
This should become a freestanding IEEE754 library, which can completely replace
the softfloat library.
the softfloat (and softmath) library.
- basically everything missing except addition/substraction
- compare functions are missing
@ -298,7 +307,7 @@ historical float routines by woz :) unfortunately not ieee754
### runtime functions
these must be available in the runtime library
These must be available in the runtime library.
```
func description softfloat cbmfp wozfp 754 codegen.c
@ -330,27 +339,27 @@ ftoseqeax Test for equal * * - -
```
### math.h functions
these are optional, required for standard libm
These are optional, required for standard libm.
```
func description softfloat cbmfp wozfp 754
func description softmath cbmfp wozfp 754
/* C99 */
float powf(float f, float a) - * - -
float sinf(float s) - * - -
float cosf(float s) - * - -
float sinf(float s) * * - -
float cosf(float s) * * - -
float logf(float x) - * * -
float expf(float x) - * - -
float sqrtf(float x) - * - -
float tanf(float x) - * - -
float atanf(float x) - * - -
float fabsf(float x) - * - -
float roundf(float x) - * - -
float truncf(float x) - * - -
float fabsf(float x) * * - -
float roundf(float x) * * - -
float truncf(float x) * * - -
float ceilf(float x) * - - -
```
### extra functions
optional utility functions.
Optional utility functions.
```
func description softfloat cbmfp wozfp 754

View File

@ -0,0 +1,12 @@
This is a collection of various math related functions found in math.h. All
functions are single precision (only), and are rather selected for their
compactness than speed.
STILL MISSING:
float logf(float x)
float expf(float x)
float sqrtf(float x)
float tanf(float x)
float atanf(float x)

View File

@ -0,0 +1,13 @@
#include <math.h>
float ceilf(float x)
{
int n = (int) x;
if (n >= x) {
return n;
}
return n + 1;
}

View File

@ -0,0 +1,67 @@
/* nicked from https://github.com/AZHenley/cosine/blob/master/cosine.c */
/*
MIT License
Copyright (c) 2020 Austin Henley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <math.h>
#define RUNTERMS 8
float cosf(float x)
{
int i;
int div;
float num;
float result = 1.0f;
float inter = 1.0f;
float comp;
float den;
div = (int)(x / M_PI);
x = x - (div * M_PI);
num = x * x;
for (i = 1; i <= RUNTERMS; i++) {
comp = 2.0f * i;
den = comp * (comp - 1.0f);
inter *= num / den;
if (i % 2 == 0) {
result += inter;
} else {
result -= inter;
}
}
if (div % 2 != 0) {
return result * -1.0f;
}
return result;
}
#if 0
float c(float x) {
return sinf(x + (M_PI / 2.0));
}
#endif

View File

@ -0,0 +1,6 @@
#include <math.h>
float fabsf(float x) {
return x < 0.0 ? -x : x;
}

View File

@ -0,0 +1,7 @@
#include <math.h>
float powf(float x, float y)
{
return expf(x * logf(y));
}

View File

@ -0,0 +1,10 @@
#include <math.h>
float roundf(float x)
{
if (x > 0.0f) {
return (float)((signed)(x + 0.5f));
}
return (float)((signed)(x - 0.5f));
}

View File

@ -0,0 +1,6 @@
#include <math.h>
float sinf(float x) {
return cosf(x - (M_PI / 2.0));
}

View File

@ -0,0 +1,7 @@
#include <math.h>
float truncf(float x)
{
return (float)((signed)x);
}

View File

@ -3,6 +3,10 @@
#include <math.h>
#include <conio.h> // for cgetc
#ifdef __SIM6502__
#define cgetc()
#endif
char buf[100];
/*
@ -98,7 +102,7 @@ float roundtruncvals[22] = {
void roundtruncabs(void)
{
int n;
printf("round/truncate:\n");
printf("round/trunc:\n");
for (n = 0; n < 21; n++) {
printf("%12s", _ftostr(buf, roundtruncvals[n]));
printf("%12s", _ftostr(buf, roundf(roundtruncvals[n])));
@ -106,10 +110,11 @@ void roundtruncabs(void)
printf("\n");
}
printf("<key>\n"); cgetc();
printf("abs:\n");
printf("abs/ceil:\n");
for (n = 0; n < 21; n++) {
printf("%12s", _ftostr(buf, roundtruncvals[n]));
printf("%12s", _ftostr(buf, fabsf(roundtruncvals[n])));
printf("%12s", _ftostr(buf, ceilf(roundtruncvals[n])));
printf("\n");
}
printf("<key>\n"); cgetc();
@ -202,6 +207,6 @@ int main(void)
{
roundtruncabs();
sincostanatan();
powersqrt();
logexp();
powersqrt();
}