ReactOS 0.4.17-dev-1321-g5c122a9
log10.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS CRT
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Implementation of log10.
5 * COPYRIGHT: Imported from musl libc
6 * https://git.musl-libc.org/cgit/musl/tree/src/math/log10.c
7 * blob: 81026876b223d4f56d6c2542b18898d6105aa34d
8 * See https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
9 */
10
11/* origin: FreeBSD /usr/src/lib/msun/src/e_log10.c */
12/*
13 * ====================================================
14 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
15 *
16 * Developed at SunSoft, a Sun Microsystems, Inc. business.
17 * Permission to use, copy, modify, and distribute this
18 * software is freely granted, provided that this notice
19 * is preserved.
20 * ====================================================
21 */
22/*
23 * Return the base 10 logarithm of x. See log.c for most comments.
24 *
25 * Reduce x to 2^k (1+f) and calculate r = log(1+f) - f + f*f/2
26 * as in log.c, then combine and scale in extra precision:
27 * log10(x) = (f - f*f/2 + r)/log(10) + k*log10(2)
28 */
29
30#include <math.h>
31#include <stdint.h>
32
33#ifdef _MSC_VER
34#pragma function(log10)
35#endif
36
37static const double
38ivln10hi = 4.34294481878168880939e-01, /* 0x3fdbcb7b, 0x15200000 */
39ivln10lo = 2.50829467116452752298e-11, /* 0x3dbb9438, 0xca9aadd5 */
40log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
41log10_2lo = 3.69423907715893078616e-13, /* 0x3D59FEF3, 0x11F12B36 */
42Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
43Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
44Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
45Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
46Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
47Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
48Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
49
50double log10(double x)
51{
52 union {double f; uint64_t i;} u = {x};
53 double_t hfsq,f,s,z,R,w,t1,t2,dk,y,hi,lo,val_hi,val_lo;
54 uint32_t hx;
55 int k;
56
57 hx = u.i>>32;
58 k = 0;
59 if (hx < 0x00100000 || hx>>31) {
60 if (u.i<<1 == 0)
61 return -1/(x*x); /* log(+-0)=-inf */
62 if (hx>>31)
63 return (x-x)/0.0; /* log(-#) = NaN */
64 /* subnormal number, scale x up */
65 k -= 54;
66 x *= 0x1p54;
67 u.f = x;
68 hx = u.i>>32;
69 } else if (hx >= 0x7ff00000) {
70 return x;
71 } else if (hx == 0x3ff00000 && u.i<<32 == 0)
72 return 0;
73
74 /* reduce x into [sqrt(2)/2, sqrt(2)] */
75 hx += 0x3ff00000 - 0x3fe6a09e;
76 k += (int)(hx>>20) - 0x3ff;
77 hx = (hx&0x000fffff) + 0x3fe6a09e;
78 u.i = (uint64_t)hx<<32 | (u.i&0xffffffff);
79 x = u.f;
80
81 f = x - 1.0;
82 hfsq = 0.5*f*f;
83 s = f/(2.0+f);
84 z = s*s;
85 w = z*z;
86 t1 = w*(Lg2+w*(Lg4+w*Lg6));
87 t2 = z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
88 R = t2 + t1;
89
90 /* See log2.c for details. */
91 /* hi+lo = f - hfsq + s*(hfsq+R) ~ log(1+f) */
92 hi = f - hfsq;
93 u.f = hi;
94 u.i &= (uint64_t)-1<<32;
95 hi = u.f;
96 lo = f - hi - hfsq + s*(hfsq+R);
97
98 /* val_hi+val_lo ~ log10(1+f) + k*log10(2) */
99 val_hi = hi*ivln10hi;
100 dk = k;
101 y = dk*log10_2hi;
102 val_lo = dk*log10_2lo + (lo+hi)*ivln10lo + lo*ivln10hi;
103
104 /*
105 * Extra precision in for adding y is not strictly needed
106 * since there is no very large cancellation near x = sqrt(2) or
107 * x = 1/sqrt(2), but we do it anyway since it costs little on CPUs
108 * with some parallelism and it reduces the error for many args.
109 */
110 w = y + val_hi;
111 val_lo += (y - w) + val_hi;
112 val_hi = w;
113
114 return val_lo + val_hi;
115}
const UCHAR * dk
Definition: bcrypt.c:517
@ R
Definition: bidi.c:79
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
static const uint32_t k[]
Definition: sha256.c:24
double log10(double x)
Definition: freeldr.c:191
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLdouble s
Definition: gl.h:2039
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLfloat f
Definition: glext.h:7540
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
GLdouble GLdouble z
Definition: glext.h:5874
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
#define f
Definition: ke_i.h:83
#define uint64_t
Definition: nsiface.idl:62
static const double ivln10lo
Definition: log10.c:39
static const double Lg7
Definition: log10.c:48
static const double Lg6
Definition: log10.c:47
static const double log10_2lo
Definition: log10.c:41
static const double ivln10hi
Definition: log10.c:38
static const double Lg4
Definition: log10.c:45
static const double Lg5
Definition: log10.c:46
static const double log10_2hi
Definition: log10.c:40
static const double Lg3
Definition: log10.c:44
static const double Lg2
Definition: log10.c:43
static const double Lg1
Definition: log10.c:42