Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418346/* Functions needed for bootstrapping the gmp build, based on mini-gmp.12Copyright 2001, 2002, 2004, 2011, 2012 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of either:89* the GNU Lesser General Public License as published by the Free10Software Foundation; either version 3 of the License, or (at your11option) any later version.1213or1415* the GNU General Public License as published by the Free Software16Foundation; either version 2 of the License, or (at your option) any17later version.1819or both in parallel, as here.2021The GNU MP Library is distributed in the hope that it will be useful, but22WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY23or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License24for more details.2526You should have received copies of the GNU General Public License and the27GNU Lesser General Public License along with the GNU MP Library. If not,28see https://www.gnu.org/licenses/. */293031#include "mini-gmp/mini-gmp.c"3233#define MIN(l,o) ((l) < (o) ? (l) : (o))34#define PTR(x) ((x)->_mp_d)35#define SIZ(x) ((x)->_mp_size)3637#define xmalloc gmp_default_alloc3839int40isprime (unsigned long int t)41{42unsigned long int q, r, d;4344if (t < 32)45return (0xa08a28acUL >> t) & 1;46if ((t & 1) == 0)47return 0;4849if (t % 3 == 0)50return 0;51if (t % 5 == 0)52return 0;53if (t % 7 == 0)54return 0;5556for (d = 11;;)57{58q = t / d;59r = t - q * d;60if (q < d)61return 1;62if (r == 0)63break;64d += 2;65q = t / d;66r = t - q * d;67if (q < d)68return 1;69if (r == 0)70break;71d += 4;72}73return 0;74}7576int77log2_ceil (int n)78{79int e;80assert (n >= 1);81for (e = 0; ; e++)82if ((1 << e) >= n)83break;84return e;85}8687/* Set inv to the inverse of d, in the style of invert_limb, ie. for88udiv_qrnnd_preinv. */89void90mpz_preinv_invert (mpz_t inv, mpz_t d, int numb_bits)91{92mpz_t t;93int norm;94assert (SIZ(d) > 0);9596norm = numb_bits - mpz_sizeinbase (d, 2);97assert (norm >= 0);98mpz_init_set_ui (t, 1L);99mpz_mul_2exp (t, t, 2*numb_bits - norm);100mpz_tdiv_q (inv, t, d);101mpz_set_ui (t, 1L);102mpz_mul_2exp (t, t, numb_bits);103mpz_sub (inv, inv, t);104105mpz_clear (t);106}107108/* Calculate r satisfying r*d == 1 mod 2^n. */109void110mpz_invert_2exp (mpz_t r, mpz_t a, unsigned long n)111{112unsigned long i;113mpz_t inv, prod;114115assert (mpz_odd_p (a));116117mpz_init_set_ui (inv, 1L);118mpz_init (prod);119120for (i = 1; i < n; i++)121{122mpz_mul (prod, inv, a);123if (mpz_tstbit (prod, i) != 0)124mpz_setbit (inv, i);125}126127mpz_mul (prod, inv, a);128mpz_tdiv_r_2exp (prod, prod, n);129assert (mpz_cmp_ui (prod, 1L) == 0);130131mpz_set (r, inv);132133mpz_clear (inv);134mpz_clear (prod);135}136137/* Calculate inv satisfying r*a == 1 mod 2^n. */138void139mpz_invert_ui_2exp (mpz_t r, unsigned long a, unsigned long n)140{141mpz_t az;142mpz_init_set_ui (az, a);143mpz_invert_2exp (r, az, n);144mpz_clear (az);145}146147148