diff options
Diffstat (limited to 'Build/source/libs/gmp/gmp-6.0.0/mpq')
33 files changed, 2482 insertions, 0 deletions
diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/Makefile.am b/Build/source/libs/gmp/gmp-6.0.0/mpq/Makefile.am new file mode 100644 index 00000000000..406aa9f8dc6 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/Makefile.am @@ -0,0 +1,41 @@ +## Process this file with automake to generate Makefile.in + +# Copyright 1996, 1998, 2000-2002 Free Software Foundation, Inc. +# +# This file is part of the GNU MP Library. +# +# The GNU MP Library is free software; you can redistribute it and/or modify +# it under the terms of either: +# +# * the GNU Lesser General Public License as published by the Free +# Software Foundation; either version 3 of the License, or (at your +# option) any later version. +# +# or +# +# * the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any +# later version. +# +# or both in parallel, as here. +# +# The GNU MP Library is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received copies of the GNU General Public License and the +# GNU Lesser General Public License along with the GNU MP Library. If not, +# see https://www.gnu.org/licenses/. + + +INCLUDES = -D__GMP_WITHIN_GMP -I$(top_srcdir) + +noinst_LTLIBRARIES = libmpq.la +libmpq_la_SOURCES = \ + abs.c aors.c canonicalize.c clear.c clears.c \ + cmp.c cmp_si.c cmp_ui.c div.c equal.c \ + get_d.c get_den.c get_num.c get_str.c \ + init.c inits.c inp_str.c inv.c md_2exp.c mul.c neg.c out_str.c \ + set.c set_den.c set_num.c set_si.c set_str.c set_ui.c set_z.c set_d.c \ + set_f.c swap.c diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/abs.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/abs.c new file mode 100644 index 00000000000..7fe04a35bb6 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/abs.c @@ -0,0 +1,56 @@ +/* mpq_abs -- absolute value of a rational. + +Copyright 2000, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#define __GMP_FORCE_mpq_abs 1 + +#include "gmp.h" +#include "gmp-impl.h" + + +void +mpq_abs (mpq_ptr dst, mpq_srcptr src) +{ + mp_size_t num_abs_size = ABSIZ(NUM(src)); + + if (dst != src) + { + mp_size_t den_size = SIZ(DEN(src)); + mp_ptr dp; + + dp = MPZ_NEWALLOC (NUM(dst), num_abs_size); + MPN_COPY (dp, PTR(NUM(src)), num_abs_size); + + dp = MPZ_NEWALLOC (DEN(dst), den_size); + SIZ(DEN(dst)) = den_size; + MPN_COPY (dp, PTR(DEN(src)), den_size); + } + + SIZ(NUM(dst)) = num_abs_size; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/aors.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/aors.c new file mode 100644 index 00000000000..81edc1fa398 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/aors.c @@ -0,0 +1,113 @@ +/* mpq_add, mpq_sub -- add or subtract rational numbers. + +Copyright 1991, 1994-1997, 2000, 2001, 2004, 2005 Free Software Foundation, +Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + + +static void __gmpq_aors (REGPARM_3_1 (mpq_ptr, mpq_srcptr, mpq_srcptr, void (*) (mpz_ptr, mpz_srcptr, mpz_srcptr))) REGPARM_ATTR (1); +#define mpq_aors(w,x,y,fun) __gmpq_aors (REGPARM_3_1 (w, x, y, fun)) + +REGPARM_ATTR (1) static void +mpq_aors (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2, + void (*fun) (mpz_ptr, mpz_srcptr, mpz_srcptr)) +{ + mpz_t gcd; + mpz_t tmp1, tmp2; + mp_size_t op1_num_size = ABSIZ(NUM(op1)); + mp_size_t op1_den_size = SIZ(DEN(op1)); + mp_size_t op2_num_size = ABSIZ(NUM(op2)); + mp_size_t op2_den_size = SIZ(DEN(op2)); + TMP_DECL; + + TMP_MARK; + MPZ_TMP_INIT (gcd, MIN (op1_den_size, op2_den_size)); + MPZ_TMP_INIT (tmp1, op1_num_size + op2_den_size); + MPZ_TMP_INIT (tmp2, op2_num_size + op1_den_size); + + /* ROP might be identical to either operand, so don't store the + result there until we are finished with the input operands. We + dare to overwrite the numerator of ROP when we are finished + with the numerators of OP1 and OP2. */ + + mpz_gcd (gcd, DEN(op1), DEN(op2)); + if (! MPZ_EQUAL_1_P (gcd)) + { + mpz_t t; + + MPZ_TMP_INIT (t, MAX (op1_num_size + op2_den_size, + op2_num_size + op1_den_size) + 2 - SIZ(gcd)); + + mpz_divexact_gcd (t, DEN(op2), gcd); + mpz_divexact_gcd (tmp2, DEN(op1), gcd); + + mpz_mul (tmp1, NUM(op1), t); + mpz_mul (t, NUM(op2), tmp2); + + (*fun) (t, tmp1, t); + + mpz_gcd (gcd, t, gcd); + if (MPZ_EQUAL_1_P (gcd)) + { + mpz_set (NUM(rop), t); + mpz_mul (DEN(rop), DEN(op2), tmp2); + } + else + { + mpz_divexact_gcd (NUM(rop), t, gcd); + mpz_divexact_gcd (tmp1, DEN(op2), gcd); + mpz_mul (DEN(rop), tmp1, tmp2); + } + } + else + { + /* The common divisor is 1. This is the case (for random input) with + probability 6/(pi**2), which is about 60.8%. */ + mpz_mul (tmp1, NUM(op1), DEN(op2)); + mpz_mul (tmp2, NUM(op2), DEN(op1)); + (*fun) (NUM(rop), tmp1, tmp2); + mpz_mul (DEN(rop), DEN(op1), DEN(op2)); + } + TMP_FREE; +} + + +void +mpq_add (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2) +{ + mpq_aors (rop, op1, op2, mpz_add); +} + +void +mpq_sub (mpq_ptr rop, mpq_srcptr op1, mpq_srcptr op2) +{ + mpq_aors (rop, op1, op2, mpz_sub); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/canonicalize.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/canonicalize.c new file mode 100644 index 00000000000..882f408fb83 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/canonicalize.c @@ -0,0 +1,63 @@ +/* mpq_canonicalize(op) -- Remove common factors of the denominator and + numerator in OP. + +Copyright 1991, 1994-1996, 2000, 2001, 2005 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_canonicalize (mpq_t op) +{ + mpz_t gcd; + TMP_DECL; + + if (UNLIKELY (SIZ(DEN(op)) == 0)) + DIVIDE_BY_ZERO; + + TMP_MARK; + + /* ??? Dunno if the 1+ is needed. */ + MPZ_TMP_INIT (gcd, 1 + MAX (ABSIZ(NUM(op)), + ABSIZ(DEN(op)))); + + mpz_gcd (gcd, NUM(op), DEN(op)); + if (! MPZ_EQUAL_1_P (gcd)) + { + mpz_divexact_gcd (NUM(op), NUM(op), gcd); + mpz_divexact_gcd (DEN(op), DEN(op), gcd); + } + + if (SIZ(DEN(op)) < 0) + { + SIZ(NUM(op)) = -SIZ(NUM(op)); + SIZ(DEN(op)) = -SIZ(DEN(op)); + } + TMP_FREE; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/clear.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/clear.c new file mode 100644 index 00000000000..ce0b973f9f4 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/clear.c @@ -0,0 +1,41 @@ +/* mpq_clear -- free the space occupied by a mpq_t. + +Copyright 1991, 1994, 1995, 2000, 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_clear (mpq_t m) +{ + (*__gmp_free_func) (PTR(NUM(m)), + (size_t) ALLOC(NUM(m)) * GMP_LIMB_BYTES); + (*__gmp_free_func) (PTR(DEN(m)), + (size_t) ALLOC(DEN(m)) * GMP_LIMB_BYTES); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/clears.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/clears.c new file mode 100644 index 00000000000..f949fe36cd8 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/clears.c @@ -0,0 +1,52 @@ +/* mpq_clears() -- Clear multiple mpq_t variables. + +Copyright 2009, 2014 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include <stdarg.h> +#include <stdio.h> /* for NULL */ +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_clears (mpq_ptr x, ...) +{ + va_list ap; + + va_start (ap, x); + + while (x != NULL) + { + (*__gmp_free_func) (PTR(NUM(x)), + (size_t) ALLOC(NUM(x)) * GMP_LIMB_BYTES); + (*__gmp_free_func) (PTR(DEN(x)), + (size_t) ALLOC(DEN(x)) * GMP_LIMB_BYTES); + x = va_arg (ap, mpq_ptr); + } + va_end (ap); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp.c new file mode 100644 index 00000000000..e633b6f42d4 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp.c @@ -0,0 +1,126 @@ +/* mpq_cmp(u,v) -- Compare U, V. Return positive, zero, or negative + based on if U > V, U == V, or U < V. + +Copyright 1991, 1994, 1996, 2001, 2002, 2005 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" +#include "longlong.h" + +int +mpq_cmp (const mpq_t op1, const mpq_t op2) +{ + mp_size_t num1_size = SIZ(NUM(op1)); + mp_size_t den1_size = SIZ(DEN(op1)); + mp_size_t num2_size = SIZ(NUM(op2)); + mp_size_t den2_size = SIZ(DEN(op2)); + mp_size_t tmp1_size, tmp2_size; + mp_ptr tmp1_ptr, tmp2_ptr; + mp_size_t num1_sign; + int cc; + TMP_DECL; + + /* need canonical signs to get right result */ + ASSERT (den1_size > 0); + ASSERT (den2_size > 0); + + if (num1_size == 0) + return -num2_size; + if (num2_size == 0) + return num1_size; + if ((num1_size ^ num2_size) < 0) /* I.e. are the signs different? */ + return num1_size; + + num1_sign = num1_size; + num1_size = ABS (num1_size); + num2_size = ABS (num2_size); + + tmp1_size = num1_size + den2_size; + tmp2_size = num2_size + den1_size; + + /* 1. Check to see if we can tell which operand is larger by just looking at + the number of limbs. */ + + /* NUM1 x DEN2 is either TMP1_SIZE limbs or TMP1_SIZE-1 limbs. + Same for NUM1 x DEN1 with respect to TMP2_SIZE. */ + if (tmp1_size > tmp2_size + 1) + /* NUM1 x DEN2 is surely larger in magnitude than NUM2 x DEN1. */ + return num1_sign; + if (tmp2_size > tmp1_size + 1) + /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1. */ + return -num1_sign; + + /* 2. Same, but compare the number of significant bits. */ + { + int cnt1, cnt2; + mp_bitcnt_t bits1, bits2; + + count_leading_zeros (cnt1, PTR(NUM(op1))[num1_size - 1]); + count_leading_zeros (cnt2, PTR(DEN(op2))[den2_size - 1]); + bits1 = tmp1_size * GMP_NUMB_BITS - cnt1 - cnt2 + 2 * GMP_NAIL_BITS; + + count_leading_zeros (cnt1, PTR(NUM(op2))[num2_size - 1]); + count_leading_zeros (cnt2, PTR(DEN(op1))[den1_size - 1]); + bits2 = tmp2_size * GMP_NUMB_BITS - cnt1 - cnt2 + 2 * GMP_NAIL_BITS; + + if (bits1 > bits2 + 1) + return num1_sign; + if (bits2 > bits1 + 1) + return -num1_sign; + } + + /* 3. Finally, cross multiply and compare. */ + + TMP_MARK; + TMP_ALLOC_LIMBS_2 (tmp1_ptr,tmp1_size, tmp2_ptr,tmp2_size); + + if (num1_size >= den2_size) + tmp1_size -= 0 == mpn_mul (tmp1_ptr, + PTR(NUM(op1)), num1_size, + PTR(DEN(op2)), den2_size); + else + tmp1_size -= 0 == mpn_mul (tmp1_ptr, + PTR(DEN(op2)), den2_size, + PTR(NUM(op1)), num1_size); + + if (num2_size >= den1_size) + tmp2_size -= 0 == mpn_mul (tmp2_ptr, + PTR(NUM(op2)), num2_size, + PTR(DEN(op1)), den1_size); + else + tmp2_size -= 0 == mpn_mul (tmp2_ptr, + PTR(DEN(op1)), den1_size, + PTR(NUM(op2)), num2_size); + + + cc = tmp1_size - tmp2_size != 0 + ? tmp1_size - tmp2_size : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size); + TMP_FREE; + return num1_sign < 0 ? -cc : cc; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp_si.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp_si.c new file mode 100644 index 00000000000..0a31fe8c9e7 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp_si.c @@ -0,0 +1,67 @@ +/* _mpq_cmp_si -- compare mpq and long/ulong fraction. + +Copyright 2001, 2013 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + + +/* Something like mpq_cmpabs_ui would be more useful for the neg/neg case, + and perhaps a version accepting a parameter to reverse the test, to make + it a tail call here. */ + +int +_mpq_cmp_si (mpq_srcptr q, long n, unsigned long d) +{ + /* need canonical sign to get right result */ + ASSERT (SIZ(DEN(q)) > 0); + + if (SIZ(NUM(q)) >= 0) + { + if (n >= 0) + return _mpq_cmp_ui (q, n, d); /* >=0 cmp >=0 */ + else + return 1; /* >=0 cmp <0 */ + } + else + { + if (n >= 0) + return -1; /* <0 cmp >=0 */ + else + { + mpq_t qabs; + SIZ(NUM(qabs)) = ABSIZ(NUM(q)); + PTR(NUM(qabs)) = PTR(NUM(q)); + SIZ(DEN(qabs)) = SIZ(DEN(q)); + PTR(DEN(qabs)) = PTR(DEN(q)); + + return - _mpq_cmp_ui (qabs, NEG_CAST (unsigned long, n), d); /* <0 cmp <0 */ + } + } +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp_ui.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp_ui.c new file mode 100644 index 00000000000..a2e2d8ca812 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/cmp_ui.c @@ -0,0 +1,100 @@ +/* mpq_cmp_ui(u,vn,vd) -- Compare U with Vn/Vd. Return positive, zero, or + negative based on if U > V, U == V, or U < V. Vn and Vd may have + common factors. + +Copyright 1993, 1994, 1996, 2000-2003, 2005 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +int +_mpq_cmp_ui (const mpq_t op1, unsigned long int num2, unsigned long int den2) +{ + mp_size_t num1_size = SIZ(NUM(op1)); + mp_size_t den1_size = SIZ(DEN(op1)); + mp_size_t tmp1_size, tmp2_size; + mp_ptr tmp1_ptr, tmp2_ptr; + mp_limb_t cy_limb; + int cc; + TMP_DECL; + +#if GMP_NAIL_BITS != 0 + if ((num2 | den2) > GMP_NUMB_MAX) + { + mpq_t op2; + mpq_init (op2); + mpz_set_ui (mpq_numref (op2), num2); + mpz_set_ui (mpq_denref (op2), den2); + cc = mpq_cmp (op1, op2); + mpq_clear (op2); + return cc; + } +#endif + + /* need canonical sign to get right result */ + ASSERT (den1_size > 0); + + if (UNLIKELY (den2 == 0)) + DIVIDE_BY_ZERO; + + if (num1_size == 0) + return -(num2 != 0); + if (num1_size < 0) + return num1_size; + if (num2 == 0) + return num1_size; + + /* NUM1 x DEN2 is either TMP1_SIZE limbs or TMP1_SIZE-1 limbs. + Same for NUM1 x DEN1 with respect to TMP2_SIZE. */ + if (num1_size > den1_size + 1) + /* NUM1 x DEN2 is surely larger in magnitude than NUM2 x DEN1. */ + return num1_size; + if (den1_size > num1_size + 1) + /* NUM1 x DEN2 is surely smaller in magnitude than NUM2 x DEN1. */ + return -num1_size; + + TMP_MARK; + tmp1_ptr = TMP_ALLOC_LIMBS (num1_size + 1); + tmp2_ptr = TMP_ALLOC_LIMBS (den1_size + 1); + + cy_limb = mpn_mul_1 (tmp1_ptr, PTR(NUM(op1)), num1_size, + (mp_limb_t) den2); + tmp1_ptr[num1_size] = cy_limb; + tmp1_size = num1_size + (cy_limb != 0); + + cy_limb = mpn_mul_1 (tmp2_ptr, PTR(DEN(op1)), den1_size, + (mp_limb_t) num2); + tmp2_ptr[den1_size] = cy_limb; + tmp2_size = den1_size + (cy_limb != 0); + + cc = tmp1_size - tmp2_size != 0 + ? tmp1_size - tmp2_size : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size); + TMP_FREE; + return cc; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/div.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/div.c new file mode 100644 index 00000000000..7d91e0998ae --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/div.c @@ -0,0 +1,115 @@ +/* mpq_div -- divide two rational numbers. + +Copyright 1991, 1994-1996, 2000, 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + + +void +mpq_div (mpq_ptr quot, mpq_srcptr op1, mpq_srcptr op2) +{ + mpz_t gcd1, gcd2; + mpz_t tmp1, tmp2; + mpz_t numtmp; + mp_size_t op1_num_size; + mp_size_t op1_den_size; + mp_size_t op2_num_size; + mp_size_t op2_den_size; + mp_size_t alloc; + TMP_DECL; + + op2_num_size = ABSIZ(NUM(op2)); + + if (UNLIKELY (op2_num_size == 0)) + DIVIDE_BY_ZERO; + + op1_num_size = ABSIZ(NUM(op1)); + + if (op1_num_size == 0) + { + /* We special case this to simplify allocation logic; gcd(0,x) = x + is a singular case for the allocations. */ + SIZ(NUM(quot)) = 0; + PTR(DEN(quot))[0] = 1; + SIZ(DEN(quot)) = 1; + return; + } + + op2_den_size = SIZ(DEN(op2)); + op1_den_size = SIZ(DEN(op1)); + + TMP_MARK; + + alloc = MIN (op1_num_size, op2_num_size); + MPZ_TMP_INIT (gcd1, alloc); + + alloc = MIN (op1_den_size, op2_den_size); + MPZ_TMP_INIT (gcd2, alloc); + + alloc = MAX (op1_num_size, op2_num_size); + MPZ_TMP_INIT (tmp1, alloc); + + alloc = MAX (op1_den_size, op2_den_size); + MPZ_TMP_INIT (tmp2, alloc); + + alloc = op1_num_size + op2_den_size; + MPZ_TMP_INIT (numtmp, alloc); + + /* QUOT might be identical to either operand, so don't store the result there + until we are finished with the input operands. We can overwrite the + numerator of QUOT when we are finished with the numerators of OP1 and + OP2. */ + + mpz_gcd (gcd1, NUM(op1), NUM(op2)); + mpz_gcd (gcd2, DEN(op2), DEN(op1)); + + mpz_divexact_gcd (tmp1, NUM(op1), gcd1); + mpz_divexact_gcd (tmp2, DEN(op2), gcd2); + + mpz_mul (numtmp, tmp1, tmp2); + + mpz_divexact_gcd (tmp1, NUM(op2), gcd1); + mpz_divexact_gcd (tmp2, DEN(op1), gcd2); + + mpz_mul (DEN(quot), tmp1, tmp2); + + /* We needed to go via NUMTMP to take care of QUOT being the same as OP2. + Now move NUMTMP to QUOT->_mp_num. */ + mpz_set (NUM(quot), numtmp); + + /* Keep the denominator positive. */ + if (SIZ(DEN(quot)) < 0) + { + SIZ(DEN(quot)) = -SIZ(DEN(quot)); + SIZ(NUM(quot)) = -SIZ(NUM(quot)); + } + + TMP_FREE; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/equal.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/equal.c new file mode 100644 index 00000000000..500c2d802ea --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/equal.c @@ -0,0 +1,69 @@ +/* mpq_equal(u,v) -- Compare U, V. Return non-zero if they are equal, zero + if they are non-equal. + +Copyright 1996, 2001, 2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +int +mpq_equal (mpq_srcptr op1, mpq_srcptr op2) __GMP_NOTHROW +{ + mp_size_t num1_size, num2_size, den1_size, den2_size, i; + mp_srcptr num1_ptr, num2_ptr, den1_ptr, den2_ptr; + + /* need fully canonical for correct results */ + ASSERT_MPQ_CANONICAL (op1); + ASSERT_MPQ_CANONICAL (op2); + + num1_size = SIZ(NUM(op1)); + num2_size = SIZ(NUM(op2)); + if (num1_size != num2_size) + return 0; + + num1_ptr = PTR(NUM(op1)); + num2_ptr = PTR(NUM(op2)); + num1_size = ABS (num1_size); + for (i = 0; i < num1_size; i++) + if (num1_ptr[i] != num2_ptr[i]) + return 0; + + den1_size = SIZ(DEN(op1)); + den2_size = SIZ(DEN(op2)); + if (den1_size != den2_size) + return 0; + + den1_ptr = PTR(DEN(op1)); + den2_ptr = PTR(DEN(op2)); + for (i = 0; i < den1_size; i++) + if (den1_ptr[i] != den2_ptr[i]) + return 0; + + return 1; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/get_d.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_d.c new file mode 100644 index 00000000000..6318e222936 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_d.c @@ -0,0 +1,175 @@ +/* double mpq_get_d (mpq_t src) -- mpq to double, rounding towards zero. + +Copyright 1995, 1996, 2001-2005 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include <stdio.h> /* for NULL */ +#include "gmp.h" +#include "gmp-impl.h" +#include "longlong.h" + + +/* All that's needed is to get the high 53 bits of the quotient num/den, + rounded towards zero. More than 53 bits is fine, any excess is ignored + by mpn_get_d. + + N_QLIMBS is how many quotient limbs we need to satisfy the mantissa of a + double, assuming the highest of those limbs is non-zero. The target + qsize for mpn_tdiv_qr is then 1 more than this, since that function may + give a zero in the high limb (and non-zero in the second highest). + + The use of 8*sizeof(double) in N_QLIMBS is an overestimate of the + mantissa bits, but it gets the same result as the true value (53 or 48 or + whatever) when rounded up to a multiple of GMP_NUMB_BITS, for non-nails. + + Enhancements: + + Use the true mantissa size in the N_QLIMBS formula, to save a divide step + in nails. + + Examine the high limbs of num and den to see if the highest 1 bit of the + quotient will fall high enough that just N_QLIMBS-1 limbs is enough to + get the necessary bits, thereby saving a division step. + + Bit shift either num or den to arrange for the above condition on the + high 1 bit of the quotient, to save a division step always. A shift to + save a division step is definitely worthwhile with mpn_tdiv_qr, though we + may want to reassess this on big num/den when a quotient-only division + exists. + + Maybe we could estimate the final exponent using nsize-dsize (and + possibly the high limbs of num and den), so as to detect overflow and + return infinity or zero quickly. Overflow is never very helpful to an + application, and can therefore probably be regarded as abnormal, but we + may still like to optimize it if the conditions are easy. (This would + only be for float formats we know, unknown formats are not important and + can be left to mpn_get_d.) + + Future: + + If/when mpn_tdiv_qr supports its qxn parameter we can use that instead of + padding n with zeros in temporary space. + + If/when a quotient-only division exists it can be used here immediately. + remp is only to satisfy mpn_tdiv_qr, the remainder is not used. + + Alternatives: + + An alternative algorithm, that may be faster: + 0. Let n be somewhat larger than the number of significant bits in a double. + 1. Extract the most significant n bits of the denominator, and an equal + number of bits from the numerator. + 2. Interpret the extracted numbers as integers, call them a and b + respectively, and develop n bits of the fractions ((a + 1) / b) and + (a / (b + 1)) using mpn_divrem. + 3. If the computed values are identical UP TO THE POSITION WE CARE ABOUT, + we are done. If they are different, repeat the algorithm from step 1, + but first let n = n * 2. + 4. If we end up using all bits from the numerator and denominator, fall + back to a plain division. + 5. Just to make life harder, The computation of a + 1 and b + 1 above + might give carry-out... Needs special handling. It might work to + subtract 1 in both cases instead. + + Not certain if this approach would be faster than a quotient-only + division. Presumably such optimizations are the sort of thing we would + like to have helping everywhere that uses a quotient-only division. */ + +double +mpq_get_d (const mpq_t src) +{ + double res; + mp_srcptr np, dp; + mp_ptr remp, tp; + mp_size_t nsize = SIZ(NUM(src)); + mp_size_t dsize = SIZ(DEN(src)); + mp_size_t qsize, prospective_qsize, zeros, chop, tsize; + mp_size_t sign_quotient = nsize; + long exp; +#define N_QLIMBS (1 + (sizeof (double) + GMP_LIMB_BYTES-1) / GMP_LIMB_BYTES) + mp_limb_t qarr[N_QLIMBS + 1]; + mp_ptr qp = qarr; + TMP_DECL; + + ASSERT (dsize > 0); /* canonical src */ + + /* mpn_get_d below requires a non-zero operand */ + if (UNLIKELY (nsize == 0)) + return 0.0; + + TMP_MARK; + nsize = ABS (nsize); + dsize = ABS (dsize); + np = PTR(NUM(src)); + dp = PTR(DEN(src)); + + prospective_qsize = nsize - dsize + 1; /* from using given n,d */ + qsize = N_QLIMBS + 1; /* desired qsize */ + + zeros = qsize - prospective_qsize; /* padding n to get qsize */ + exp = (long) -zeros * GMP_NUMB_BITS; /* relative to low of qp */ + + chop = MAX (-zeros, 0); /* negative zeros means shorten n */ + np += chop; + nsize -= chop; + zeros += chop; /* now zeros >= 0 */ + + tsize = nsize + zeros; /* size for possible copy of n */ + + if (WANT_TMP_DEBUG) + { + /* separate blocks, for malloc debugging */ + remp = TMP_ALLOC_LIMBS (dsize); + tp = (zeros > 0 ? TMP_ALLOC_LIMBS (tsize) : NULL); + } + else + { + /* one block with conditionalized size, for efficiency */ + remp = TMP_ALLOC_LIMBS (dsize + (zeros > 0 ? tsize : 0)); + tp = remp + dsize; + } + + /* zero extend n into temporary space, if necessary */ + if (zeros > 0) + { + MPN_ZERO (tp, zeros); + MPN_COPY (tp+zeros, np, nsize); + np = tp; + nsize = tsize; + } + + ASSERT (qsize == nsize - dsize + 1); + mpn_tdiv_qr (qp, remp, (mp_size_t) 0, np, nsize, dp, dsize); + + /* strip possible zero high limb */ + qsize -= (qp[qsize-1] == 0); + + res = mpn_get_d (qp, qsize, sign_quotient, exp); + TMP_FREE; + return res; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/get_den.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_den.c new file mode 100644 index 00000000000..48a385c817e --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_den.c @@ -0,0 +1,43 @@ +/* mpq_get_den(den,rat_src) -- Set DEN to the denominator of RAT_SRC. + +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_get_den (mpz_ptr den, mpq_srcptr src) +{ + mp_size_t size = SIZ(DEN(src)); + mp_ptr dp; + + dp = MPZ_NEWALLOC (den, size); + SIZ(den) = size; + MPN_COPY (dp, PTR(DEN(src)), size); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/get_num.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_num.c new file mode 100644 index 00000000000..cb464ccb94f --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_num.c @@ -0,0 +1,45 @@ + /* mpq_get_num(num,rat_src) -- Set NUM to the numerator of RAT_SRC. + +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_get_num (mpz_ptr num, mpq_srcptr src) +{ + mp_size_t size = SIZ(NUM(src)); + mp_size_t abs_size = ABS (size); + mp_ptr dp; + + dp = MPZ_NEWALLOC (num, abs_size); + SIZ(num) = size; + + MPN_COPY (dp, PTR(NUM(src)), abs_size); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/get_str.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_str.c new file mode 100644 index 00000000000..8fa0de1cd74 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/get_str.c @@ -0,0 +1,76 @@ +/* mpq_get_str -- mpq to string conversion. + +Copyright 2001, 2002, 2006, 2011 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include <stdio.h> +#include <string.h> +#include "gmp.h" +#include "gmp-impl.h" +#include "longlong.h" + +char * +mpq_get_str (char *str, int base, mpq_srcptr q) +{ + size_t str_alloc, len; + + if (base > 62 || base < -36) + return NULL; + + str_alloc = 0; + if (str == NULL) + { + /* This is an overestimate since we don't bother checking how much of + the high limbs of num and den are used. +2 for rounding up the + chars per bit of num and den. +3 for sign, slash and '\0'. */ + DIGITS_IN_BASE_PER_LIMB (str_alloc, ABSIZ(NUM(q)) + SIZ(DEN(q)), ABS(base)); + str_alloc += 6; + + str = (char *) (*__gmp_allocate_func) (str_alloc); + } + + mpz_get_str (str, base, mpq_numref(q)); + len = strlen (str); + if (! MPZ_EQUAL_1_P (mpq_denref (q))) + { + str[len++] = '/'; + mpz_get_str (str+len, base, mpq_denref(q)); + len += strlen (str+len); + } + + ASSERT (len == strlen(str)); + ASSERT (str_alloc == 0 || len+1 <= str_alloc); + ASSERT (len+1 <= /* size recommended to applications */ + mpz_sizeinbase (mpq_numref(q), ABS(base)) + + mpz_sizeinbase (mpq_denref(q), ABS(base)) + 3); + + if (str_alloc != 0) + __GMP_REALLOCATE_FUNC_MAYBE_TYPE (str, str_alloc, len+1, char); + + return str; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/init.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/init.c new file mode 100644 index 00000000000..0f526ef31d0 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/init.c @@ -0,0 +1,49 @@ +/* mpq_init -- Make a new rational number with value 0/1. + +Copyright 1991, 1994, 1995, 2000-2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_init (mpq_t x) +{ + ALLOC(NUM(x)) = 1; + PTR(NUM(x)) = (mp_ptr) (*__gmp_allocate_func) (GMP_LIMB_BYTES); + SIZ(NUM(x)) = 0; + ALLOC(DEN(x)) = 1; + PTR(DEN(x)) = (mp_ptr) (*__gmp_allocate_func) (GMP_LIMB_BYTES); + PTR(DEN(x))[0] = 1; + SIZ(DEN(x)) = 1; + +#ifdef __CHECKER__ + /* let the low limb look initialized, for the benefit of mpz_get_ui etc */ + PTR(NUM(x))[0] = 0; +#endif +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/inits.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/inits.c new file mode 100644 index 00000000000..97c41656ffd --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/inits.c @@ -0,0 +1,49 @@ +/* mpq_inits() -- Initialize multiple mpq_t variables and set them to 0. + +Copyright 2009 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include <stdarg.h> +#include <stdio.h> /* for NULL */ +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_inits (mpq_ptr x, ...) +{ + va_list ap; + + va_start (ap, x); + + while (x != NULL) + { + mpq_init (x); + x = va_arg (ap, mpq_ptr); + } + va_end (ap); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/inp_str.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/inp_str.c new file mode 100644 index 00000000000..7ba95743f8f --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/inp_str.c @@ -0,0 +1,76 @@ +/* mpq_inp_str -- read an mpq from a FILE. + +Copyright 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include <stdio.h> +#include <ctype.h> +#include "gmp.h" +#include "gmp-impl.h" + + +size_t +mpq_inp_str (mpq_ptr q, FILE *fp, int base) +{ + size_t nread; + int c; + + if (fp == NULL) + fp = stdin; + + SIZ(DEN(q)) = 1; + PTR(DEN(q))[0] = 1; + + nread = mpz_inp_str (mpq_numref(q), fp, base); + if (nread == 0) + return 0; + + c = getc (fp); + nread++; + + if (c == '/') + { + c = getc (fp); + nread++; + + nread = mpz_inp_str_nowhite (mpq_denref(q), fp, base, c, nread); + if (nread == 0) + { + SIZ(NUM(q)) = 0; + SIZ(DEN(q)) = 1; + PTR(DEN(q))[0] = 1; + } + } + else + { + ungetc (c, fp); + nread--; + } + + return nread; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/inv.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/inv.c new file mode 100644 index 00000000000..bd703d48602 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/inv.c @@ -0,0 +1,71 @@ +/* mpq_inv(dest,src) -- invert a rational number, i.e. set DEST to SRC + with the numerator and denominator swapped. + +Copyright 1991, 1994, 1995, 2000, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_inv (mpq_ptr dest, mpq_srcptr src) +{ + mp_size_t num_size = SIZ(NUM(src)); + mp_size_t den_size = SIZ(DEN(src)); + + if (num_size < 0) + { + num_size = -num_size; + den_size = -den_size; + } + else if (UNLIKELY (num_size == 0)) + DIVIDE_BY_ZERO; + + SIZ(DEN(dest)) = num_size; + SIZ(NUM(dest)) = den_size; + + /* If dest == src we may just swap the numerator and denominator; + we ensured that the new denominator is positive. */ + + if (dest == src) + { + MP_PTR_SWAP (PTR(NUM(dest)), PTR(DEN(dest))); + MP_SIZE_T_SWAP (ALLOC(NUM(dest)), ALLOC(DEN(dest))); + } + else + { + mp_ptr dp; + + den_size = ABS (den_size); + dp = MPZ_NEWALLOC (NUM(dest), den_size); + MPN_COPY (dp, PTR(DEN(src)), den_size); + + dp = MPZ_NEWALLOC (DEN(dest), num_size); + MPN_COPY (dp, PTR(NUM(src)), num_size); + } +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/md_2exp.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/md_2exp.c new file mode 100644 index 00000000000..a2a7e1e2fb7 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/md_2exp.c @@ -0,0 +1,111 @@ +/* mpq_mul_2exp, mpq_div_2exp - multiply or divide by 2^N */ + +/* +Copyright 2000, 2002, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" +#include "longlong.h" + + +/* The multiplier/divisor "n", representing 2^n, is applied by right shifting + "r" until it's odd (if it isn't already), and left shifting "l" for the + rest. */ + +static void +mord_2exp (mpz_ptr ldst, mpz_ptr rdst, mpz_srcptr lsrc, mpz_srcptr rsrc, + mp_bitcnt_t n) +{ + mp_size_t rsrc_size = SIZ(rsrc); + mp_size_t len = ABS (rsrc_size); + mp_ptr rsrc_ptr = PTR(rsrc); + mp_ptr p, rdst_ptr; + mp_limb_t plow; + + p = rsrc_ptr; + plow = *p; + while (n >= GMP_NUMB_BITS && plow == 0) + { + n -= GMP_NUMB_BITS; + p++; + plow = *p; + } + + /* no realloc here if rsrc==rdst, so p and rsrc_ptr remain valid */ + len -= (p - rsrc_ptr); + rdst_ptr = MPZ_REALLOC (rdst, len); + + if ((plow & 1) || n == 0) + { + /* need INCR when src==dst */ + if (p != rdst_ptr) + MPN_COPY_INCR (rdst_ptr, p, len); + } + else + { + unsigned long shift; + if (plow == 0) + shift = n; + else + { + count_trailing_zeros (shift, plow); + shift = MIN (shift, n); + } + mpn_rshift (rdst_ptr, p, len, shift); + len -= (rdst_ptr[len-1] == 0); + n -= shift; + } + SIZ(rdst) = (rsrc_size >= 0) ? len : -len; + + if (n) + mpz_mul_2exp (ldst, lsrc, n); + else if (ldst != lsrc) + mpz_set (ldst, lsrc); +} + + +void +mpq_mul_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n) +{ + mord_2exp (NUM(dst), DEN(dst), NUM(src), DEN(src), n); +} + +void +mpq_div_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n) +{ + if (SIZ(NUM(src)) == 0) + { + SIZ(NUM(dst)) = 0; + SIZ(DEN(dst)) = 1; + PTR(DEN(dst))[0] = 1; + return; + } + + mord_2exp (DEN(dst), NUM(dst), DEN(src), NUM(src), n); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/mul.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/mul.c new file mode 100644 index 00000000000..49d618f3bc1 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/mul.c @@ -0,0 +1,103 @@ +/* mpq_mul -- multiply two rational numbers. + +Copyright 1991, 1994-1996, 2000-2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + + +void +mpq_mul (mpq_ptr prod, mpq_srcptr op1, mpq_srcptr op2) +{ + mpz_t gcd1, gcd2; + mpz_t tmp1, tmp2; + mp_size_t op1_num_size; + mp_size_t op1_den_size; + mp_size_t op2_num_size; + mp_size_t op2_den_size; + mp_size_t alloc; + TMP_DECL; + + if (op1 == op2) + { + /* No need for any GCDs when squaring. */ + mpz_mul (mpq_numref (prod), mpq_numref (op1), mpq_numref (op1)); + mpz_mul (mpq_denref (prod), mpq_denref (op1), mpq_denref (op1)); + return; + } + + op1_num_size = ABSIZ(NUM(op1)); + op1_den_size = SIZ(DEN(op1)); + op2_num_size = ABSIZ(NUM(op2)); + op2_den_size = SIZ(DEN(op2)); + + if (op1_num_size == 0 || op2_num_size == 0) + { + /* We special case this to simplify allocation logic; gcd(0,x) = x + is a singular case for the allocations. */ + SIZ(NUM(prod)) = 0; + PTR(DEN(prod))[0] = 1; + SIZ(DEN(prod)) = 1; + return; + } + + TMP_MARK; + + alloc = MIN (op1_num_size, op2_den_size); + MPZ_TMP_INIT (gcd1, alloc); + + alloc = MIN (op2_num_size, op1_den_size); + MPZ_TMP_INIT (gcd2, alloc); + + alloc = MAX (op1_num_size, op2_den_size); + MPZ_TMP_INIT (tmp1, alloc); + + alloc = MAX (op2_num_size, op1_den_size); + MPZ_TMP_INIT (tmp2, alloc); + + /* PROD might be identical to either operand, so don't store the result there + until we are finished with the input operands. We can overwrite the + numerator of PROD when we are finished with the numerators of OP1 and + OP2. */ + + mpz_gcd (gcd1, NUM(op1), DEN(op2)); + mpz_gcd (gcd2, NUM(op2), DEN(op1)); + + mpz_divexact_gcd (tmp1, NUM(op1), gcd1); + mpz_divexact_gcd (tmp2, NUM(op2), gcd2); + + mpz_mul (NUM(prod), tmp1, tmp2); + + mpz_divexact_gcd (tmp1, DEN(op2), gcd1); + mpz_divexact_gcd (tmp2, DEN(op1), gcd2); + + mpz_mul (DEN(prod), tmp1, tmp2); + + TMP_FREE; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/neg.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/neg.c new file mode 100644 index 00000000000..054e21ebe33 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/neg.c @@ -0,0 +1,58 @@ +/* mpq_neg -- negate a rational. + +Copyright 2000, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#define __GMP_FORCE_mpq_neg 1 + +#include "gmp.h" +#include "gmp-impl.h" + + +void +mpq_neg (mpq_ptr dst, mpq_srcptr src) +{ + mp_size_t num_size = SIZ(NUM(src)); + + if (src != dst) + { + mp_size_t size; + mp_ptr dp; + + size = ABS(num_size); + dp = MPZ_NEWALLOC (NUM(dst), size); + MPN_COPY (dp, PTR(NUM(src)), size); + + size = SIZ(DEN(src)); + dp = MPZ_NEWALLOC (DEN(dst), size); + SIZ(DEN(dst)) = size; + MPN_COPY (dp, PTR(DEN(src)), size); + } + + SIZ(NUM(dst)) = -num_size; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/out_str.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/out_str.c new file mode 100644 index 00000000000..c2bc053b215 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/out_str.c @@ -0,0 +1,54 @@ +/* mpq_out_str(stream,base,integer) */ + +/* +Copyright 2000, 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include <stdio.h> +#include "gmp.h" +#include "gmp-impl.h" + + +size_t +mpq_out_str (FILE *stream, int base, mpq_srcptr q) +{ + size_t written; + + if (stream == NULL) + stream = stdout; + + written = mpz_out_str (stream, base, mpq_numref (q)); + + if (mpz_cmp_ui (mpq_denref (q), 1) != 0) + { + putc ('/', stream); + written += 1 + mpz_out_str (stream, base, mpq_denref (q)); + } + + return ferror (stream) ? 0 : written; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set.c new file mode 100644 index 00000000000..fb7bfad49c6 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set.c @@ -0,0 +1,51 @@ +/* mpq_set(dest,src) -- Set DEST to SRC. + +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_set (mpq_ptr dest, mpq_srcptr src) +{ + mp_size_t num_size, den_size; + mp_size_t abs_num_size; + mp_ptr dp; + + num_size = SIZ(NUM(src)); + abs_num_size = ABS (num_size); + dp = MPZ_NEWALLOC (NUM(dest), abs_num_size); + SIZ(NUM(dest)) = num_size; + MPN_COPY (dp, PTR(NUM(src)), abs_num_size); + + den_size = SIZ(DEN(src)); + dp = MPZ_NEWALLOC (DEN(dest), den_size); + SIZ(DEN(dest)) = den_size; + MPN_COPY (dp, PTR(DEN(src)), den_size); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_d.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_d.c new file mode 100644 index 00000000000..308677db402 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_d.c @@ -0,0 +1,166 @@ +/* mpq_set_d(mpq_t q, double d) -- Set q to d without rounding. + +Copyright 2000, 2002, 2003, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "config.h" + +#if HAVE_FLOAT_H +#include <float.h> /* for DBL_MAX */ +#endif + +#include "gmp.h" +#include "gmp-impl.h" +#include "longlong.h" + +#if LIMBS_PER_DOUBLE > 4 + choke me +#endif + +void +mpq_set_d (mpq_ptr dest, double d) +{ + int negative; + mp_exp_t exp; + mp_limb_t tp[LIMBS_PER_DOUBLE]; + mp_ptr np, dp; + mp_size_t nn, dn; + int c; + + DOUBLE_NAN_INF_ACTION (d, + __gmp_invalid_operation (), + __gmp_invalid_operation ()); + + negative = d < 0; + d = ABS (d); + + exp = __gmp_extract_double (tp, d); + + /* There are two main version of the conversion. The `then' arm handles + numbers with a fractional part, while the `else' arm handles integers. */ +#if LIMBS_PER_DOUBLE == 4 + if (exp <= 1 || (exp == 2 && (tp[0] | tp[1]) != 0)) +#endif +#if LIMBS_PER_DOUBLE == 3 + if (exp <= 1 || (exp == 2 && tp[0] != 0)) +#endif +#if LIMBS_PER_DOUBLE == 2 + if (exp <= 1) +#endif + { + if (d == 0.0) + { + SIZ(NUM(dest)) = 0; + SIZ(DEN(dest)) = 1; + PTR(DEN(dest))[0] = 1; + return; + } + + dn = -exp; + np = MPZ_NEWALLOC (NUM(dest), 3); +#if LIMBS_PER_DOUBLE == 4 + if ((tp[0] | tp[1] | tp[2]) == 0) + np[0] = tp[3], nn = 1; + else if ((tp[0] | tp[1]) == 0) + np[1] = tp[3], np[0] = tp[2], nn = 2; + else if (tp[0] == 0) + np[2] = tp[3], np[1] = tp[2], np[0] = tp[1], nn = 3; + else + np[3] = tp[3], np[2] = tp[2], np[1] = tp[1], np[0] = tp[0], nn = 4; +#endif +#if LIMBS_PER_DOUBLE == 3 + if ((tp[0] | tp[1]) == 0) + np[0] = tp[2], nn = 1; + else if (tp[0] == 0) + np[1] = tp[2], np[0] = tp[1], nn = 2; + else + np[2] = tp[2], np[1] = tp[1], np[0] = tp[0], nn = 3; +#endif +#if LIMBS_PER_DOUBLE == 2 + if (tp[0] == 0) + np[0] = tp[1], nn = 1; + else + np[1] = tp[1], np[0] = tp[0], nn = 2; +#endif + dn += nn + 1; + ASSERT_ALWAYS (dn > 0); + dp = MPZ_NEWALLOC (DEN(dest), dn); + MPN_ZERO (dp, dn - 1); + dp[dn - 1] = 1; + count_trailing_zeros (c, np[0] | dp[0]); + if (c != 0) + { + mpn_rshift (np, np, nn, c); + nn -= np[nn - 1] == 0; + mpn_rshift (dp, dp, dn, c); + dn -= dp[dn - 1] == 0; + } + SIZ(DEN(dest)) = dn; + SIZ(NUM(dest)) = negative ? -nn : nn; + } + else + { + nn = exp; + np = MPZ_NEWALLOC (NUM(dest), nn); + switch (nn) + { + default: + MPN_ZERO (np, nn - LIMBS_PER_DOUBLE); + np += nn - LIMBS_PER_DOUBLE; + /* fall through */ +#if LIMBS_PER_DOUBLE == 2 + case 2: + np[1] = tp[1], np[0] = tp[0]; + break; +#endif +#if LIMBS_PER_DOUBLE == 3 + case 3: + np[2] = tp[2], np[1] = tp[1], np[0] = tp[0]; + break; + case 2: + np[1] = tp[2], np[0] = tp[1]; + break; +#endif +#if LIMBS_PER_DOUBLE == 4 + case 4: + np[3] = tp[3], np[2] = tp[2], np[1] = tp[1], np[0] = tp[0]; + break; + case 3: + np[2] = tp[3], np[1] = tp[2], np[0] = tp[1]; + break; + case 2: + np[1] = tp[3], np[0] = tp[2]; + break; +#endif + } + dp = PTR(DEN(dest)); + dp[0] = 1; + SIZ(DEN(dest)) = 1; + SIZ(NUM(dest)) = negative ? -nn : nn; + } +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_den.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_den.c new file mode 100644 index 00000000000..4bcb86b0977 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_den.c @@ -0,0 +1,45 @@ +/* mpq_set_den(dest,den) -- Set the denominator of DEST from DEN. + +Copyright 1991, 1994-1996, 2000, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_set_den (mpq_ptr dest, mpz_srcptr den) +{ + mp_size_t size = SIZ (den); + mp_size_t abs_size = ABS (size); + mp_ptr dp; + + dp = MPZ_NEWALLOC (DEN(dest), abs_size); + + SIZ(DEN(dest)) = size; + MPN_COPY (dp, PTR(den), abs_size); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_f.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_f.c new file mode 100644 index 00000000000..7764a17f05e --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_f.c @@ -0,0 +1,107 @@ +/* mpq_set_f -- set an mpq from an mpf. + +Copyright 2000-2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" +#include "longlong.h" + + +void +mpq_set_f (mpq_ptr q, mpf_srcptr f) +{ + mp_size_t fexp = EXP(f); + mp_ptr fptr = PTR(f); + mp_size_t fsize = SIZ(f); + mp_size_t abs_fsize = ABS(fsize); + mp_limb_t flow; + + if (fsize == 0) + { + /* set q=0 */ + SIZ(NUM(q)) = 0; + SIZ(DEN(q)) = 1; + PTR(DEN(q))[0] = 1; + return; + } + + /* strip low zero limbs from f */ + flow = *fptr; + MPN_STRIP_LOW_ZEROS_NOT_ZERO (fptr, abs_fsize, flow); + + if (fexp >= abs_fsize) + { + /* radix point is to the right of the limbs, no denominator */ + mp_ptr num_ptr; + + num_ptr = MPZ_NEWALLOC (mpq_numref (q), fexp); + MPN_ZERO (num_ptr, fexp - abs_fsize); + MPN_COPY (num_ptr + fexp - abs_fsize, fptr, abs_fsize); + + SIZ(NUM(q)) = fsize >= 0 ? fexp : -fexp; + SIZ(DEN(q)) = 1; + PTR(DEN(q))[0] = 1; + } + else + { + /* radix point is within or to the left of the limbs, use denominator */ + mp_ptr num_ptr, den_ptr; + mp_size_t den_size; + + den_size = abs_fsize - fexp; + num_ptr = MPZ_NEWALLOC (mpq_numref (q), abs_fsize); + den_ptr = MPZ_NEWALLOC (mpq_denref (q), den_size+1); + + if (flow & 1) + { + /* no powers of two to strip from numerator */ + + MPN_COPY (num_ptr, fptr, abs_fsize); + MPN_ZERO (den_ptr, den_size); + den_ptr[den_size] = 1; + } + else + { + /* right shift numerator, adjust denominator accordingly */ + int shift; + + den_size--; + count_trailing_zeros (shift, flow); + + mpn_rshift (num_ptr, fptr, abs_fsize, shift); + abs_fsize -= (num_ptr[abs_fsize-1] == 0); + + MPN_ZERO (den_ptr, den_size); + den_ptr[den_size] = GMP_LIMB_HIGHBIT >> (shift-1); + } + + SIZ(NUM(q)) = fsize >= 0 ? abs_fsize : -abs_fsize; + SIZ(DEN(q)) = den_size + 1; + } +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_num.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_num.c new file mode 100644 index 00000000000..c6b52ccaa18 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_num.c @@ -0,0 +1,45 @@ +/* mpq_set_num(dest,num) -- Set the numerator of DEST from NUM. + +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_set_num (mpq_ptr dest, mpz_srcptr num) +{ + mp_size_t size = SIZ (num); + mp_size_t abs_size = ABS (size); + mp_ptr dp; + + dp = MPZ_NEWALLOC (NUM(dest), abs_size); + + SIZ(NUM(dest)) = size; + MPN_COPY (dp, PTR(num), abs_size); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_si.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_si.c new file mode 100644 index 00000000000..0a10b3048d1 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_si.c @@ -0,0 +1,65 @@ +/* mpq_set_si(dest,ulong_num,ulong_den) -- Set DEST to the rational number + ULONG_NUM/ULONG_DEN. + +Copyright 1991, 1994, 1995, 2001, 2003 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_set_si (mpq_t dest, signed long int num, unsigned long int den) +{ + unsigned long int abs_num; + + if (GMP_NUMB_BITS < BITS_PER_ULONG) + { + if (num == 0) /* Canonicalize 0/d to 0/1. */ + den = 1; + mpz_set_si (mpq_numref (dest), num); + mpz_set_ui (mpq_denref (dest), den); + return; + } + + abs_num = ABS_CAST (unsigned long, num); + + if (num == 0) + { + /* Canonicalize 0/d to 0/1. */ + den = 1; + SIZ(NUM(dest)) = 0; + } + else + { + PTR(NUM(dest))[0] = abs_num; + SIZ(NUM(dest)) = num > 0 ? 1 : -1; + } + + PTR(DEN(dest))[0] = den; + SIZ(DEN(dest)) = (den != 0); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_str.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_str.c new file mode 100644 index 00000000000..930fe5576c0 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_str.c @@ -0,0 +1,69 @@ +/* mpq_set_str -- string to mpq conversion. + +Copyright 2001, 2002 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include <stdio.h> +#include <string.h> +#include "gmp.h" +#include "gmp-impl.h" + + +/* FIXME: Would like an mpz_set_mem (or similar) accepting a pointer and + length so we wouldn't have to copy the numerator just to null-terminate + it. */ + +int +mpq_set_str (mpq_ptr q, const char *str, int base) +{ + const char *slash; + char *num; + size_t numlen; + int ret; + + slash = strchr (str, '/'); + if (slash == NULL) + { + SIZ(DEN(q)) = 1; + PTR(DEN(q))[0] = 1; + + return mpz_set_str (mpq_numref(q), str, base); + } + + numlen = slash - str; + num = __GMP_ALLOCATE_FUNC_TYPE (numlen+1, char); + memcpy (num, str, numlen); + num[numlen] = '\0'; + ret = mpz_set_str (mpq_numref(q), num, base); + (*__gmp_free_func) (num, numlen+1); + + if (ret != 0) + return ret; + + return mpz_set_str (mpq_denref(q), slash+1, base); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_ui.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_ui.c new file mode 100644 index 00000000000..4473b30bb84 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_ui.c @@ -0,0 +1,61 @@ +/* mpq_set_ui(dest,ulong_num,ulong_den) -- Set DEST to the rational number + ULONG_NUM/ULONG_DEN. + +Copyright 1991, 1994, 1995, 2001-2003 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_set_ui (mpq_t dest, unsigned long int num, unsigned long int den) +{ + if (GMP_NUMB_BITS < BITS_PER_ULONG) + { + if (num == 0) /* Canonicalize 0/d to 0/1. */ + den = 1; + mpz_set_ui (mpq_numref (dest), num); + mpz_set_ui (mpq_denref (dest), den); + return; + } + + if (num == 0) + { + /* Canonicalize 0/n to 0/1. */ + den = 1; + SIZ(NUM(dest)) = 0; + } + else + { + PTR(NUM(dest))[0] = num; + SIZ(NUM(dest)) = 1; + } + + PTR(DEN(dest))[0] = den; + SIZ(DEN(dest)) = (den != 0); +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/set_z.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_z.c new file mode 100644 index 00000000000..358d1ca899c --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/set_z.c @@ -0,0 +1,49 @@ +/* mpq_set_z (dest,src) -- Set DEST to SRC. + +Copyright 1996, 2001, 2012 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_set_z (mpq_ptr dest, mpz_srcptr src) +{ + mp_size_t num_size; + mp_size_t abs_num_size; + mp_ptr dp; + + num_size = SIZ (src); + abs_num_size = ABS (num_size); + dp = MPZ_NEWALLOC (NUM(dest), abs_num_size); + SIZ(NUM(dest)) = num_size; + MPN_COPY (dp, PTR(src), abs_num_size); + + PTR(DEN(dest))[0] = 1; + SIZ(DEN(dest)) = 1; +} diff --git a/Build/source/libs/gmp/gmp-6.0.0/mpq/swap.c b/Build/source/libs/gmp/gmp-6.0.0/mpq/swap.c new file mode 100644 index 00000000000..51fdcac7fd3 --- /dev/null +++ b/Build/source/libs/gmp/gmp-6.0.0/mpq/swap.c @@ -0,0 +1,71 @@ +/* mpq_swap (U, V) -- Swap U and V. + +Copyright 1997, 1998, 2000, 2001 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + +or + + * the GNU General Public License as published by the Free Software + Foundation; either version 2 of the License, or (at your option) any + later version. + +or both in parallel, as here. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received copies of the GNU General Public License and the +GNU Lesser General Public License along with the GNU MP Library. If not, +see https://www.gnu.org/licenses/. */ + +#include "gmp.h" +#include "gmp-impl.h" + +void +mpq_swap (mpq_ptr u, mpq_ptr v) __GMP_NOTHROW +{ + mp_ptr up, vp; + mp_size_t usize, vsize; + mp_size_t ualloc, valloc; + + ualloc = ALLOC(NUM(u)); + valloc = ALLOC(NUM(v)); + ALLOC(NUM(v)) = ualloc; + ALLOC(NUM(u)) = valloc; + + usize = SIZ(NUM(u)); + vsize = SIZ(NUM(v)); + SIZ(NUM(v)) = usize; + SIZ(NUM(u)) = vsize; + + up = PTR(NUM(u)); + vp = PTR(NUM(v)); + PTR(NUM(v)) = up; + PTR(NUM(u)) = vp; + + + ualloc = ALLOC(DEN(u)); + valloc = ALLOC(DEN(v)); + ALLOC(DEN(v)) = ualloc; + ALLOC(DEN(u)) = valloc; + + usize = SIZ(DEN(u)); + vsize = SIZ(DEN(v)); + SIZ(DEN(v)) = usize; + SIZ(DEN(u)) = vsize; + + up = PTR(DEN(u)); + vp = PTR(DEN(v)); + PTR(DEN(v)) = up; + PTR(DEN(u)) = vp; +} |