si_op.c revision 1.1.1.3
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
8Copyright 2004-2018 Free Software Foundation, Inc.
9Contributed by the AriC and Caramba projects, INRIA.
10
11This file is part of the GNU MPFR Library.
12
13The GNU MPFR Library is free software; you can redistribute it and/or modify
14it under the terms of the GNU Lesser General Public License as published by
15the Free Software Foundation; either version 3 of the License, or (at your
16option) any later version.
17
18The GNU MPFR Library is distributed in the hope that it will be useful, but
19WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21License for more details.
22
23You should have received a copy of the GNU Lesser General Public License
24along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
25http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
2651 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
27
28#include "mpfr-impl.h"
29
30int
31mpfr_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
49int
50mpfr_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
68int
69mpfr_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
92int
93mpfr_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
116int
117mpfr_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
139int
140mpfr_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