beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / aors_ui.h
blob2e3c481ab702ac06fd4d94d558b80a250ba4f0ae
1 /* mpz_add_ui, mpz_sub_ui -- Add or subtract an mpz_t and an unsigned
2 one-word integer.
4 Copyright 1991, 1993, 1994, 1996, 1999-2002, 2004, 2012, 2013 Free Software
5 Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free Software
19 Foundation; either version 2 of the License, or (at your option) any
20 later version.
22 or both in parallel, as here.
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 for more details.
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library. If not,
31 see https://www.gnu.org/licenses/. */
33 #include "gmp.h"
34 #include "gmp-impl.h"
37 #ifdef OPERATION_add_ui
38 #define FUNCTION mpz_add_ui
39 #define FUNCTION2 mpz_add
40 #define VARIATION_CMP >=
41 #define VARIATION_NEG
42 #define VARIATION_UNNEG -
43 #endif
45 #ifdef OPERATION_sub_ui
46 #define FUNCTION mpz_sub_ui
47 #define FUNCTION2 mpz_sub
48 #define VARIATION_CMP <
49 #define VARIATION_NEG -
50 #define VARIATION_UNNEG
51 #endif
53 #ifndef FUNCTION
54 Error, need OPERATION_add_ui or OPERATION_sub_ui
55 #endif
58 void
59 FUNCTION (mpz_ptr w, mpz_srcptr u, unsigned long int vval)
61 mp_srcptr up;
62 mp_ptr wp;
63 mp_size_t usize, wsize;
64 mp_size_t abs_usize;
66 #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */
67 if (vval > GMP_NUMB_MAX)
69 mpz_t v;
70 mp_limb_t vl[2];
71 PTR(v) = vl;
72 vl[0] = vval & GMP_NUMB_MASK;
73 vl[1] = vval >> GMP_NUMB_BITS;
74 SIZ(v) = 2;
75 FUNCTION2 (w, u, v);
76 return;
78 #endif
80 usize = SIZ (u);
81 if (usize == 0)
83 PTR (w)[0] = vval;
84 SIZ (w) = VARIATION_NEG (vval != 0);
85 return;
88 abs_usize = ABS (usize);
90 /* If not space for W (and possible carry), increase space. */
91 wp = MPZ_REALLOC (w, abs_usize + 1);
93 /* These must be after realloc (U may be the same as W). */
94 up = PTR (u);
96 if (usize VARIATION_CMP 0)
98 mp_limb_t cy;
99 cy = mpn_add_1 (wp, up, abs_usize, (mp_limb_t) vval);
100 wp[abs_usize] = cy;
101 wsize = VARIATION_NEG (abs_usize + cy);
103 else
105 /* The signs are different. Need exact comparison to determine
106 which operand to subtract from which. */
107 if (abs_usize == 1 && up[0] < vval)
109 wp[0] = vval - up[0];
110 wsize = VARIATION_NEG 1;
112 else
114 mpn_sub_1 (wp, up, abs_usize, (mp_limb_t) vval);
115 /* Size can decrease with at most one limb. */
116 wsize = VARIATION_UNNEG (abs_usize - (wp[abs_usize - 1] == 0));
120 SIZ (w) = wsize;