xref: /PHP-8.3/ext/bcmath/libbcmath/src/raisemod.c (revision 709869c8)
1 /* raisemod.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 "private.h"
34 #include <stddef.h>
35 
36 /* Raise BASE to the EXPO power, reduced modulo MOD.  The result is placed in RESULT. */
bc_raisemod(bc_num base,bc_num expo,bc_num mod,bc_num * result,size_t scale)37 raise_mod_status bc_raisemod(bc_num base, bc_num expo, bc_num mod, bc_num *result, size_t scale)
38 {
39 	bc_num power, exponent, modulus, parity, temp;
40 	size_t rscale;
41 
42 	/* Check the base for scale digits. */
43 	if (base->n_scale != 0) {
44 		return BASE_HAS_FRACTIONAL;
45 	}
46 	/* Check the exponent for scale digits. */
47 	if (expo->n_scale != 0) {
48 		return EXPO_HAS_FRACTIONAL;
49 	}
50 	if (bc_is_neg(expo)) {
51 		return EXPO_IS_NEGATIVE;
52 	}
53 	/* Check the modulus for scale digits. */
54 	if (mod->n_scale != 0) {
55 		return MOD_HAS_FRACTIONAL;
56 	}
57 	/* Modulus cannot be 0 */
58 	if (bc_is_zero(mod)) {
59 		return MOD_IS_ZERO;
60 	}
61 
62 	/* Set initial values. */
63 	power = bc_copy_num(base);
64 	exponent = bc_copy_num(expo);
65 	modulus = bc_copy_num(mod);
66 	temp = bc_copy_num(BCG(_one_));
67 	bc_init_num(&parity);
68 
69 	/* Do the calculation. */
70 	rscale = MAX(scale, power->n_scale);
71 	if (!_bc_do_compare(modulus, BCG(_one_), false, false)) {
72 		bc_free_num (&temp);
73 		temp = bc_new_num (1, scale);
74 	} else {
75 		while (!bc_is_zero(exponent)) {
76 			(void) bc_divmod(exponent, BCG(_two_), &exponent, &parity, 0);
77 			if (!bc_is_zero(parity)) {
78 				bc_multiply(temp, power, &temp, rscale);
79 				(void) bc_modulo(temp, modulus, &temp, scale);
80 			}
81 			bc_multiply(power, power, &power, rscale);
82 			(void) bc_modulo(power, modulus, &power, scale);
83 		}
84 	}
85 
86 	/* Assign the value. */
87 	bc_free_num (&power);
88 	bc_free_num (&exponent);
89 	bc_free_num (&modulus);
90 	bc_free_num (result);
91 	bc_free_num (&parity);
92 	*result = temp;
93 	return OK;
94 }
95