xref: /php-src/ext/bcmath/libbcmath/src/raise.c (revision 2c8662d6)
1 /* raise.c: bcmath library file. */
2 /*
3     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
4     Copyright (C) 2000 Philip A. Nelson
5 
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2 of the License, or (at your option) any later version.
10 
11     This library 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 GNU
14     Lesser General Public License for more details.  (LICENSE)
15 
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to:
18 
19       The Free Software Foundation, Inc.
20       59 Temple Place, Suite 330
21       Boston, MA 02111-1307 USA.
22 
23     You may contact the author by:
24        e-mail:  philnelson@acm.org
25       us-mail:  Philip A. Nelson
26                 Computer Science Department, 9062
27                 Western Washington University
28                 Bellingham, WA 98226-9062
29 
30 *************************************************************************/
31 
32 #include "bcmath.h"
33 #include <assert.h>
34 #include <stdbool.h>
35 #include <stddef.h>
36 
bc_square_ex(bc_num n1,bc_num * result,size_t scale_min)37 void bc_square_ex(bc_num n1, bc_num *result, size_t scale_min) {
38 	bc_num square_ex = bc_square(n1, scale_min);
39 	bc_free_num(result);
40 	*(result) = square_ex;
41 }
42 
43 /* Raise "base" to the "exponent" power.  The result is placed in RESULT.
44    Maximum exponent is LONG_MAX.  If a "exponent" is not an integer,
45    only the integer part is used.  */
bc_raise(bc_num base,long exponent,bc_num * result,size_t scale)46 bool bc_raise(bc_num base, long exponent, bc_num *result, size_t scale) {
47 	bc_num temp, power;
48 	size_t rscale;
49 	size_t pwrscale;
50 	size_t calcscale;
51 	bool is_neg;
52 
53 	/* Special case if exponent is a zero. */
54 	if (exponent == 0) {
55 		bc_free_num (result);
56 		*result = bc_copy_num(BCG(_one_));
57 		return true;
58 	}
59 
60 	/* Other initializations. */
61 	if (exponent < 0) {
62 		is_neg = true;
63 		exponent = -exponent;
64 		rscale = scale;
65 	} else {
66 		is_neg = false;
67 		rscale = MIN (base->n_scale * exponent, MAX(scale, base->n_scale));
68 	}
69 
70 	/* Set initial value of temp. */
71 	power = bc_copy_num(base);
72 	pwrscale = base->n_scale;
73 	while ((exponent & 1) == 0) {
74 		pwrscale = 2 * pwrscale;
75 		bc_square_ex(power, &power, pwrscale);
76 		exponent = exponent >> 1;
77 	}
78 	temp = bc_copy_num(power);
79 	calcscale = pwrscale;
80 	exponent = exponent >> 1;
81 
82 	/* Do the calculation. */
83 	while (exponent > 0) {
84 		pwrscale = 2 * pwrscale;
85 		bc_square_ex(power, &power, pwrscale);
86 		if ((exponent & 1) == 1) {
87 			calcscale = pwrscale + calcscale;
88 			bc_multiply_ex(temp, power, &temp, calcscale);
89 		}
90 		exponent = exponent >> 1;
91 	}
92 
93 	/* Assign the value. */
94 	if (is_neg) {
95 		if (bc_divide(BCG(_one_), temp, result, rscale) == false) {
96 			bc_free_num (&temp);
97 			bc_free_num (&power);
98 			return false;
99 		}
100 		bc_free_num (&temp);
101 	} else {
102 		bc_free_num (result);
103 		*result = temp;
104 		(*result)->n_scale = MIN(scale, (*result)->n_scale);
105 	}
106 	bc_free_num (&power);
107 	return true;
108 }
109 
110 /* This is used internally by BCMath */
bc_raise_bc_exponent(bc_num base,bc_num expo,bc_num * result,size_t scale)111 void bc_raise_bc_exponent(bc_num base, bc_num expo, bc_num *result, size_t scale) {
112 	/* Exponent must not have fractional part */
113 	assert(expo->n_scale == 0);
114 
115 	long exponent = bc_num2long(expo);
116 	/* Exponent must be properly convertable to long */
117 	if (exponent == 0 && (expo->n_len > 1 || expo->n_value[0] != 0)) {
118 		assert(false && "Exponent is not well formed in internal call");
119 		//assert(exponent != 0 || (expo->n_len == 0 && expo->n_value[0] == 0));
120 	}
121 	//assert(exponent != 0 || (expo->n_len == 0 && expo->n_value[0] == 0));
122 	bc_raise(base, exponent, result, scale);
123 }
124 
125