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
|
.TH LIBJ2_J2U_SUB_J2U 3 LIBJ2
.SH NAME
libj2_j2u_sub_j2u \- Calculate difference
.SH SYNOPSIS
.nf
#include <libj2.h>
void \fBlibj2_j2u_sub_j2u\fP(struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
void \fBlibj2_j2u_sub_j2u_to_j2u\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_j2u_sub_ju\fP(struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
void \fBlibj2_j2u_sub_ju_to_j2u\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_ju_sub_j2u_to_j2u\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_ju_sub_ju_to_j2u\fP(uintmax_t \fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
int \fBlibj2_j2u_sub_j2u_overflow\fP(struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
int \fBlibj2_j2u_sub_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_sub_ju_overflow\fP(struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
int \fBlibj2_j2u_sub_ju_to_j2u_overflow\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
int \fBlibj2_ju_sub_j2u_to_j2u_overflow\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
int \fBlibj2_ju_sub_ju_to_j2u_overflow\fP(uintmax_t \fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
int \fBlibj2_j2u_sub_j2u_overflow_p\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
int \fBlibj2_j2u_sub_ju_overflow_p\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
int \fBlibj2_ju_sub_j2u_overflow_p\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP);
int \fBlibj2_ju_sub_ju_overflow_p\fP(uintmax_t \fIa\fP, uintmax_t \fIb\fP);
void \fBlibj2_j2u_sub_j2u_borrow\fP(struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP, int *\fIc\fP);
void \fBlibj2_j2u_sub_j2u_to_j2u_borrow\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP, int *\fIc\fP);
void \fBlibj2_j2u_rsub_j2u\fP(struct libj2_j2u *\fIb\fP, const struct libj2_j2u *\fIa\fP);
void \fBlibj2_j2u_rsub_ju\fP(struct libj2_j2u *\fIb\fP, uintmax_t \fIa\fP);
int \fBlibj2_j2u_rsub_j2u_overflow\fP(struct libj2_j2u *\fIb\fP, const struct libj2_j2u *\fIa\fP);
int \fBlibj2_j2u_rsub_ju_overflow\fP(struct libj2_j2u *\fIb\fP, uintmax_t \fIa\fP);
int \fBlibj2_j2u_rsub_j2u_overflow_p\fP(const struct libj2_j2u *\fIb\fP, const struct libj2_j2u *\fIa\fP);
int \fBlibj2_j2u_rsub_ju_overflow_p\fP(const struct libj2_j2u *\fIb\fP, uintmax_t \fIa\fP);
void \fBlibj2_j2u_rsub_j2u_borrow\fP(struct libj2_j2u *\fIb\fP, const struct libj2_j2u *\fIa\fP, int *\fIc\fP);
void \fBlibj2_j2u_sat_sub_j2u\fP(struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP);
void \fBlibj2_j2u_sat_sub_j2u_to_j2u\fP(const struct libj2_j2u *\fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_j2u_sat_sub_ju\fP(struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP);
void \fBlibj2_j2u_sat_sub_ju_to_j2u\fP(const struct libj2_j2u *\fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_ju_sat_sub_j2u_to_j2u\fP(uintmax_t \fIa\fP, const struct libj2_j2u *\fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_ju_sat_sub_ju_to_j2u\fP(uintmax_t \fIa\fP, uintmax_t \fIb\fP, struct libj2_j2u *\fIr\fP);
void \fBlibj2_j2u_sat_rsub_j2u\fP(struct libj2_j2u *\fIb\fP, const struct libj2_j2u *\fIa\fP);
void \fBlibj2_j2u_sat_rsub_ju\fP(struct libj2_j2u *\fIb\fP, uintmax_t \fIa\fP);
.fi
.PP
Link with
.IR -lj2 .
.SH DESCRIPTION
The above listed functions calculate the
difference of the values of
.I a
(the minuend)
and
.I b
(the subtrahend).
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_j2u_sub_j2u_overflow_p (),
.BR libj2_j2u_sub_ju_overflow_p (),
.BR libj2_ju_sub_j2u_overflow_p (),
.BR libj2_ju_sub_ju_overflow_p (),
.BR libj2_j2u_rsub_j2u_overflow_p (),
and
.BR libj2_j2u_rsub_ju_overflow_p ()
only predict overflow, and do not calculate
the difference, and therefore does store it
anywhere; and except the functions
.BR libj2_j2u_rsub_j2u (),
.BR libj2_j2u_rsub_ju (),
.BR libj2_j2u_rsub_j2u_overflow (),
.BR libj2_j2u_rsub_ju_overflow (),
.BR libj2_j2u_sat_rsub_j2u (),
and
.BR libj2_j2u_sat_rsub_ju ()
store the result in
.IR b .
.PP
The value-returning functions (that is, those
with word
.B overflow
in their name), detect arithmetic overflow.
.PP
The
.BR libj2_j2u_sub_j2u_borrow (),
.BR libj2_j2u_sub_j2u_to_j2u_borrow (),
and
.BR libj2_j2u_rsub_j2u_borrow ()
functions subtract
.IR *c ,
which must be 1 or 0,
from the result, and sets
.I *c
to 1 on overflow, and 0 otherwise.
.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. However, the
.BR sat_sub -
and
.BR sat_rsub -functions
use saturated arithmetics, meaning that the result
will be the maximum representable value on positive
overflow and the value zero on negative overflow.
.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 too large to be represented,
and 0 otherwise.
.SH ERRORS
The above listed functions cannot fail.
.SH HISTORY
The
.BR libj2_j2u_sub_j2u (),
.BR libj2_j2u_sub_j2u_to_j2u (),
.BR libj2_j2u_sub_ju (),
.BR libj2_j2u_sub_ju_to_j2u (),
.BR libj2_ju_sub_j2u_to_j2u (),
.BR libj2_ju_sub_ju_to_j2u (),
.BR libj2_j2u_sub_j2u_overflow (),
.BR libj2_j2u_sub_j2u_to_j2u_overflow (),
.BR libj2_j2u_sub_ju_overflow (),
.BR libj2_j2u_sub_ju_to_j2u_overflow (),
.BR libj2_ju_sub_j2u_to_j2u_overflow (),
.BR libj2_ju_sub_ju_to_j2u_overflow (),
.BR libj2_j2u_sub_j2u_overflow_p (),
.BR libj2_j2u_sub_ju_overflow_p (),
.BR libj2_ju_sub_j2u_overflow_p (),
.BR libj2_ju_sub_ju_overflow_p (),
.BR libj2_j2u_rsub_j2u (),
.BR libj2_j2u_rsub_ju (),
.BR libj2_j2u_rsub_j2u_overflow (),
.BR libj2_j2u_rsub_ju_overflow (),
.BR libj2_j2u_rsub_j2u_overflow_p (),
and
.BR libj2_j2u_rsub_ju_overflow_p ()
functions were added in version 1.0 of
.BR libj2 .
.PP
The
.BR libj2_j2u_sat_sub_j2u (),
.BR libj2_j2u_sat_sub_j2u_to_j2u (),
.BR libj2_j2u_sat_sub_ju (),
.BR libj2_j2u_sat_sub_ju_to_j2u (),
.BR libj2_ju_sat_sub_j2u_to_j2u (),
.BR libj2_ju_sat_sub_ju_to_j2u (),
.BR libj2_j2u_sat_rsub_j2u (),
and
.BR libj2_j2u_sat_rsub_ju ()
functions were added in version 1.1 of
.BR libj2 .
.PP
The
.BR libj2_j2u_sub_j2u_borrow (),
.BR libj2_j2u_sub_j2u_to_j2u_borrow (),
and
.BR libj2_j2u_rsub_j2u_borrow ()
functions were added in version 1.2 of
.BR libj2 .
.SH NOTES
The return value 1 represents negative
overflow. Positive overflow is impossible.
.SH SEE ALSO
.BR libj2 (7),
.BR libj2_j2i_sub_j2i (3),
.BR libj2_j2u_abs_diff_j2u (3),
.BR libj2_j2i_add_j2i (3),
.BR libj2_j2i_mul_j2i (3),
.BR libj2_j2i_divmod_j2i (3)
|