xref: /netbsd-src/external/lgpl3/mpfr/dist/src/stack_interface.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1*ba125506Smrg /* custom interface -- initialize a floating-point number with given
2*ba125506Smrg    allocation area
3d59437c0Smrg 
4*ba125506Smrg Copyright 2005-2023 Free Software Foundation, Inc.
5efdec83bSmrg Contributed by the AriC and Caramba projects, INRIA.
6d59437c0Smrg 
7d59437c0Smrg This file is part of the GNU MPFR Library.
8d59437c0Smrg 
9d59437c0Smrg The GNU MPFR Library is free software; you can redistribute it and/or modify
10d59437c0Smrg it under the terms of the GNU Lesser General Public License as published by
11d59437c0Smrg the Free Software Foundation; either version 3 of the License, or (at your
12d59437c0Smrg option) any later version.
13d59437c0Smrg 
14d59437c0Smrg The GNU MPFR Library is distributed in the hope that it will be useful, but
15d59437c0Smrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16d59437c0Smrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17d59437c0Smrg License for more details.
18d59437c0Smrg 
19d59437c0Smrg You should have received a copy of the GNU Lesser General Public License
20d59437c0Smrg along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
212ba2404bSmrg https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22d59437c0Smrg 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23d59437c0Smrg 
24d59437c0Smrg #include "mpfr-impl.h"
25d59437c0Smrg 
26d59437c0Smrg #undef mpfr_custom_get_size
27d59437c0Smrg size_t
mpfr_custom_get_size(mpfr_prec_t prec)28d59437c0Smrg mpfr_custom_get_size (mpfr_prec_t prec)
29d59437c0Smrg {
30efdec83bSmrg   return MPFR_PREC2LIMBS (prec) * MPFR_BYTES_PER_MP_LIMB;
31d59437c0Smrg }
32d59437c0Smrg 
33d59437c0Smrg #undef mpfr_custom_init
34d59437c0Smrg void
mpfr_custom_init(void * mantissa,mpfr_prec_t prec)35d59437c0Smrg mpfr_custom_init (void *mantissa, mpfr_prec_t prec)
36d59437c0Smrg {
37d59437c0Smrg   return ;
38d59437c0Smrg }
39d59437c0Smrg 
40d59437c0Smrg #undef mpfr_custom_get_significand
41d59437c0Smrg void *
mpfr_custom_get_significand(mpfr_srcptr x)42d59437c0Smrg mpfr_custom_get_significand (mpfr_srcptr x)
43d59437c0Smrg {
44d59437c0Smrg   return (void*) MPFR_MANT (x);
45d59437c0Smrg }
46d59437c0Smrg 
47d59437c0Smrg #undef mpfr_custom_get_exp
48d59437c0Smrg mpfr_exp_t
mpfr_custom_get_exp(mpfr_srcptr x)49d59437c0Smrg mpfr_custom_get_exp (mpfr_srcptr x)
50d59437c0Smrg {
51d59437c0Smrg   return MPFR_EXP (x);
52d59437c0Smrg }
53d59437c0Smrg 
54d59437c0Smrg #undef mpfr_custom_move
55d59437c0Smrg void
mpfr_custom_move(mpfr_ptr x,void * new_position)56d59437c0Smrg mpfr_custom_move (mpfr_ptr x, void *new_position)
57d59437c0Smrg {
58d59437c0Smrg   MPFR_MANT (x) = (mp_limb_t *) new_position;
59d59437c0Smrg }
60d59437c0Smrg 
61d59437c0Smrg #undef mpfr_custom_init_set
62d59437c0Smrg void
mpfr_custom_init_set(mpfr_ptr x,int kind,mpfr_exp_t exp,mpfr_prec_t prec,void * mantissa)63d59437c0Smrg mpfr_custom_init_set (mpfr_ptr x, int kind, mpfr_exp_t exp,
64d59437c0Smrg                      mpfr_prec_t prec, void *mantissa)
65d59437c0Smrg {
66d59437c0Smrg   mpfr_kind_t t;
67d59437c0Smrg   int s;
68d59437c0Smrg   mpfr_exp_t e;
69d59437c0Smrg 
70d59437c0Smrg   if (kind >= 0)
71d59437c0Smrg     {
72d59437c0Smrg       t = (mpfr_kind_t) kind;
73d59437c0Smrg       s = MPFR_SIGN_POS;
74d59437c0Smrg     }
75d59437c0Smrg   else
76d59437c0Smrg     {
77d59437c0Smrg       t = (mpfr_kind_t) -kind;
78d59437c0Smrg       s = MPFR_SIGN_NEG;
79d59437c0Smrg     }
80d59437c0Smrg   MPFR_ASSERTD (t <= MPFR_REGULAR_KIND);
81d59437c0Smrg   e = MPFR_LIKELY (t == MPFR_REGULAR_KIND) ? exp :
82d59437c0Smrg     MPFR_UNLIKELY (t == MPFR_NAN_KIND) ? MPFR_EXP_NAN :
83d59437c0Smrg     MPFR_UNLIKELY (t == MPFR_INF_KIND) ? MPFR_EXP_INF : MPFR_EXP_ZERO;
84d59437c0Smrg 
85d59437c0Smrg   MPFR_PREC (x) = prec;
86d59437c0Smrg   MPFR_SET_SIGN (x, s);
87d59437c0Smrg   MPFR_EXP (x) = e;
88d59437c0Smrg   MPFR_MANT (x) = (mp_limb_t*) mantissa;
89d59437c0Smrg   return;
90d59437c0Smrg }
91d59437c0Smrg 
92d59437c0Smrg #undef mpfr_custom_get_kind
93d59437c0Smrg int
mpfr_custom_get_kind(mpfr_srcptr x)94d59437c0Smrg mpfr_custom_get_kind (mpfr_srcptr x)
95d59437c0Smrg {
96d59437c0Smrg   if (MPFR_LIKELY (!MPFR_IS_SINGULAR (x)))
97d59437c0Smrg     return (int) MPFR_REGULAR_KIND * MPFR_INT_SIGN (x);
98d59437c0Smrg   if (MPFR_IS_INF (x))
99d59437c0Smrg     return (int) MPFR_INF_KIND * MPFR_INT_SIGN (x);
100d59437c0Smrg   if (MPFR_IS_NAN (x))
101d59437c0Smrg     return (int) MPFR_NAN_KIND;
102d59437c0Smrg   MPFR_ASSERTD (MPFR_IS_ZERO (x));
103d59437c0Smrg   return (int) MPFR_ZERO_KIND * MPFR_INT_SIGN (x);
104d59437c0Smrg }
105d59437c0Smrg 
106