[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tyndur-devel] [PATCH] Libc: Erstes Stueck richtige libm



On Sat, Aug 08, 2009 at 03:00:47PM +0200, Patrick Kaufmann wrote:
> From: patrick <patrick@xxxxxxxxxxxxxxxx>
> 
> + Libc: Erstes Stueck richtige libm
> 
> Signed-off-by: Patrick Kaufmann <patrick@xxxxxxxxxx>
> ---
>  src/modules/include/math.h            |   74 ++++++++++++----
>  src/modules/lib/stdlibc/math.c        |   45 ----------
>  src/modules/lib/stdlibc/math/fabs.c   |   67 ++++++++++++++
>  src/modules/lib/stdlibc/math/fcos.c   |   85 ++++++++++++++++++
>  src/modules/lib/stdlibc/math/fexp.c   |   58 ++++++++++++
>  src/modules/lib/stdlibc/math/fldexp.c |  112 +++++++++++++++++++++++
>  src/modules/lib/stdlibc/math/flog.c   |  157 +++++++++++++++++++++++++++++++++
>  src/modules/lib/stdlibc/math/fsin.c   |   83 +++++++++++++++++
>  src/modules/lib/stdlibc/math/fsqrt.c  |   64 +++++++++++++
>  src/modules/lib/stdlibc/math/ftan.c   |  143 ++++++++++++++++++++++++++++++
>  10 files changed, 826 insertions(+), 62 deletions(-)
>  delete mode 100644 src/modules/lib/stdlibc/math.c
>  create mode 100644 src/modules/lib/stdlibc/math/fabs.c
>  create mode 100644 src/modules/lib/stdlibc/math/fcos.c
>  create mode 100644 src/modules/lib/stdlibc/math/fexp.c
>  create mode 100644 src/modules/lib/stdlibc/math/fldexp.c
>  create mode 100644 src/modules/lib/stdlibc/math/flog.c
>  create mode 100644 src/modules/lib/stdlibc/math/fsin.c
>  create mode 100644 src/modules/lib/stdlibc/math/fsqrt.c
>  create mode 100644 src/modules/lib/stdlibc/math/ftan.c
> 
> diff --git a/src/modules/include/math.h b/src/modules/include/math.h
> index 8be2d9c..a5d39b4 100644
> --- a/src/modules/include/math.h
> +++ b/src/modules/include/math.h
> @@ -1,8 +1,8 @@
> -/*  
> - * Copyright (c) 2007 The tyndur Project. All rights reserved.
> +/*
> + * Copyright (c) 2009 The tyndur Project. All rights reserved.
>   *
>   * This code is derived from software contributed to the tyndur Project
> - * by Antoine Kaufmann.
> + * by Patrick Kaufmann.
>   *
>   * Redistribution and use in source and binary forms, with or without
>   * modification, are permitted provided that the following conditions
> @@ -13,26 +13,66 @@
>   *    notice, this list of conditions and the following disclaimer in the
>   *    documentation and/or other materials provided with the distribution.
>   *
> - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
>   * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
>   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> - * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR 
> - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
> - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
> - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
> - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
> - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
> - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
> + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
> + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
> + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
>   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> - */  
> + */
>  
>  #ifndef _MATH_H_
>  #define _MATH_H_
> -#include <lost/config.h>
>  
> -// Betrag einer reelen Zahl errechen
> -#ifndef CONFIG_LIBC_NO_STUBS
> -double fabs(double x);
> -#endif
> +# define NAN    (__builtin_nanf (""))

An dieser Stelle könnte man ruhig großzügiger Konstanten definieren. Die
Manpage zu math.h führt da einiges auf, z.B. M_E, M_PI oder M_PI_2, die du
stattdessen in der Implementierung gelegentlich definiert hast.

Ich gehe jetzt bei den folgenden Funktionen durch, ob sie mit dem Testprogramm
funktioniert haben (d.h. gleiches Ergebnis wie libm unter Linux) oder nicht.
Getestet worden ist jeweils die double-Variante.

> +
> +double      tan(double);
> +float       tanf(float);
> +long double tanl(long double);

Falsche Ergebnisse.

> +double      sqrt(double);
> +float       sqrtf(float);
> +long double sqrtl(long double);

Scheint den Originalwert zurückzugeben

> +double      sin(double);
> +float       sinf(float);
> +long double sinl(long double);

Ok.

>
> +double      log2(double);
> +float       log2f(float);
> +long double log2l(long double);

Ok.

> +double      log10(double);
> +float       log10f(float);
> +long double log10l(long double);

Falsche Ergebnisse.

> +double      log(double);
> +float       logf(float);
> +long double logl(long double);

