1/*
2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2018 Free Software Foundation, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19/************************************************************************/
20/* */
21/* MODULE_NAME:mplog.c */
22/* */
23/* FUNCTIONS: mplog */
24/* */
25/* FILES NEEDED: endian.h mpa.h mplog.h */
26/* mpexp.c */
27/* */
28/* Multi-Precision logarithm function subroutine (for precision p >= 4, */
29/* 2**(-1024) < x < 2**1024) and x is outside of the interval */
30/* [1-2**(-54),1+2**(-54)]. Upon entry, x should be set to the */
31/* multi-precision value of the input and y should be set into a multi- */
32/* precision value of an approximation of log(x) with relative error */
33/* bound of at most 2**(-52). The routine improves the accuracy of y. */
34/* */
35/************************************************************************/
36#include "endian.h"
37#include "mpa.h"
38
39void
40__mplog (mp_no *x, mp_no *y, int p)
41{
42 int i, m;
43 static const int mp[33] =
44 {
45 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
46 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
47 };
48 mp_no mpt1, mpt2;
49
50 /* Choose m. */
51 m = mp[p];
52
53 /* Perform m newton iterations to solve for y: exp(y) - x = 0. The
54 iterations formula is: y(n + 1) = y(n) + (x * exp(-y(n)) - 1). */
55 __cpy (y, &mpt1, p);
56 for (i = 0; i < m; i++)
57 {
58 mpt1.d[0] = -mpt1.d[0];
59 __mpexp (&mpt1, &mpt2, p);
60 __mul (x, &mpt2, &mpt1, p);
61 __sub (&mpt1, &__mpone, &mpt2, p);
62 __add (y, &mpt2, &mpt1, p);
63 __cpy (&mpt1, y, p);
64 }
65}
66