BTRFS: Reimplement TreeIterator, add some error checks and remove redundancies.
[haiku.git] / src / libs / mapm / mapm_rcp.c
blob59a36a1c7e1d13cbe1e1e3a3123150ce4c5d726c
2 /*
3 * M_APM - mapm_rcp.c
5 * Copyright (C) 2000 - 2007 Michael C. Ring
7 * Permission to use, copy, and distribute this software and its
8 * documentation for any purpose with or without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation.
13 * Permission to modify the software is granted. Permission to distribute
14 * the modified code is granted. Modifications are to be distributed by
15 * using the file 'license.txt' as a template to modify the file header.
16 * 'license.txt' is available in the official MAPM distribution.
18 * This software is provided "as is" without express or implied warranty.
22 * $Id: mapm_rcp.c,v 1.7 2007/12/03 01:46:46 mike Exp $
24 * This file contains the fast division and reciprocal functions
26 * $Log: mapm_rcp.c,v $
27 * Revision 1.7 2007/12/03 01:46:46 mike
28 * Update license
30 * Revision 1.6 2003/07/21 20:20:17 mike
31 * Modify error messages to be in a consistent format.
33 * Revision 1.5 2003/05/01 21:58:40 mike
34 * remove math.h
36 * Revision 1.4 2003/03/31 22:15:49 mike
37 * call generic error handling function
39 * Revision 1.3 2002/11/03 21:32:09 mike
40 * Updated function parameters to use the modern style
42 * Revision 1.2 2000/09/26 16:27:48 mike
43 * add some comments
45 * Revision 1.1 2000/09/26 16:16:00 mike
46 * Initial revision
49 #include "m_apm_lc.h"
51 /****************************************************************************/
52 void m_apm_divide(M_APM rr, int places, M_APM aa, M_APM bb)
54 M_APM tmp0, tmp1;
55 int sn, nexp, dplaces;
57 sn = aa->m_apm_sign * bb->m_apm_sign;
59 if (sn == 0) /* one number is zero, result is zero */
61 if (bb->m_apm_sign == 0)
63 M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_divide\', Divide by 0");
66 M_set_to_zero(rr);
67 return;
71 * Use the original 'Knuth' method for smaller divides. On the
72 * author's system, this was the *approx* break even point before
73 * the reciprocal method used below became faster.
76 if (places < 250)
78 M_apm_sdivide(rr, places, aa, bb);
79 return;
82 /* mimic the decimal place behavior of the original divide */
84 nexp = aa->m_apm_exponent - bb->m_apm_exponent;
86 if (nexp > 0)
87 dplaces = nexp + places;
88 else
89 dplaces = places;
91 tmp0 = M_get_stack_var();
92 tmp1 = M_get_stack_var();
94 m_apm_reciprocal(tmp0, (dplaces + 8), bb);
95 m_apm_multiply(tmp1, tmp0, aa);
96 m_apm_round(rr, dplaces, tmp1);
98 M_restore_stack(2);
100 /****************************************************************************/
101 void m_apm_reciprocal(M_APM rr, int places, M_APM aa)
103 M_APM last_x, guess, tmpN, tmp1, tmp2;
104 char sbuf[32];
105 int ii, bflag, dplaces, nexp, tolerance;
107 if (aa->m_apm_sign == 0)
109 M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_reciprocal\', Input = 0");
111 M_set_to_zero(rr);
112 return;
115 last_x = M_get_stack_var();
116 guess = M_get_stack_var();
117 tmpN = M_get_stack_var();
118 tmp1 = M_get_stack_var();
119 tmp2 = M_get_stack_var();
121 m_apm_absolute_value(tmpN, aa);
124 normalize the input number (make the exponent 0) so
125 the 'guess' below will not over/under flow on large
126 magnitude exponents.
129 nexp = aa->m_apm_exponent;
130 tmpN->m_apm_exponent -= nexp;
132 m_apm_to_string(sbuf, 15, tmpN);
133 m_apm_set_double(guess, (1.0 / atof(sbuf)));
135 tolerance = places + 4;
136 dplaces = places + 16;
137 bflag = FALSE;
139 m_apm_negate(last_x, MM_Ten);
141 /* Use the following iteration to calculate the reciprocal :
144 X = X * [ 2 - N * X ]
148 ii = 0;
150 while (TRUE)
152 m_apm_multiply(tmp1, tmpN, guess);
153 m_apm_subtract(tmp2, MM_Two, tmp1);
154 m_apm_multiply(tmp1, tmp2, guess);
156 if (bflag)
157 break;
159 m_apm_round(guess, dplaces, tmp1);
161 /* force at least 2 iterations so 'last_x' has valid data */
163 if (ii != 0)
165 m_apm_subtract(tmp2, guess, last_x);
167 if (tmp2->m_apm_sign == 0)
168 break;
171 * if we are within a factor of 4 on the error term,
172 * we will be accurate enough after the *next* iteration
173 * is complete.
176 if ((-4 * tmp2->m_apm_exponent) > tolerance)
177 bflag = TRUE;
180 m_apm_copy(last_x, guess);
181 ii++;
184 m_apm_round(rr, places, tmp1);
185 rr->m_apm_exponent -= nexp;
186 rr->m_apm_sign = aa->m_apm_sign;
187 M_restore_stack(5);
189 /****************************************************************************/