1 /* mpfr_add_si -- add a floating-point number with a machine integer
2    mpfr_sub_si -- sub a floating-point number with a machine integer
3    mpfr_si_sub -- sub a machine number with a floating-point number
4    mpfr_mul_si -- multiply a floating-point number by a machine integer
5    mpfr_div_si -- divide a floating-point number by a machine integer
6    mpfr_si_div -- divide a machine number by a floating-point number
7 
8 Copyright 2004-2020 Free Software Foundation, Inc.
9 Contributed by the AriC and Caramba projects, INRIA.
10 
11 This file is part of the GNU MPFR Library.
12 
13 The GNU MPFR Library is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
17 
18 The GNU MPFR Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21 License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
25 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
26 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
27 
28 #include "mpfr-impl.h"
29 
30 int
mpfr_add_si(mpfr_ptr y,mpfr_srcptr x,long int u,mpfr_rnd_t rnd_mode)31 mpfr_add_si (mpfr_ptr y, mpfr_srcptr x, long int u, mpfr_rnd_t rnd_mode)
32 {
33   int res;
34 
35   MPFR_LOG_FUNC
36     (("x[%Pu]=%.*Rg u=%ld rnd=%d",
37       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
38      ("y[%Pu]=%.*Rg inexact=%d",
39       mpfr_get_prec(y), mpfr_log_prec, y, res));
40 
41   if (u >= 0)
42     res = mpfr_add_ui (y, x, u, rnd_mode);
43   else
44     res = mpfr_sub_ui (y, x, - (unsigned long) u, rnd_mode);
45 
46   return res;
47 }
48 
49 int
mpfr_sub_si(mpfr_ptr y,mpfr_srcptr x,long int u,mpfr_rnd_t rnd_mode)50 mpfr_sub_si (mpfr_ptr y, mpfr_srcptr x, long int u, mpfr_rnd_t rnd_mode)
51 {
52   int res;
53 
54   MPFR_LOG_FUNC
55     (("x[%Pu]=%.*Rg u=%ld rnd=%d",
56       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
57      ("y[%Pu]=%.*Rg inexact=%d",
58       mpfr_get_prec(y), mpfr_log_prec, y, res));
59 
60   if (u >= 0)
61     res = mpfr_sub_ui (y, x, u, rnd_mode);
62   else
63     res = mpfr_add_ui (y, x, - (unsigned long) u, rnd_mode);
64 
65   return res;
66 }
67 
68 int
mpfr_si_sub(mpfr_ptr y,long int u,mpfr_srcptr x,mpfr_rnd_t rnd_mode)69 mpfr_si_sub (mpfr_ptr y, long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
70 {
71   int res;
72 
73   MPFR_LOG_FUNC
74     (("x[%Pu]=%.*Rg u=%ld rnd=%d",
75       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
76      ("y[%Pu]=%.*Rg inexact=%d",
77       mpfr_get_prec(y), mpfr_log_prec, y, res));
78 
79   if (u >= 0)
80     res = mpfr_ui_sub (y, u, x, rnd_mode);
81   else
82     {
83       res = - mpfr_add_ui (y, x, - (unsigned long) u,
84                            MPFR_INVERT_RND (rnd_mode));
85       MPFR_CHANGE_SIGN (y);
86     }
87 
88   return res;
89 }
90 
91 #undef mpfr_mul_si
92 int
mpfr_mul_si(mpfr_ptr y,mpfr_srcptr x,long int u,mpfr_rnd_t rnd_mode)93 mpfr_mul_si (mpfr_ptr y, mpfr_srcptr x, long int u, mpfr_rnd_t rnd_mode)
94 {
95   int res;
96 
97   MPFR_LOG_FUNC
98     (("x[%Pu]=%.*Rg u=%ld rnd=%d",
99       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
100      ("y[%Pu]=%.*Rg inexact=%d",
101       mpfr_get_prec(y), mpfr_log_prec, y, res));
102 
103   if (u >= 0)
104     res = mpfr_mul_ui (y, x, u, rnd_mode);
105   else
106     {
107       res = - mpfr_mul_ui (y, x, - (unsigned long) u,
108                            MPFR_INVERT_RND (rnd_mode));
109       MPFR_CHANGE_SIGN (y);
110     }
111 
112   return res;
113 }
114 
115 #undef mpfr_div_si
116 int
mpfr_div_si(mpfr_ptr y,mpfr_srcptr x,long int u,mpfr_rnd_t rnd_mode)117 mpfr_div_si (mpfr_ptr y, mpfr_srcptr x, long int u, mpfr_rnd_t rnd_mode)
118 {
119   int res;
120 
121   MPFR_LOG_FUNC
122     (("x[%Pu]=%.*Rg u=%ld rnd=%d",
123       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
124      ("y[%Pu]=%.*Rg inexact=%d",
125       mpfr_get_prec(y), mpfr_log_prec, y, res));
126 
127   if (u >= 0)
128     res = mpfr_div_ui (y, x, u, rnd_mode);
129   else
130     {
131       res = - mpfr_div_ui (y, x, - (unsigned long) u,
132                            MPFR_INVERT_RND (rnd_mode));
133       MPFR_CHANGE_SIGN (y);
134     }
135 
136   return res;
137 }
138 
139 int
mpfr_si_div(mpfr_ptr y,long int u,mpfr_srcptr x,mpfr_rnd_t rnd_mode)140 mpfr_si_div (mpfr_ptr y, long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
141 {
142   int res;
143 
144   MPFR_LOG_FUNC
145     (("x[%Pu]=%.*Rg u=%ld rnd=%d",
146       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
147      ("y[%Pu]=%.*Rg inexact=%d",
148       mpfr_get_prec(y), mpfr_log_prec, y, res));
149 
150   if (u >= 0)
151     res = mpfr_ui_div (y, u, x, rnd_mode);
152   else
153     {
154       res = - mpfr_ui_div (y, - (unsigned long) u, x,
155                            MPFR_INVERT_RND(rnd_mode));
156       MPFR_CHANGE_SIGN (y);
157     }
158 
159   return res;
160 }
161