Sweden-Number/dlls/ntdll/math.c

282 lines
7.6 KiB
C
Raw Normal View History

/*
* Math functions
*
* Copyright 2021 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
*
* For functions copied from musl libc (http://musl.libc.org/):
* ====================================================
* Copyright 2005-2020 Rich Felker, et al.
*
* 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.
* ====================================================
*/
#include <math.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "ntdll_misc.h"
/* Copied from musl: src/internal/libm.h */
static inline float fp_barrierf(float x)
{
volatile float y = x;
return y;
}
/*********************************************************************
* abs (NTDLL.@)
*/
int CDECL abs( int i )
{
return i >= 0 ? i : -i;
}
/*********************************************************************
* atan (NTDLL.@)
*
* Copied from musl: src/math/atan.c
*/
double CDECL atan( double x )
{
static const double atanhi[] = {
4.63647609000806093515e-01,
7.85398163397448278999e-01,
9.82793723247329054082e-01,
1.57079632679489655800e+00,
};
static const double atanlo[] = {
2.26987774529616870924e-17,
3.06161699786838301793e-17,
1.39033110312309984516e-17,
6.12323399573676603587e-17,
};
static const double aT[] = {
3.33333333333329318027e-01,
-1.99999999998764832476e-01,
1.42857142725034663711e-01,
-1.11111104054623557880e-01,
9.09088713343650656196e-02,
-7.69187620504482999495e-02,
6.66107313738753120669e-02,
-5.83357013379057348645e-02,
4.97687799461593236017e-02,
-3.65315727442169155270e-02,
1.62858201153657823623e-02,
};
double w, s1, s2, z;
unsigned int ix, sign;
int id;
ix = *(ULONGLONG*)&x >> 32;
sign = ix >> 31;
ix &= 0x7fffffff;
if (ix >= 0x44100000) { /* if |x| >= 2^66 */
if (isnan(x))
return x;
z = atanhi[3] + 7.5231638452626401e-37;
return sign ? -z : z;
}
if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
if (ix < 0x3e400000) { /* |x| < 2^-27 */
if (ix < 0x00100000)
/* raise underflow for subnormal x */
fp_barrierf((float)x);
return x;
}
id = -1;
} else {
x = fabs(x);
if (ix < 0x3ff30000) { /* |x| < 1.1875 */
if (ix < 0x3fe60000) { /* 7/16 <= |x| < 11/16 */
id = 0;
x = (2.0 * x - 1.0) / (2.0 + x);
} else { /* 11/16 <= |x| < 19/16 */
id = 1;
x = (x - 1.0) / (x + 1.0);
}
} else {
if (ix < 0x40038000) { /* |x| < 2.4375 */
id = 2;
x = (x - 1.5) / (1.0 + 1.5 * x);
} else { /* 2.4375 <= |x| < 2^66 */
id = 3;
x = -1.0 / x;
}
}
}
/* end of argument reduction */
z = x * x;
w = z * z;
/* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
s1 = z * (aT[0] + w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
if (id < 0)
return x - x * (s1 + s2);
z = atanhi[id] - (x * (s1 + s2) - atanlo[id] - x);
return sign ? -z : z;
}
/*********************************************************************
* ceil (NTDLL.@)
*/
double CDECL ceil( double d )
{
return unix_funcs->ceil( d );
}
/*********************************************************************
* cos (NTDLL.@)
*/
double CDECL cos( double d )
{
return unix_funcs->cos( d );
}
/*********************************************************************
* fabs (NTDLL.@)
*/
double CDECL fabs( double d )
{
return unix_funcs->fabs( d );
}
/*********************************************************************
* floor (NTDLL.@)
*/
double CDECL floor( double d )
{
return unix_funcs->floor( d );
}
/*********************************************************************
* log (NTDLL.@)
*/
double CDECL log( double d )
{
return unix_funcs->log( d );
}
/*********************************************************************
* pow (NTDLL.@)
*/
double CDECL pow( double x, double y )
{
return unix_funcs->pow( x, y );
}
/*********************************************************************
* sin (NTDLL.@)
*/
double CDECL sin( double d )
{
return unix_funcs->sin( d );
}
/*********************************************************************
* sqrt (NTDLL.@)
*/
double CDECL sqrt( double d )
{
return unix_funcs->sqrt( d );
}
/*********************************************************************
* tan (NTDLL.@)
*/
double CDECL tan( double d )
{
return unix_funcs->tan( d );
}
#if (defined(__GNUC__) || defined(__clang__)) && defined(__i386__)
#define FPU_DOUBLE(var) double var; \
__asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
#define FPU_DOUBLES(var1,var2) double var1,var2; \
__asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
__asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
/*********************************************************************
* _CIcos (NTDLL.@)
*/
double CDECL _CIcos(void)
{
FPU_DOUBLE(x);
return cos(x);
}
/*********************************************************************
* _CIlog (NTDLL.@)
*/
double CDECL _CIlog(void)
{
FPU_DOUBLE(x);
return log(x);
}
/*********************************************************************
* _CIpow (NTDLL.@)
*/
double CDECL _CIpow(void)
{
FPU_DOUBLES(x,y);
return pow(x,y);
}
/*********************************************************************
* _CIsin (NTDLL.@)
*/
double CDECL _CIsin(void)
{
FPU_DOUBLE(x);
return sin(x);
}
/*********************************************************************
* _CIsqrt (NTDLL.@)
*/
double CDECL _CIsqrt(void)
{
FPU_DOUBLE(x);
return sqrt(x);
}
/*********************************************************************
* _ftol (NTDLL.@)
*/
LONGLONG CDECL _ftol(void)
{
FPU_DOUBLE(x);
return (LONGLONG)x;
}
#endif /* (defined(__GNUC__) || defined(__clang__)) && defined(__i386__) */