mirror of
https://github.com/cc65/cc65.git
synced 2024-06-26 05:29:30 +00:00
beginning of a reference libm in C
This commit is contained in:
parent
86d0859d8d
commit
013ef1553a
|
@ -37,6 +37,9 @@ float __fastcall__ roundf(float x); /* C99 */
|
||||||
/* double trunc(double x); */ /* C99 */
|
/* double trunc(double x); */ /* C99 */
|
||||||
float __fastcall__ truncf(float x); /* C99 */
|
float __fastcall__ truncf(float x); /* C99 */
|
||||||
|
|
||||||
|
/* double ceil(double x) */
|
||||||
|
float ceilf(float x); /* C99 */
|
||||||
|
|
||||||
/* beware, this is not standard */
|
/* beware, this is not standard */
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
#define M_PI 3.14159265358979323846f
|
#define M_PI 3.14159265358979323846f
|
||||||
|
|
|
@ -176,7 +176,8 @@ endif
|
||||||
|
|
||||||
SRCDIRS = $(SRCDIR)
|
SRCDIRS = $(SRCDIR)
|
||||||
|
|
||||||
SRCDIRS += float/softfloat
|
SRCDIRS += float/softfloat \
|
||||||
|
float/softmath
|
||||||
|
|
||||||
ifeq ($(TARGET),$(filter $(TARGET),$(CBMS)))
|
ifeq ($(TARGET),$(filter $(TARGET),$(CBMS)))
|
||||||
SRCDIRS += cbm
|
SRCDIRS += cbm
|
||||||
|
|
|
@ -91,7 +91,7 @@ Related to fp stuff are:
|
||||||
### Roadmap
|
### Roadmap
|
||||||
|
|
||||||
- Test/Fix using the Softfloat lib some more
|
- 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
|
- 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
|
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?
|
- 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
|
### Files & Functions
|
||||||
|
|
||||||
#### assignment.c
|
#### assignment.c
|
||||||
|
@ -256,27 +264,28 @@ DoConversion Emit code to convert the given expression to a new type
|
||||||
### variants
|
### variants
|
||||||
|
|
||||||
The floating point support calls library functions for any operations on non
|
The floating point support calls library functions for any operations on non
|
||||||
constant values
|
constant values.
|
||||||
|
|
||||||
#### softfloat
|
#### softfloat
|
||||||
|
|
||||||
This is a Port of "Berkeley SoftFloat Release 2c". It is currently used by
|
This is a Port of "Berkeley SoftFloat Release 2c". It is currently used by
|
||||||
default for all targets.
|
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
|
#### cbmkernal
|
||||||
|
|
||||||
This is a wrapper to the CBM kernal functions.
|
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
|
||||||
- this one is fairly complete, including math functions
|
samples will do this by default.
|
||||||
- the only missing function is ftosrsubeax
|
|
||||||
- WANTED: which can be easily added once testcode is found that actually triggers it :)
|
|
||||||
|
|
||||||
#### ieee754
|
#### ieee754
|
||||||
|
|
||||||
This should become a freestanding IEEE754 library, which can completely replace
|
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
|
- basically everything missing except addition/substraction
|
||||||
- compare functions are missing
|
- compare functions are missing
|
||||||
|
@ -298,7 +307,7 @@ historical float routines by woz :) unfortunately not ieee754
|
||||||
|
|
||||||
### runtime functions
|
### 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
|
func description softfloat cbmfp wozfp 754 codegen.c
|
||||||
|
|
||||||
|
@ -330,27 +339,27 @@ ftoseqeax Test for equal * * - -
|
||||||
```
|
```
|
||||||
### math.h functions
|
### 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 powf(float f, float a) - * - -
|
||||||
float sinf(float s) - * - -
|
float sinf(float s) * * - -
|
||||||
float cosf(float s) - * - -
|
float cosf(float s) * * - -
|
||||||
float logf(float x) - * * -
|
float logf(float x) - * * -
|
||||||
float expf(float x) - * - -
|
float expf(float x) - * - -
|
||||||
float sqrtf(float x) - * - -
|
float sqrtf(float x) - * - -
|
||||||
float tanf(float x) - * - -
|
float tanf(float x) - * - -
|
||||||
float atanf(float x) - * - -
|
float atanf(float x) - * - -
|
||||||
float fabsf(float x) - * - -
|
float fabsf(float x) * * - -
|
||||||
float roundf(float x) - * - -
|
float roundf(float x) * * - -
|
||||||
float truncf(float x) - * - -
|
float truncf(float x) * * - -
|
||||||
|
float ceilf(float x) * - - -
|
||||||
```
|
```
|
||||||
|
|
||||||
### extra functions
|
### extra functions
|
||||||
|
|
||||||
optional utility functions.
|
Optional utility functions.
|
||||||
```
|
```
|
||||||
func description softfloat cbmfp wozfp 754
|
func description softfloat cbmfp wozfp 754
|
||||||
|
|
||||||
|
|
12
libsrc/float/softmath/readme.txt
Normal file
12
libsrc/float/softmath/readme.txt
Normal 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)
|
13
libsrc/float/softmath/softmath-ceilf.c
Normal file
13
libsrc/float/softmath/softmath-ceilf.c
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
float ceilf(float x)
|
||||||
|
{
|
||||||
|
int n = (int) x;
|
||||||
|
|
||||||
|
if (n >= x) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n + 1;
|
||||||
|
}
|
67
libsrc/float/softmath/softmath-cosf.c
Normal file
67
libsrc/float/softmath/softmath-cosf.c
Normal 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
|
6
libsrc/float/softmath/softmath-fabsf.c
Normal file
6
libsrc/float/softmath/softmath-fabsf.c
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
float fabsf(float x) {
|
||||||
|
return x < 0.0 ? -x : x;
|
||||||
|
}
|
7
libsrc/float/softmath/softmath-powf.c
Normal file
7
libsrc/float/softmath/softmath-powf.c
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
float powf(float x, float y)
|
||||||
|
{
|
||||||
|
return expf(x * logf(y));
|
||||||
|
}
|
10
libsrc/float/softmath/softmath-roundf.c
Normal file
10
libsrc/float/softmath/softmath-roundf.c
Normal 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));
|
||||||
|
}
|
6
libsrc/float/softmath/softmath-sinf.c
Normal file
6
libsrc/float/softmath/softmath-sinf.c
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
float sinf(float x) {
|
||||||
|
return cosf(x - (M_PI / 2.0));
|
||||||
|
}
|
7
libsrc/float/softmath/softmath-truncf.c
Normal file
7
libsrc/float/softmath/softmath-truncf.c
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
float truncf(float x)
|
||||||
|
{
|
||||||
|
return (float)((signed)x);
|
||||||
|
}
|
|
@ -3,6 +3,10 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <conio.h> // for cgetc
|
#include <conio.h> // for cgetc
|
||||||
|
|
||||||
|
#ifdef __SIM6502__
|
||||||
|
#define cgetc()
|
||||||
|
#endif
|
||||||
|
|
||||||
char buf[100];
|
char buf[100];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -98,7 +102,7 @@ float roundtruncvals[22] = {
|
||||||
void roundtruncabs(void)
|
void roundtruncabs(void)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
printf("round/truncate:\n");
|
printf("round/trunc:\n");
|
||||||
for (n = 0; n < 21; n++) {
|
for (n = 0; n < 21; n++) {
|
||||||
printf("%12s", _ftostr(buf, roundtruncvals[n]));
|
printf("%12s", _ftostr(buf, roundtruncvals[n]));
|
||||||
printf("%12s", _ftostr(buf, roundf(roundtruncvals[n])));
|
printf("%12s", _ftostr(buf, roundf(roundtruncvals[n])));
|
||||||
|
@ -106,10 +110,11 @@ void roundtruncabs(void)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("<key>\n"); cgetc();
|
printf("<key>\n"); cgetc();
|
||||||
printf("abs:\n");
|
printf("abs/ceil:\n");
|
||||||
for (n = 0; n < 21; n++) {
|
for (n = 0; n < 21; n++) {
|
||||||
printf("%12s", _ftostr(buf, roundtruncvals[n]));
|
printf("%12s", _ftostr(buf, roundtruncvals[n]));
|
||||||
printf("%12s", _ftostr(buf, fabsf(roundtruncvals[n])));
|
printf("%12s", _ftostr(buf, fabsf(roundtruncvals[n])));
|
||||||
|
printf("%12s", _ftostr(buf, ceilf(roundtruncvals[n])));
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("<key>\n"); cgetc();
|
printf("<key>\n"); cgetc();
|
||||||
|
@ -202,6 +207,6 @@ int main(void)
|
||||||
{
|
{
|
||||||
roundtruncabs();
|
roundtruncabs();
|
||||||
sincostanatan();
|
sincostanatan();
|
||||||
powersqrt();
|
|
||||||
logexp();
|
logexp();
|
||||||
|
powersqrt();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user