1/* Include file for internal GNU MP types and definitions.
2
3Copyright (C) 1991, 1993, 1994, 1995, 1996, 2011 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 02111-1307, USA. */
21
22#include <stdlib.h>
23#include "quadmath-imp.h"
24
25#undef alloca
26#define alloca __builtin_alloca
27
28#define ABS(x) (x >= 0 ? x : -x)
29#ifndef MIN
30#define MIN(l,o) ((l) < (o) ? (l) : (o))
31#endif
32#ifndef MAX
33#define MAX(h,i) ((h) > (i) ? (h) : (i))
34#endif
35
36#define BITS_PER_MP_LIMB (__SIZEOF_LONG__ * __CHAR_BIT__)
37#define BYTES_PER_MP_LIMB (BITS_PER_MP_LIMB / __CHAR_BIT__)
38typedef unsigned long int	mp_limb_t;
39typedef long int		mp_limb_signed_t;
40
41typedef mp_limb_t *             mp_ptr;
42typedef const mp_limb_t *	mp_srcptr;
43typedef long int                mp_size_t;
44typedef long int                mp_exp_t;
45
46/* Define stuff for longlong.h.  */
47typedef unsigned int UQItype	__attribute__ ((mode (QI)));
48typedef 	 int SItype	__attribute__ ((mode (SI)));
49typedef unsigned int USItype	__attribute__ ((mode (SI)));
50typedef		 int DItype	__attribute__ ((mode (DI)));
51typedef unsigned int UDItype	__attribute__ ((mode (DI)));
52
53typedef mp_limb_t UWtype;
54typedef unsigned int UHWtype;
55#define W_TYPE_SIZE BITS_PER_MP_LIMB
56
57#ifdef HAVE_HIDDEN_VISIBILITY
58#define attribute_hidden __attribute__((__visibility__ ("hidden")))
59#else
60#define attribute_hidden
61#endif
62
63#include "longlong.h"
64
65/* Copy NLIMBS *limbs* from SRC to DST.  */
66#define MPN_COPY_INCR(DST, SRC, NLIMBS) \
67  do {									\
68    mp_size_t __i;							\
69    for (__i = 0; __i < (NLIMBS); __i++)				\
70      (DST)[__i] = (SRC)[__i];						\
71  } while (0)
72#define MPN_COPY_DECR(DST, SRC, NLIMBS) \
73  do {									\
74    mp_size_t __i;							\
75    for (__i = (NLIMBS) - 1; __i >= 0; __i--)				\
76      (DST)[__i] = (SRC)[__i];						\
77  } while (0)
78#define MPN_COPY MPN_COPY_INCR
79
80/* Zero NLIMBS *limbs* AT DST.  */
81#define MPN_ZERO(DST, NLIMBS) \
82  do {									\
83    mp_size_t __i;							\
84    for (__i = 0; __i < (NLIMBS); __i++)				\
85      (DST)[__i] = 0;							\
86  } while (0)
87
88#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
89  do {									\
90    if ((size) < KARATSUBA_THRESHOLD)					\
91      impn_mul_n_basecase (prodp, up, vp, size);			\
92    else								\
93      impn_mul_n (prodp, up, vp, size, tspace);			\
94  } while (0)
95
96#define __MPN(x) __quadmath_mpn_##x
97
98/* Internal mpn calls */
99#define impn_mul_n_basecase	__MPN(impn_mul_n_basecase)
100#define impn_mul_n		__MPN(impn_mul_n)
101
102/* Prototypes for internal mpn calls.  */
103void impn_mul_n_basecase (mp_ptr prodp, mp_srcptr up, mp_srcptr vp,
104			  mp_size_t size) attribute_hidden;
105void impn_mul_n (mp_ptr prodp, mp_srcptr up, mp_srcptr vp, mp_size_t size,
106		 mp_ptr tspace) attribute_hidden;
107
108#define mpn_add_n		__MPN(add_n)
109#define mpn_addmul_1		__MPN(addmul_1)
110#define mpn_cmp			__MPN(cmp)
111#define mpn_divrem		__MPN(divrem)
112#define mpn_lshift		__MPN(lshift)
113#define mpn_mul			__MPN(mul)
114#define mpn_mul_1		__MPN(mul_1)
115#define mpn_rshift		__MPN(rshift)
116#define mpn_sub_n		__MPN(sub_n)
117#define mpn_submul_1		__MPN(submul_1)
118
119mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t)
120     attribute_hidden;
121mp_limb_t mpn_addmul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t)
122     attribute_hidden;
123int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t) attribute_hidden;
124mp_limb_t mpn_divrem (mp_ptr, mp_size_t, mp_ptr, mp_size_t, mp_srcptr,
125		      mp_size_t) attribute_hidden;
126mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int)
127     attribute_hidden;
128mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t)
129     attribute_hidden;
130mp_limb_t mpn_mul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t)
131     attribute_hidden;
132mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int)
133     attribute_hidden;
134mp_limb_t mpn_sub_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t)
135     attribute_hidden;
136mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t)
137     attribute_hidden;
138
139#define mpn_extract_flt128 __MPN(extract_flt128)
140mp_size_t mpn_extract_flt128 (mp_ptr res_ptr, mp_size_t size, int *expt,
141			      int *is_neg, __float128 value) attribute_hidden;
142
143#define mpn_construct_float128 __MPN(construct_float128)
144__float128 mpn_construct_float128 (mp_srcptr frac_ptr, int expt, int sign)
145     attribute_hidden;
146
147#define mpn_divmod(qp,np,nsize,dp,dsize) mpn_divrem (qp,0,np,nsize,dp,dsize)
148
149static inline mp_limb_t
150mpn_add_1 (register mp_ptr res_ptr,
151	   register mp_srcptr s1_ptr,
152	   register mp_size_t s1_size,
153	   register mp_limb_t s2_limb)
154{
155  register mp_limb_t x;
156
157  x = *s1_ptr++;
158  s2_limb = x + s2_limb;
159  *res_ptr++ = s2_limb;
160  if (s2_limb < x)
161    {
162      while (--s1_size != 0)
163	{
164	  x = *s1_ptr++ + 1;
165	  *res_ptr++ = x;
166	  if (x != 0)
167	    goto fin;
168	}
169
170      return 1;
171    }
172
173 fin:
174  if (res_ptr != s1_ptr)
175    {
176      mp_size_t i;
177      for (i = 0; i < s1_size - 1; i++)
178	res_ptr[i] = s1_ptr[i];
179    }
180  return 0;
181}
182