GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* Generate mp_bases data.12Copyright 1991, 1993, 1994, 1996, 2000, 2002, 2004 Free Software Foundation,3Inc.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 the GNU Lesser General Public License as published by9the Free Software Foundation; either version 2.1 of the License, or (at your10option) any later version.1112The GNU MP Library is distributed in the hope that it will be useful, but13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY14or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public15License for more details.1617You should have received a copy of the GNU Lesser General Public License18along with the GNU MP Library; see the file COPYING.LIB. If not, write to19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,20MA 02110-1301, USA. */2122#include <math.h>2324#include "dumbmp.c"252627int chars_per_limb;28double chars_per_bit_exactly;29mpz_t big_base;30int normalization_steps;31mpz_t big_base_inverted;3233mpz_t t;3435#define POW2_P(n) (((n) & ((n) - 1)) == 0)3637unsigned int38ulog2 (unsigned int x)39{40unsigned int i;41for (i = 0; x != 0; i++)42x >>= 1;43return i;44}4546void47generate (int limb_bits, int nail_bits, int base)48{49int numb_bits = limb_bits - nail_bits;5051mpz_set_ui (t, 1L);52mpz_mul_2exp (t, t, numb_bits);53mpz_set_ui (big_base, 1L);54chars_per_limb = 0;55for (;;)56{57mpz_mul_ui (big_base, big_base, (long) base);58if (mpz_cmp (big_base, t) > 0)59break;60chars_per_limb++;61}6263chars_per_bit_exactly = 0.69314718055994530942 / log ((double) base);6465mpz_ui_pow_ui (big_base, (long) base, (long) chars_per_limb);6667normalization_steps = limb_bits - mpz_sizeinbase (big_base, 2);6869mpz_set_ui (t, 1L);70mpz_mul_2exp (t, t, 2*limb_bits - normalization_steps);71mpz_tdiv_q (big_base_inverted, t, big_base);72mpz_set_ui (t, 1L);73mpz_mul_2exp (t, t, limb_bits);74mpz_sub (big_base_inverted, big_base_inverted, t);75}7677void78header (int limb_bits, int nail_bits)79{80int numb_bits = limb_bits - nail_bits;8182generate (limb_bits, nail_bits, 10);8384printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");85printf ("\n");86printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);87printf ("Error, error, this data is for %d bits\n", numb_bits);88printf ("#endif\n");89printf ("\n");90printf ("/* mp_bases[10] data, as literal values */\n");91printf ("#define MP_BASES_CHARS_PER_LIMB_10 %d\n", chars_per_limb);92printf ("#define MP_BASES_BIG_BASE_10 CNST_LIMB(0x");93mpz_out_str (stdout, 16, big_base);94printf (")\n");95printf ("#define MP_BASES_BIG_BASE_INVERTED_10 CNST_LIMB(0x");96mpz_out_str (stdout, 16, big_base_inverted);97printf (")\n");98printf ("#define MP_BASES_NORMALIZATION_STEPS_10 %d\n", normalization_steps);99}100101void102table (int limb_bits, int nail_bits)103{104int numb_bits = limb_bits - nail_bits;105int base;106107printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");108printf ("\n");109printf ("#include \"gmp.h\"\n");110printf ("#include \"gmp-impl.h\"\n");111printf ("\n");112printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);113printf ("Error, error, this data is for %d bits\n", numb_bits);114printf ("#endif\n");115printf ("\n");116puts ("const struct bases mp_bases[257] =\n{");117puts (" /* 0 */ { 0, 0.0, 0 },");118puts (" /* 1 */ { 0, 1e37, 0 },");119for (base = 2; base <= 256; base++)120{121generate (limb_bits, nail_bits, base);122123printf (" /* %3u */ { ", base);124if (POW2_P (base))125{126printf ("%u, %.16f, 0x%x },\n",127chars_per_limb, chars_per_bit_exactly, ulog2 (base) - 1);128}129else130{131printf ("%u, %.16f, CNST_LIMB(0x",132chars_per_limb, chars_per_bit_exactly);133mpz_out_str (stdout, 16, big_base);134printf ("), CNST_LIMB(0x");135mpz_out_str (stdout, 16, big_base_inverted);136printf (") },\n");137}138}139140puts ("};");141}142143int144main (int argc, char **argv)145{146int limb_bits, nail_bits;147148mpz_init (big_base);149mpz_init (big_base_inverted);150mpz_init (t);151152if (argc != 4)153{154fprintf (stderr, "Usage: gen-bases <header|table> <limbbits> <nailbits>\n");155exit (1);156}157158limb_bits = atoi (argv[2]);159nail_bits = atoi (argv[3]);160161if (limb_bits <= 0162|| nail_bits < 0163|| nail_bits >= limb_bits)164{165fprintf (stderr, "Invalid limb/nail bits: %d %d\n",166limb_bits, nail_bits);167exit (1);168}169170if (strcmp (argv[1], "header") == 0)171header (limb_bits, nail_bits);172else if (strcmp (argv[1], "table") == 0)173table (limb_bits, nail_bits);174else175{176fprintf (stderr, "Invalid header/table choice: %s\n", argv[1]);177exit (1);178}179180return 0;181}182183184185