Falsche Ergebnisse.

> +double      ldexp(double, int);
> +float       ldexpf(float, int);
> +long double ldexpl(long double, int);

Berechnet x^y statt x * 2^y, wenn ich das richtig sehe

> +double      fabs(double);
> +float       fabsf(float);
> +long double fabsl(long double);

Ok.

> +double      exp(double);
> +float       expf(float);
> +long double expl(long double);

Falsche Ergebnisse.

> +double      cos(double);
> +float       cosf(float);
> +long double cosl(long double);

Ok

> +double      atan(double);
> +float       atanf(float);
> +long double atanl(long double);

Ok

> +double      atan2(double, double);
> +float       atan2f(float, float);
> +long double atan2l(long double, long double);

Falsche Ergebnisse

> +double      asin(double);
> +float       asinf(float);
> +long double asinl(long double);
> +double      acos(double);
> +float       acosf(float);
> +long double acosl(long double);

Sehen ungenau aus, aber grob in Ordnung. Für 1.0 geben sie aber NaN zurück, das
ist nicht so optimal.

>
>  
>  #endif
> +
> diff --git a/src/modules/lib/stdlibc/math.c b/src/modules/lib/stdlibc/math.c
> deleted file mode 100644
> index 76d437f..0000000
> --- a/src/modules/lib/stdlibc/math.c
> +++ /dev/null
> @@ -1,45 +0,0 @@
> -/*
> - * Copyright (c) 2007 The tyndur Project. All rights reserved.
> - *
> - * This code is derived from software contributed to the tyndur Project
> - * by Antoine Kaufmann.
> - *
> - * Redistribution and use in source and binary forms, with or without
> - * modification, are permitted provided that the following conditions
> - * are met:
> - * 1. Redistributions of source code must retain the above copyright
> - *    notice, this list of conditions and the following disclaimer.
> - * 2. Redistributions in binary form must reproduce the above copyright
> - *    notice, this list of conditions and the following disclaimer in the
> - *    documentation and/or other materials provided with the distribution.
> - *
> - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> - * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
> - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
> - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
> - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
> - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
> - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
> - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
> - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> - */
> -
> -#include <stdio.h>
> -#include <math.h>
> -#include <lost/config.h>
> -
> -// FIXME: Nur bis der Float-Kram vom Kernel unterstuetzt wird
> -#ifndef CONFIG_LIBC_NO_STUBS
> -
> -/**
> - * Betrag einer reellen Zahl errechnen
> - */
> -double fabs(double x)
> -{
> -    return ((x < 0) ? -x : x);
> -}
> -
> -#endif
> -
> diff --git a/src/modules/lib/stdlibc/math/fabs.c b/src/modules/lib/stdlibc/math/fabs.c
> new file mode 100644
> index 0000000..59633da
> --- /dev/null
> +++ b/src/modules/lib/stdlibc/math/fabs.c
> @@ -0,0 +1,67 @@
> +/*
> + * Copyright (c) 2009 The tyndur Project. All rights reserved.
> + *
> + * This code is derived from software contributed to the tyndur Project
> + * by Patrick Kaufmann.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *    notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *    notice, this list of conditions and the following disclaimer in the
> + *    documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
> + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
> + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
> + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include "math.h"

Nicht <math.h> ?

> +#include <errno.h>
> +
> +double fabs(double x)
> +{
> +    double res;
> +    if (x < 0.0) {
> +        res = -x;
> +    } 

Ein überflüssiges Leerzeichen am Ende. Laut git am gibt es davon insgesamt
sieben im ganzen Patch.

Das else ist bei uns normal auch auf der Zeile vom }

> [...]
>
> diff --git a/src/modules/lib/stdlibc/math/ftan.c b/src/modules/lib/stdlibc/math/ftan.c
> new file mode 100644
> index 0000000..de2f2ba
> --- /dev/null
> +++ b/src/modules/lib/stdlibc/math/ftan.c
> @@ -0,0 +1,143 @@
> +/*
> + * Copyright (c) 2009 The tyndur Project. All rights reserved.
> + *
> + * This code is derived from software contributed to the tyndur Project
> + * by Patrick Kaufmann.
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *    notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *    notice, this list of conditions and the following disclaimer in the
> + *    documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
> + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
> + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
> + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
> + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
> + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
> + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
> + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
> + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#include "math.h"
> +#include <errno.h>
> +
> +#define pi 3.1415926535897932384
> +#define pio2 1.570796326794896619
> +
> +static short sgn(double x) {

Die geschweifte Klammer kommt auf eine neue Zeile

Ich glaube, das sollte mal vorerst an Kommentaren reichen ;-)