c145159c6b
1) asin[f l]() use Newton’s method to converge on a solution. But Newton’s method converges very slowly (> 500,000 iterations) for values of x close to 1.0; and, in the case of asinl(), sometimes fails to converge (loops forever). The attached patch uses an trig identity for values of x > sqrt(2). The resultant functions converge in no more than 5 iterations, 6 for asinl(). 2) The NuttX erf[f l]() functions are based on Chebyshev fitting to a good guess. The problem there’s a bug in the implementation that causes the functions to blow up with x near -3.0. This patch fixes that problem. It should be noted that this method returns the error function erf(x) with fractional error less than 1.2E-07 and that’s fine for the float version erff(), but the same method is used for double and long double version which will yield only slightly better precision. This patch doesn't address the issue of lower precision for erf() and erfl(). 3) a faster version of copysignf() for floats is included.
109 lines
3.7 KiB
C
109 lines
3.7 KiB
C
/****************************************************************************
|
|
* libc/math/lib_copysignf.c
|
|
*
|
|
* Copyright (C) 2015, 2016 Gregory Nutt. All rights reserved.
|
|
* Authors: Gregory Nutt <gnutt@nuttx.org>
|
|
* Dave Marples <dave@marples.net>
|
|
*
|
|
* Replaced on 2016-07-30 by David Alession with a faster version of
|
|
* copysignf() from NetBSD with the following Copyright:
|
|
*
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
*
|
|
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
* Permission to use, copy, modify, and distribute this
|
|
* software is freely granted, provided that this notice
|
|
* is preserved.
|
|
*
|
|
* 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.
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
* used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* 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 OWNER 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.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
#include <nuttx/compiler.h>
|
|
|
|
#include <math.h>
|
|
#include <stdint.h>
|
|
|
|
/****************************************************************************
|
|
* Pre-processor Definitions
|
|
****************************************************************************/
|
|
|
|
/* Get a 32 bit int from a float. */
|
|
|
|
#define GET_FLOAT_WORD(i,d) \
|
|
do \
|
|
{ \
|
|
ieee_float_shape_type gf_u; \
|
|
gf_u.value = (d); \
|
|
(i) = gf_u.word; \
|
|
} while (0)
|
|
|
|
/* Set a float from a 32 bit int. */
|
|
|
|
#define SET_FLOAT_WORD(d,i) \
|
|
do \
|
|
{ \
|
|
ieee_float_shape_type sf_u; \
|
|
sf_u.word = (i); \
|
|
(d) = sf_u.value; \
|
|
} while (0)
|
|
|
|
/****************************************************************************
|
|
* Private Types
|
|
****************************************************************************/
|
|
|
|
/* union which permits us to convert between a float and a 32 bit int. */
|
|
|
|
typedef union
|
|
{
|
|
float value;
|
|
uint32_t word;
|
|
}
|
|
ieee_float_shape_type;
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
float copysignf(float x, float y)
|
|
{
|
|
uint32_t ix;
|
|
uint32_t iy;
|
|
|
|
GET_FLOAT_WORD(ix, x);
|
|
GET_FLOAT_WORD(iy, y);
|
|
SET_FLOAT_WORD(x, (ix & 0x7fffffff) | (iy & 0x80000000));
|
|
|
|
return x;
|
|
}
|