xref: /php-src/ext/bcmath/libbcmath/src/bcmath.h (revision 70acd6e9)
1 /* bcmath.h: bcmath library header.    	*/
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 #ifndef _BCMATH_H_
33 #define _BCMATH_H_
34 
35 #include <stddef.h>
36 
37 typedef enum {PLUS, MINUS} sign;
38 
39 typedef struct bc_struct *bc_num;
40 
41 typedef struct bc_struct {
42 	size_t        n_len;   /* The number of digits before the decimal point. */
43 	size_t        n_scale; /* The number of digits after the decimal point. */
44 	char         *n_value; /* The number. Not zero char terminated. */
45 	unsigned int  n_refs;  /* The number of pointers to this number. */
46 	sign          n_sign;
47 } bc_struct;
48 
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #include "zend.h"
54 #include <stdbool.h>
55 #include "zend_string.h"
56 
57 /* Needed for BCG() macro and PHP_ROUND_XXX */
58 #include "../../php_bcmath.h"
59 
60 /* The base used in storing the numbers in n_value above.
61    Currently, this MUST be 10. */
62 
63 #define BASE 10
64 
65 /*  Some useful macros and constants. */
66 
67 #define CH_VAL(c)     (c - '0')
68 #define BCD_CHAR(d)   (d + '0')
69 
70 #ifdef MIN
71 #undef MIN
72 #undef MAX
73 #endif
74 #define MAX(a, b)      ((a)>(b)?(a):(b))
75 #define MIN(a, b)      ((a)>(b)?(b):(a))
76 
77 #ifndef LONG_MAX
78 #define LONG_MAX 0x7ffffff
79 #endif
80 
81 
82 /* Function Prototypes */
83 
84 void bc_init_numbers(void);
85 
86 bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent);
87 
88 bc_num _bc_new_num_nonzeroed_ex(size_t length, size_t scale, bool persistent);
89 
90 void _bc_free_num_ex(bc_num *num, bool persistent);
91 
92 /* Make a copy of a number! Just increments the reference count! */
bc_copy_num(bc_num num)93 static inline bc_num bc_copy_num(bc_num num)
94 {
95 	num->n_refs++;
96 	return num;
97 }
98 
99 void bc_init_num(bc_num *num);
100 
101 bool bc_str2num(bc_num *num, const char *str, const char *end, size_t scale, bool auto_scale);
102 
103 zend_string *bc_num2str_ex(bc_num num, size_t scale);
104 
105 void bc_int2num(bc_num *num, int val);
106 
107 long bc_num2long(bc_num num);
108 
109 int bc_compare(bc_num n1, bc_num n2);
110 
111 bool bc_is_zero(bc_num num);
112 
113 bool bc_is_zero_for_scale(bc_num num, size_t scale);
114 
115 bool bc_is_near_zero(bc_num num, size_t scale);
116 
117 bool bc_is_neg(bc_num num);
118 
119 bc_num bc_add(bc_num n1, bc_num n2, size_t scale_min);
120 
121 #define bc_add_ex(n1, n2, result, scale_min) do {	\
122 	bc_num add_ex = bc_add(n1, n2, scale_min);		\
123 	bc_free_num (result);                           \
124 	*(result) = add_ex;                             \
125 } while (0)
126 
127 bc_num bc_sub(bc_num n1, bc_num n2, size_t scale_min);
128 
129 #define bc_sub_ex(n1, n2, result, scale_min) do {	\
130 	bc_num sub_ex = bc_sub(n1, n2, scale_min);		\
131 	bc_free_num (result);                           \
132 	*(result) = sub_ex;                             \
133 } while (0)
134 
135 bc_num bc_multiply(bc_num n1, bc_num n2, size_t scale);
136 
137 #define bc_multiply_ex(n1, n2, result, scale_min) do {	\
138 	bc_num mul_ex = bc_multiply(n1, n2, scale_min); \
139 	bc_free_num (result);                           \
140 	*(result) = mul_ex;                             \
141 } while (0)
142 
143 bool bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale);
144 
145 bool bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
146 
147 bool bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
148 
149 bc_num bc_floor_or_ceil(bc_num num, bool is_floor);
150 
151 void bc_round(bc_num num, zend_long places, zend_long mode, bc_num *result);
152 
153 typedef enum {
154 	OK,
155 	BASE_HAS_FRACTIONAL,
156 	EXPO_HAS_FRACTIONAL,
157 	EXPO_IS_NEGATIVE,
158 	MOD_HAS_FRACTIONAL,
159 	MOD_IS_ZERO
160 } raise_mod_status;
161 
162 raise_mod_status bc_raisemod(bc_num base, bc_num exponent, bc_num mod, bc_num *result, size_t scale);
163 
164 void bc_raise(bc_num base, long exponent, bc_num *resul, size_t scale);
165 
166 void bc_raise_bc_exponent(bc_num base, bc_num exponent, bc_num *resul, size_t scale);
167 
168 bool bc_sqrt(bc_num *num, size_t scale);
169 
170 /* Prototypes needed for external utility routines. */
171 #define bc_new_num(length, scale)			_bc_new_num_ex((length), (scale), 0)
172 #define bc_new_num_nonzeroed(length, scale)	_bc_new_num_nonzeroed_ex((length), (scale), 0)
173 #define bc_free_num(num)					_bc_free_num_ex((num), 0)
174 #define bc_num2str(num)						bc_num2str_ex((num), (num->n_scale))
175 
176 #endif
177