Limited decimal printing to sane precision.

Made C library able to access new math library.
UNIX compile defaults to using new math library.
Changed name of new math library to math.library.c
Added defines for new math library.
Renamed new soft floating point library to softfloat_library.c

git-svn-id: http://picoc.googlecode.com/svn/trunk@304 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-05-29 00:12:12 +00:00
parent 11e56d378c
commit a38afda0ab
5 changed files with 1000 additions and 259 deletions

View file

@ -1,9 +1,9 @@
CC=gcc
CFLAGS=-Wall -g -DUNIX_HOST #-DDEBUG_EXPRESSIONS #-DDEBUG_LEXER
LIBS=-lm
LIBS=#-lm
TARGET = picoc
SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c variable.c clibrary.c library_unix.c platform.c platform_unix.c
SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c variable.c clibrary.c library_unix.c platform.c platform_unix.c math_library.c
OBJS := $(SRCS:%.c=%.o)
all: depend $(TARGET)

View file

@ -97,6 +97,7 @@ void PrintInt(int Num, struct OutputStream *Stream)
void PrintFP(double Num, struct OutputStream *Stream)
{
int Exponent = 0;
int MaxDecimal;
if (Num < 0)
{
@ -115,7 +116,7 @@ void PrintFP(double Num, struct OutputStream *Stream)
Num = (Num - (int)Num) * 10;
if (abs(Num) >= 1e-7)
{
for (; abs(Num) >= 1e-7; Num = (Num - (int)(Num + 1e-7)) * 10)
for (MaxDecimal = 6; MaxDecimal > 0 && abs(Num) >= 1e-7; Num = (Num - (int)(Num + 1e-7)) * 10, MaxDecimal--)
PrintCh('0' + (int)(Num + 1e-7), Stream);
}
else
@ -313,110 +314,110 @@ void LibGetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value
ReturnValue->Val->Integer = PlatformGetCharacter();
}
#ifdef MATH_LIBRARY
#ifdef PICOC_MATH_LIBRARY
void LibSin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = sin(Param[0]->Val->FP);
ReturnValue->Val->FP = math_sin(Param[0]->Val->FP);
}
void LibCos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = cos(Param[0]->Val->FP);
ReturnValue->Val->FP = math_cos(Param[0]->Val->FP);
}
void LibTan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = tan(Param[0]->Val->FP);
ReturnValue->Val->FP = math_tan(Param[0]->Val->FP);
}
void LibAsin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = asin(Param[0]->Val->FP);
ReturnValue->Val->FP = math_asin(Param[0]->Val->FP);
}
void LibAcos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = acos(Param[0]->Val->FP);
ReturnValue->Val->FP = math_acos(Param[0]->Val->FP);
}
void LibAtan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = atan(Param[0]->Val->FP);
ReturnValue->Val->FP = math_atan(Param[0]->Val->FP);
}
void LibSinh(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = sinh(Param[0]->Val->FP);
ReturnValue->Val->FP = math_sinh(Param[0]->Val->FP);
}
void LibCosh(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = cosh(Param[0]->Val->FP);
ReturnValue->Val->FP = math_cosh(Param[0]->Val->FP);
}
void LibTanh(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = tanh(Param[0]->Val->FP);
ReturnValue->Val->FP = math_tanh(Param[0]->Val->FP);
}
void LibAsinh(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = asinh(Param[0]->Val->FP);
ReturnValue->Val->FP = math_asinh(Param[0]->Val->FP);
}
void LibAcosh(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = acosh(Param[0]->Val->FP);
ReturnValue->Val->FP = math_acosh(Param[0]->Val->FP);
}
void LibAtanh(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = atanh(Param[0]->Val->FP);
ReturnValue->Val->FP = math_atanh(Param[0]->Val->FP);
}
void LibExp(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = exp(Param[0]->Val->FP);
ReturnValue->Val->FP = math_exp(Param[0]->Val->FP);
}
void LibAbs(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
void LibFabs(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = abs(Param[0]->Val->FP);
ReturnValue->Val->FP = math_fabs(Param[0]->Val->FP);
}
void LibLog(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = log(Param[0]->Val->FP);
ReturnValue->Val->FP = math_log(Param[0]->Val->FP);
}
void LibLog10(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = log10(Param[0]->Val->FP);
ReturnValue->Val->FP = math_log10(Param[0]->Val->FP);
}
void LibPow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = pow(Param[0]->Val->FP, Param[1]->Val->FP);
ReturnValue->Val->FP = math_pow(Param[0]->Val->FP, Param[1]->Val->FP);
}
void LibSqrt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = sqrt(Param[0]->Val->FP);
ReturnValue->Val->FP = math_sqrt(Param[0]->Val->FP);
}
void LibRound(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = floor(Param[0]->Val->FP + 0.5); // XXX - fix for soft float
ReturnValue->Val->FP = math_floor(Param[0]->Val->FP + 0.5); // XXX - fix for soft float
}
void LibCeil(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = ceil(Param[0]->Val->FP);
ReturnValue->Val->FP = math_ceil(Param[0]->Val->FP);
}
void LibFloor(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{
ReturnValue->Val->FP = floor(Param[0]->Val->FP);
ReturnValue->Val->FP = math_floor(Param[0]->Val->FP);
}
#endif
@ -427,7 +428,7 @@ struct LibraryFunction CLibrary[] =
{ LibSPrintf, "char *sprintf(char *, char *, ...)" },
{ LibGets, "void gets(char *, int)" },
{ LibGetc, "int getchar()" },
#ifdef MATH_LIBRARY
#ifdef PICOC_MATH_LIBRARY
{ LibSin, "float sin(float)" },
{ LibCos, "float cos(float)" },
{ LibTan, "float tan(float)" },
@ -441,7 +442,7 @@ struct LibraryFunction CLibrary[] =
{ LibAcosh, "float acosh(float)" },
{ LibAtanh, "float atanh(float)" },
{ LibExp, "float exp(float)" },
{ LibAbs, "float fabs(float)" },
{ LibFabs, "float fabs(float)" },
{ LibLog, "float log(float)" },
{ LibLog10, "float log10(float)" },
{ LibPow, "float pow(float,float)" },

File diff suppressed because it is too large Load diff

View file

@ -30,6 +30,9 @@
#define PlatformSetExitPoint() setjmp(ExitBuf)
/* defines for the optional "fdlibm" maths library */
#define _IEEE_LIBM
/* host platform includes */
#ifdef UNIX_HOST
# include <stdio.h>
@ -44,7 +47,9 @@
# include <setjmp.h>
# ifndef NO_FP
# include <math.h>
# define MATH_LIBRARY
# define PICOC_MATH_LIBRARY
# define NEED_MATH_LIBRARY
# undef BIG_ENDIAN
#endif
extern jmp_buf ExitBuf;
@ -60,6 +65,7 @@ extern jmp_buf ExitBuf;
# include <setjmp.h>
# include <math.h>
# define assert(x)
# undef BIG_ENDIAN
# else
# ifdef SURVEYOR_HOST
@ -82,6 +88,7 @@ extern jmp_buf ExitBuf;
# undef INTERACTIVE_PROMPT_LINE
# define INTERACTIVE_PROMPT_STATEMENT "> "
# define INTERACTIVE_PROMPT_LINE "- "
# undef BIG_ENDIAN
# else
# ifdef UMON_HOST
# define NO_FP
@ -103,5 +110,48 @@ extern int ExitBuf[];
#endif
#endif /* PLATFORM_H */
#ifdef NEED_MATH_LIBRARY
extern double math_sin(double x);
extern double math_cos(double x);
extern double math_tan(double x);
extern double math_asin(double x);
extern double math_acos(double x);
extern double math_atan(double x);
extern double math_sinh(double x);
extern double math_cosh(double x);
extern double math_tanh(double x);
extern double math_asinh(double x);
extern double math_acosh(double x);
extern double math_atanh(double x);
extern double math_exp(double x);
extern double math_fabs(double x);
extern double math_log(double x);
extern double math_log10(double x);
extern double math_pow(double x, double y);
extern double math_sqrt(double x);
extern double math_floor(double x);
extern double math_ceil(double x);
#else /* NEED_MATH_LIBRARY */
#define math_sin(x) sin(x)
#define math_cos(x) cos(x)
#define math_tan(x) tan(x)
#define math_asin(x) asin(x)
#define math_acos(x) acos(x)
#define math_atan(x) atan(x)
#define math_sinh(x) sinh(x)
#define math_cosh(x) cosh(x)
#define math_tanh(x) tanh(x)
#define math_asinh(x) asinh(x)
#define math_acosh(x) acosh(x)
#define math_atanh(x) atanh(x)
#define math_exp(x) exp(x)
#define math_fabs(x) fabs(x)
#define math_log(x) log(x)
#define math_log10(x) log10(x)
#define math_pow(x,y) pow(x,y)
#define math_sqrt(x) sqrt(x)
#define math_floor(x) floor(x)
#define math_ceil(x) ceil(x)
#endif /* NEED_MATH_LIBRARY */
#endif /* PLATFORM_H */