1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
.TH LIBJ2_J2I_MUL_J2I 3 LIBJ2
.SH NAME
libj2_j2i_mul_j2i \- Calculate product
.SH SYNOPSIS
.nf
#include <libj2.h>
enum libj2_overflow {
\fILIBJ2_NO_OVERFLOW\fP = 0,
\fILIBJ2_POSITIVE_OVERFLOW\fP = 1,
\fILIBJ2_POSITIVE_OVERFLOW_UNKNOWN\fP = 2,
\fILIBJ2_NEGATIVE_OVERFLOW\fP = -1,
\fILIBJ2_NEGATIVE_OVERFLOW_UNKNOWN\fP = -2
};
#define \fILIBJ2_OVERFLOW\fP LIBJ2_POSITIVE_OVERFLOW
#define \fILIBJ2_OVERFLOW_UNKNOWN\fP LIBJ2_POSITIVE_OVERFLOW_UNKNOWN
void \fBlibj2_j2i_mul_j2i\fP(struct libj2_j2i *\fIa\fP, const struct libj2_j2i *\fIb\fP);
void \fBlibj2_j2i_mul_j2i_to_j2i\fP(const struct libj2_j2i *\fIa\fP, const struct libj2_j2i *\fIb\fP, struct libj2_j2i *\fIr\fP);
void \fBlibj2_j2i_mul_ji\fP(struct libj2_j2i *\fIa\fP, intmax_t \fIb\fP);
void \fBlibj2_j2i_mul_ji_to_j2i\fP(const struct libj2_j2i *\fIa\fP, intmax_t \fIb\fP, struct libj2_j2i *\fIr\fP);
void \fBlibj2_ji_mul_j2i_to_j2i\fP(intmax_t \fIa\fP, const struct libj2_j2i *\fIb\fP, struct libj2_j2i *\fIr\fP);
void \fBlibj2_ji_mul_ji_to_j2i\fP(intmax_t \fIa\fP, intmax_t \fIb\fP, struct libj2_j2i *\fIr\fP);
int \fBlibj2_j2i_mul_j2i_overflow\fP(struct libj2_j2i *\fIa\fP, const struct libj2_j2i *\fIb\fP);
int \fBlibj2_j2i_mul_j2i_to_j2i_overflow\fP(const struct libj2_j2i *\fIa\fP, const struct libj2_j2i *\fIb\fP, struct libj2_j2i *\fIr\fP);
int \fBlibj2_j2i_mul_ji_overflow\fP(struct libj2_j2i *\fIa\fP, intmax_t \fIb\fP);
int \fBlibj2_j2i_mul_ji_to_j2i_overflow\fP(const struct libj2_j2i *\fIa\fP, intmax_t \fIb\fP, struct libj2_j2i *\fIr\fP);
int \fBlibj2_ji_mul_j2i_to_j2i_overflow\fP(intmax_t \fIa\fP, const struct libj2_j2i *\fIb\fP, struct libj2_j2i *\fIr\fP);
int \fBlibj2_j2i_mul_j2i_overflow_p\fP(const struct libj2_j2i *\fIa\fP, const struct libj2_j2i *\fIb\fP);
int \fBlibj2_j2i_mul_ji_overflow_p\fP(const struct libj2_j2i *\fIa\fP, intmax_t \fIb\fP);
int \fBlibj2_ji_mul_j2i_overflow_p\fP(intmax_t \fIa\fP, const struct libj2_j2i *\fIb\fP);
enum libj2_overflow \fBlibj2_j2i_mul_j2i_overflow_p_quick\fP(const struct libj2_j2i *\fIa\fP, const struct libj2_j2i *\fIb\fP);
enum libj2_overflow \fBlibj2_j2i_mul_ji_overflow_p_quick\fP(const struct libj2_j2i *\fIa\fP, intmax_t \fIb\fP);
enum libj2_overflow \fBlibj2_ji_mul_j2i_overflow_p_quick\fP(intmax_t \fIa\fP, const struct libj2_j2i *\fIb\fP);
int \fBlibj2_j2i_mul_j2i_to_j2i_overflow_p\fP(const struct libj2_j2i *\fIa\fP, const struct libj2_j2i *\fIb\fP, struct libj2_j2i *\fIr\fP, int *\fIr_set\fP);
int \fBlibj2_j2i_mul_ji_to_j2i_overflow_p\fP(const struct libj2_j2i *\fIa\fP, intmax_t \fIb\fP, struct libj2_j2i *\fIr\fP, int *\fIr_set\fP);
int \fBlibj2_ji_mul_j2i_to_j2i_overflow_p\fP(intmax_t \fIa\fP, const struct libj2_j2i *\fIb\fP, struct libj2_j2i *\fIr\fP, int *\fIr_set\fP);
void \fBlibj2_j2u_mul_j2u\fP(struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
void \fBlibj2_j2u_mul_j2u_to_j2u\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_j2u_mul_ju\fP(struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
void \fBlibj2_j2u_mul_ju_to_j2u\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_ju_mul_j2u_to_j2u\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_ju_mul_ju_to_j2u\fP(uintmax_t \fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_j2u_mul_j2u_destructive\fP(struct libj2_j2u *restrict \fIa\fP, struct libj2_j2u *restrict \fIb\fP);
int \fBlibj2_j2u_mul_j2u_overflow\fP(struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
int \fBlibj2_j2u_mul_j2u_to_j2u_overflow\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
int \fBlibj2_j2u_mul_ju_overflow\fP(struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
int \fBlibj2_j2u_mul_ju_to_j2u_overflow\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
int \fBlibj2_ju_mul_j2u_to_j2u_overflow\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
int \fBlibj2_j2u_mul_j2u_overflow_destructive\fP(struct libj2_j2u *restrict \fIa\fP, struct libj2_j2u *restrict \fIb\fP);
int \fBlibj2_j2u_mul_j2u_overflow_p\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
int \fBlibj2_j2u_mul_ju_overflow_p\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
int \fBlibj2_ju_mul_j2u_overflow_p\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP);
enum libj2_overflow \fBlibj2_j2u_mul_j2u_overflow_p_quick\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
enum libj2_overflow \fBlibj2_j2u_mul_ju_overflow_p_quick\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
enum libj2_overflow \fBlibj2_ju_mul_j2u_overflow_p_quick\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP);
int \fBlibj2_j2u_mul_j2u_to_j2u_overflow_p\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP, int *\fIr_set\fP);
int \fBlibj2_j2u_mul_ju_to_j2u_overflow_p\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP, int *\fIr_set\fP);
int \fBlibj2_ju_mul_j2u_to_j2u_overflow_p\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP, int *\fIr_set\fP);
.fi
.PP
Link with
.IR -lj2 .
.SH DESCRIPTION
The above listed functions calculate the
product of the values of
.I a
and
.IR b .
The functions with the parameter
.I r
store the result in
.IR r ,
the other functions store the result in
.IR a ,
except the functions
.BR libj2_j2i_mul_j2i_overflow_p (),
.BR libj2_j2i_mul_ji_overflow_p (),
.BR libj2_ji_mul_j2i_overflow_p (),
.BR libj2_j2u_mul_j2u_overflow_p (),
.BR libj2_j2u_mul_ju_overflow_p (),
.BR libj2_ju_mul_j2u_overflow_p (),
.BR libj2_j2i_mul_j2i_overflow_p_quick (),
.BR libj2_j2i_mul_ji_overflow_p_quick (),
.BR libj2_ji_mul_j2i_overflow_p_quick (),
.BR libj2_j2u_mul_j2u_overflow_p_quick (),
.BR libj2_j2u_mul_ju_overflow_p_quick (),
and
.BR libj2_ju_mul_j2u_overflow_p_quick ()
only predict overflow, and do not calculate
the sum, and therefore does store it anywhere.
.PP
Additionally, in situations where the
.BR libj2_j2i_mul_j2i_overflow_p_quick (),
.BR libj2_j2i_mul_ji_overflow_p_quick (),
.BR libj2_ji_mul_j2i_overflow_p_quick (),
.BR libj2_j2u_mul_j2u_overflow_p_quick (),
.BR libj2_j2u_mul_ju_overflow_p_quick (),
and
.BR libj2_ju_mul_j2u_overflow_p_quick ()
functions return
.I LIBJ2_POSITIVE_OVERFLOW_UNKNOWN
or
.IR LIBJ2_NEGATIVE_OVERFLOW_UNKNOWN ,
the
.BR libj2_j2i_mul_j2i_to_j2i_overflow_p ()
.BR libj2_j2i_mul_ji_to_j2i_overflow_p ()
.BR libj2_ji_mul_j2i_to_j2i_overflow_p ()
.BR libj2_j2u_mul_j2u_to_j2u_overflow_p ()
.BR libj2_j2u_mul_ju_to_j2u_overflow_p (),
and
.BR libj2_ju_mul_j2u_to_j2u_overflow_p ()
functions will calculate the product, store it in
.I r
and set
.I *r_set
to 1; in other situations, those functions will
not modify
.I r
but will set
.I *r_set
to 0.
.PP
The value-returning functions (that is, those
with word
.B overflow
in their name), detect arithmetic overflow.
.PP
The result is truncated to the least significant
bits, as many as can be stored, in case of overflow;
that is, modular arithmetics is used.
.PP
The
.BR libj2_j2u_mul_j2u_destructive ()
and
.BR libj2_j2u_mul_j2u_overflow_destructive ()
functions modifies
.I b
arbitrarily, and do not support
.I a
and
.I b
being the same pointer.
.PP
The arguments are assumed to be
.RI non- NULL .
.SH RETURN VALUE
For functions with a return value, the functions
return +1 if the result is positive but too large
to be represented, -1 if the result is negative
(not only possible with functions where the result is a
.IR "struct libj2_j2i" )
but too large, and 0 otherwise.
For the
.BR libj2_j2i_mul_j2i_overflow_p_quick (),
.BR libj2_j2i_mul_ji_overflow_p_quick (),
.BR libj2_ji_mul_j2i_overflow_p_quick (),
.BR libj2_j2u_mul_j2u_overflow_p_quick (),
.BR libj2_j2u_mul_ju_overflow_p_quick (),
and
.BR libj2_ju_mul_j2u_overflow_p_quick ()
functions, the return value is a
.BR "enum libj2_overflow" ,
which may be unsigned, so to check if the
return value was negative, it is best to
first cast it to an integer, or compare it
directly to the negative constants. Additionally,
these functions will return
.I LIBJ2_POSITIVE_OVERFLOW_UNKNOWN
if the result is positive but if could not
be determined without calculating the
product whether the product is too large to
be represented, and
.I LIBJ2_NEGATIVE_OVERFLOW_UNKNOWN
if the result is negative but if could not
be determined without calculating the
product whether the product is too large to
be represented.
.PP
Since the
.BR libj2_j2u_mul_j2u_overflow_p_quick (),
.BR libj2_j2u_mul_ju_overflow_p_quick (),
and
.BR libj2_ju_mul_j2u_overflow_p_quick ()
functions operate on unsigned integers, they
will never return
.I LIBJ2_NEGATIVE_OVERFLOW
or
.IR LIBJ2_NEGATIVE_OVERFLOW_UNKNOWN .
.SH ERRORS
The above listed functions cannot fail.
.SH HISTORY
The
.BR libj2_j2u_mul_j2u (),
.BR libj2_j2u_mul_j2u_to_j2u (),
.BR libj2_j2u_mul_ju (),
.BR libj2_j2u_mul_ju_to_j2u (),
.BR libj2_ju_mul_j2u_to_j2u (),
.BR libj2_ju_mul_ju_to_j2u (),
.BR libj2_j2u_mul_j2u_destructive (),
.BR libj2_j2u_mul_j2u_overflow (),
.BR libj2_j2u_mul_j2u_to_j2u_overflow (),
.BR libj2_j2u_mul_ju_overflow (),
.BR libj2_j2u_mul_ju_to_j2u_overflow (),
.BR libj2_ju_mul_j2u_to_j2u_overflow (),
.BR libj2_j2u_mul_j2u_overflow_destructive (),
.BR libj2_j2u_mul_j2u_overflow_p (),
.BR libj2_j2u_mul_ju_overflow_p (),
.BR libj2_ju_mul_j2u_overflow_p (),
.BR libj2_j2u_mul_j2u_overflow_p_quick (),
.BR libj2_j2u_mul_ju_overflow_p_quick (),
.BR libj2_ju_mul_j2u_overflow_p_quick (),
.BR libj2_j2u_mul_j2u_to_j2u_overflow_p (),
.BR libj2_j2u_mul_ju_to_j2u_overflow_p (),
and
.BR libj2_ju_mul_j2u_to_j2u_overflow_p ()
functions and the
.IR LIBJ2_NO_OVERFLOW ,
.IR LIBJ2_OVERFLOW ,
and
.IR LIBJ2_OVERFLOW_UNKNOWN
constants were added in version 1.0 of
.BR libj2 .
.PP
The
.BR libj2_j2i_mul_j2i (),
.BR libj2_j2i_mul_j2i_to_j2i (),
.BR libj2_j2i_mul_ji (),
.BR libj2_j2i_mul_ji_to_j2i (),
.BR libj2_ji_mul_j2i_to_j2i (),
.BR libj2_ji_mul_ji_to_j2i (),
.BR libj2_j2i_mul_j2i_overflow (),
.BR libj2_j2i_mul_j2i_to_j2i_overflow (),
.BR libj2_j2i_mul_ji_overflow (),
.BR libj2_j2i_mul_ji_to_j2i_overflow (),
.BR libj2_ji_mul_j2i_to_j2i_overflow (),
.BR libj2_j2i_mul_j2i_overflow_p (),
.BR libj2_j2i_mul_ji_overflow_p (),
.BR libj2_ji_mul_j2i_overflow_p (),
.BR libj2_j2i_mul_j2i_overflow_p_quick (),
.BR libj2_j2i_mul_ji_overflow_p_quick (),
.BR libj2_ji_mul_j2i_overflow_p_quick (),
.BR libj2_j2i_mul_j2i_to_j2i_overflow_p (),
.BR libj2_j2i_mul_ji_to_j2i_overflow_p (),
and
.BR libj2_ji_mul_j2i_to_j2i_overflow_p ()
functions and the
.IR LIBJ2_POSITIVE_OVERFLOW ,
.IR LIBJ2_POSITIVE_OVERFLOW_UNKNOWN ,
.IR LIBJ2_NEGATIVE_OVERFLOW ,
and
.IR LIBJ2_NEGATIVE_OVERFLOW_UNKNOWN
constants were added in version 1.1 of
.BR libj2 .
.SH SEE ALSO
.BR libj2 (7),
.BR libj2_j2i_add_j2i (3),
.BR libj2_j2i_sub_j2i (3),
.BR libj2_j2u_sub_j2u (3),
.BR libj2_j2i_divmod_j2i (3)
|