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/* Generate mp_bases data.12Copyright 1991, 1993, 1994, 1996, 2000, 2002, 2004, 2011, 2012 Free Software3Foundation, Inc.45This file is part of the GNU MP Library.67The GNU MP Library is free software; you can redistribute it and/or modify8it under the terms of either:910* the GNU Lesser General Public License as published by the Free11Software Foundation; either version 3 of the License, or (at your12option) any later version.1314or1516* the GNU General Public License as published by the Free Software17Foundation; either version 2 of the License, or (at your option) any18later version.1920or both in parallel, as here.2122The GNU MP Library is distributed in the hope that it will be useful, but23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY24or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License25for more details.2627You should have received copies of the GNU General Public License and the28GNU Lesser General Public License along with the GNU MP Library. If not,29see https://www.gnu.org/licenses/. */3031#include "bootstrap.c"323334int chars_per_limb;35mpz_t big_base;36int normalization_steps;37mpz_t big_base_inverted;3839mpz_t t;4041#define POW2_P(n) (((n) & ((n) - 1)) == 0)4243unsigned int44ulog2 (unsigned int x)45{46unsigned int i;47for (i = 0; x != 0; i++)48x >>= 1;49return i;50}5152void53generate (int limb_bits, int nail_bits, int base)54{55int numb_bits = limb_bits - nail_bits;5657mpz_set_ui (t, 1L);58mpz_mul_2exp (t, t, numb_bits);59mpz_set_ui (big_base, 1L);60chars_per_limb = 0;61for (;;)62{63mpz_mul_ui (big_base, big_base, (long) base);64if (mpz_cmp (big_base, t) > 0)65break;66chars_per_limb++;67}6869mpz_ui_pow_ui (big_base, (long) base, (long) chars_per_limb);7071normalization_steps = limb_bits - mpz_sizeinbase (big_base, 2);7273mpz_set_ui (t, 1L);74mpz_mul_2exp (t, t, 2*limb_bits - normalization_steps);75mpz_tdiv_q (big_base_inverted, t, big_base);76mpz_set_ui (t, 1L);77mpz_mul_2exp (t, t, limb_bits);78mpz_sub (big_base_inverted, big_base_inverted, t);79}8081void82header (int limb_bits, int nail_bits)83{84int numb_bits = limb_bits - nail_bits;8586generate (limb_bits, nail_bits, 10);8788printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");89printf ("\n");90printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);91printf ("Error, error, this data is for %d bits\n", numb_bits);92printf ("#endif\n");93printf ("\n");94printf ("/* mp_bases[10] data, as literal values */\n");95printf ("#define MP_BASES_CHARS_PER_LIMB_10 %d\n", chars_per_limb);96printf ("#define MP_BASES_BIG_BASE_10 CNST_LIMB(0x");97mpz_out_str (stdout, 16, big_base);98printf (")\n");99printf ("#define MP_BASES_BIG_BASE_INVERTED_10 CNST_LIMB(0x");100mpz_out_str (stdout, 16, big_base_inverted);101printf (")\n");102printf ("#define MP_BASES_NORMALIZATION_STEPS_10 %d\n", normalization_steps);103}104105106#define EXTRA 16107108/* Compute log(2)/log(b) as a fixnum. */109void110mp_2logb (mpz_t r, int bi, int prec)111{112mpz_t t, t2, two, b;113int i;114115mpz_init_set_ui (t, 1);116mpz_mul_2exp (t, t, prec+EXTRA);117118mpz_init (t2);119120mpz_init_set_ui (two, 2);121mpz_mul_2exp (two, two, prec+EXTRA);122123mpz_set_ui (r, 0);124125mpz_init_set_ui (b, bi);126mpz_mul_2exp (b, b, prec+EXTRA);127128for (i = prec-1; i >= 0; i--)129{130mpz_mul_2exp (b, b, prec+EXTRA);131mpz_sqrt (b, b);132133mpz_mul (t2, t, b);134mpz_tdiv_q_2exp (t2, t2, prec+EXTRA);135136if (mpz_cmp (t2, two) < 0) /* not too large? */137{138mpz_setbit (r, i); /* set next less significant bit */139mpz_set (t, t2); /* new value acceptable */140}141}142143mpz_clear (t);144mpz_clear (t2);145mpz_clear (two);146mpz_clear (b);147}148149void150table (int limb_bits, int nail_bits)151{152int numb_bits = limb_bits - nail_bits;153int base;154mpz_t r, t, logb2, log2b;155156mpz_init (r);157mpz_init (t);158mpz_init (logb2);159mpz_init (log2b);160161printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");162printf ("\n");163printf ("#include \"gmp.h\"\n");164printf ("#include \"gmp-impl.h\"\n");165printf ("\n");166printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);167printf ("Error, error, this data is for %d bits\n", numb_bits);168printf ("#endif\n");169printf ("\n");170puts ("const struct bases mp_bases[257] =\n{");171puts (" /* 0 */ { 0, 0, 0, 0, 0 },");172puts (" /* 1 */ { 0, 0, 0, 0, 0 },");173for (base = 2; base <= 256; base++)174{175generate (limb_bits, nail_bits, base);176mp_2logb (r, base, limb_bits + 8);177mpz_tdiv_q_2exp (logb2, r, 8);178mpz_set_ui (t, 1);179mpz_mul_2exp (t, t, 2*limb_bits + 5);180mpz_sub_ui (t, t, 1);181mpz_add_ui (r, r, 1);182mpz_tdiv_q (log2b, t, r);183184printf (" /* %3u */ { ", base);185if (POW2_P (base))186{187mpz_set_ui (big_base, ulog2 (base) - 1);188mpz_set_ui (big_base_inverted, 0);189}190191printf ("%u,", chars_per_limb);192printf (" CNST_LIMB(0x");193mpz_out_str (stdout, 16, logb2);194printf ("), CNST_LIMB(0x");195mpz_out_str (stdout, 16, log2b);196printf ("), CNST_LIMB(0x");197mpz_out_str (stdout, 16, big_base);198printf ("), CNST_LIMB(0x");199mpz_out_str (stdout, 16, big_base_inverted);200printf (") },\n");201}202203puts ("};");204205mpz_clear (r);206mpz_clear (t);207mpz_clear (logb2);208mpz_clear (log2b);209210}211212int213main (int argc, char **argv)214{215int limb_bits, nail_bits;216217mpz_init (big_base);218mpz_init (big_base_inverted);219mpz_init (t);220221if (argc != 4)222{223fprintf (stderr, "Usage: gen-bases <header|table> <limbbits> <nailbits>\n");224exit (1);225}226227limb_bits = atoi (argv[2]);228nail_bits = atoi (argv[3]);229230if (limb_bits <= 0231|| nail_bits < 0232|| nail_bits >= limb_bits)233{234fprintf (stderr, "Invalid limb/nail bits: %d %d\n",235limb_bits, nail_bits);236exit (1);237}238239if (strcmp (argv[1], "header") == 0)240header (limb_bits, nail_bits);241else if (strcmp (argv[1], "table") == 0)242table (limb_bits, nail_bits);243else244{245fprintf (stderr, "Invalid header/table choice: %s\n", argv[1]);246exit (1);247}248249return 0;250}251252253