1 /* Include file for internal GNU MP types and definitions.
2 
3 Copyright (C) 1991, 1993, 1994, 1995, 1996, 2011 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
11 
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 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__)
38 typedef unsigned long int	mp_limb_t;
39 typedef long int		mp_limb_signed_t;
40 
41 typedef mp_limb_t *             mp_ptr;
42 typedef const mp_limb_t *	mp_srcptr;
43 typedef long int                mp_size_t;
44 typedef long int                mp_exp_t;
45 
46 /* Define stuff for longlong.h.  */
47 typedef unsigned int UQItype	__attribute__ ((mode (QI)));
48 typedef 	 int SItype	__attribute__ ((mode (SI)));
49 typedef unsigned int USItype	__attribute__ ((mode (SI)));
50 typedef		 int DItype	__attribute__ ((mode (DI)));
51 typedef unsigned int UDItype	__attribute__ ((mode (DI)));
52 
53 typedef mp_limb_t UWtype;
54 typedef 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.  */
103 void impn_mul_n_basecase (mp_ptr prodp, mp_srcptr up, mp_srcptr vp,
104 			  mp_size_t size) attribute_hidden;
105 void 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 
119 mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t)
120      attribute_hidden;
121 mp_limb_t mpn_addmul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t)
122      attribute_hidden;
123 int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t) attribute_hidden;
124 mp_limb_t mpn_divrem (mp_ptr, mp_size_t, mp_ptr, mp_size_t, mp_srcptr,
125 		      mp_size_t) attribute_hidden;
126 mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int)
127      attribute_hidden;
128 mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t)
129      attribute_hidden;
130 mp_limb_t mpn_mul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t)
131      attribute_hidden;
132 mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int)
133      attribute_hidden;
134 mp_limb_t mpn_sub_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t)
135      attribute_hidden;
136 mp_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)
140 mp_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 
149 static inline mp_limb_t
mpn_add_1(register mp_ptr res_ptr,register mp_srcptr s1_ptr,register mp_size_t s1_size,register mp_limb_t s2_limb)150 mpn_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