php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
raisemod.c
Go to the documentation of this file.
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. */
38{
39 bc_num power, exponent, modulus, parity, temp;
40
41 /* Check the base for scale digits. */
42 if (base->n_scale != 0) {
44 }
45 /* Check the exponent for scale digits. */
46 if (expo->n_scale != 0) {
48 }
49 if (bc_is_neg(expo)) {
50 return EXPO_IS_NEGATIVE;
51 }
52 /* Check the modulus for scale digits. */
53 if (mod->n_scale != 0) {
54 return MOD_HAS_FRACTIONAL;
55 }
56 /* Modulus cannot be 0 */
57 if (bc_is_zero(mod)) {
58 return MOD_IS_ZERO;
59 }
60
61 /* Any integer number mod 1 (or -1) must be equal to 0 */
62 if (_bc_do_compare(mod, BCG(_one_), mod->n_scale, false) == BCMATH_EQUAL) {
64 *result = bc_new_num(1, scale);
65 return OK;
66 }
67
68 /* Set initial values. */
69 power = bc_copy_num(base);
70 exponent = bc_copy_num(expo);
71 modulus = bc_copy_num(mod);
72 temp = bc_copy_num(BCG(_one_));
73 bc_init_num(&parity);
74
75 /* Do the calculation. */
76 while (!bc_is_zero(exponent)) {
77 (void) bc_divmod(exponent, BCG(_two_), &exponent, &parity, 0);
78 if (!bc_is_zero(parity)) {
79 bc_multiply_ex(temp, power, &temp, scale);
80 (void) bc_modulo(temp, modulus, &temp, scale);
81 }
82 bc_multiply_ex(power, power, &power, scale);
83 (void) bc_modulo(power, modulus, &power, scale);
84 }
85
86 /* Assign the value. */
87 bc_free_num (&power);
88 bc_free_num (&exponent);
89 bc_free_num (&modulus);
91 bc_free_num (&parity);
92 *result = temp;
93 return OK;
94}
bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale)
Definition divmod.c:86
void bc_init_num(bc_num *num)
Definition init.c:108
bool bc_is_neg(bc_num num)
Definition neg.c:36
raise_mod_status
Definition bcmath.h:167
@ MOD_HAS_FRACTIONAL
Definition bcmath.h:172
@ OK
Definition bcmath.h:168
@ BASE_HAS_FRACTIONAL
Definition bcmath.h:169
@ EXPO_HAS_FRACTIONAL
Definition bcmath.h:170
@ EXPO_IS_NEGATIVE
Definition bcmath.h:171
@ MOD_IS_ZERO
Definition bcmath.h:173
bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale)
Definition divmod.c:44
bool bc_is_zero(bc_num num)
Definition zero.c:58
struct bc_struct * bc_num
Definition bcmath.h:39
#define bc_free_num(num)
Definition bcmath.h:187
@ BCMATH_EQUAL
Definition bcmath.h:114
#define bc_new_num(length, scale)
Definition bcmath.h:185
#define bc_multiply_ex(n1, n2, result, scale_min)
Definition bcmath.h:149
bcmath_compare_result _bc_do_compare(bc_num n1, bc_num n2, size_t scale, bool use_sign)
Definition compare.c:42
bc_num _two_
Definition php_bcmath.h:35
#define BCG(v)
Definition php_bcmath.h:46
bc_num _one_
Definition php_bcmath.h:34
raise_mod_status bc_raisemod(bc_num base, bc_num expo, bc_num mod, bc_num *result, size_t scale)
Definition raisemod.c:37
size_t n_scale
Definition bcmath.h:43
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
bool result