mirror of
https://github.com/ilyakooo0/Idris-dev.git
synced 2024-09-11 08:55:23 +03:00
Fix looped temporary allocations in mini-GMP (#4324)
* Bump mini-gmp to the libgmp-6.1 version * Add a test for #4311. * Avoid looped temporary allocation in mini-gmp Backport of https://gmplib.org/repo/gmp/rev/164971d5c8d0 mpn_div_qr_1_preinv function is called in a loop while converting mpz_t to a string. Looped allocation causes garbage collection while still in mpz_get_str for big enough numbers, which may cause wrong results or crashes. Fixes #4311.
This commit is contained in:
parent
d8dd94b2b6
commit
3be1766bd7
626
rts/mini-gmp.c
626
rts/mini-gmp.c
File diff suppressed because it is too large
Load Diff
@ -1,21 +1,32 @@
|
||||
/* mini-gmp, a minimalistic implementation of a GNU GMP subset.
|
||||
|
||||
Copyright 2011, 2012, 2013 Free Software Foundation, Inc.
|
||||
Copyright 2011-2015 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 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.
|
||||
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 Lesser General Public
|
||||
License for more details.
|
||||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
|
||||
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/. */
|
||||
|
||||
/* About mini-gmp: This is a minimal implementation of a subset of the
|
||||
GMP interface. It is intended for inclusion into applications which
|
||||
@ -64,10 +75,14 @@ typedef __mpz_struct mpz_t[1];
|
||||
typedef __mpz_struct *mpz_ptr;
|
||||
typedef const __mpz_struct *mpz_srcptr;
|
||||
|
||||
extern const int mp_bits_per_limb;
|
||||
|
||||
void mpn_copyi (mp_ptr, mp_srcptr, mp_size_t);
|
||||
void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
|
||||
void mpn_zero (mp_ptr, mp_size_t);
|
||||
|
||||
int mpn_cmp (mp_srcptr, mp_srcptr, mp_size_t);
|
||||
int mpn_zero_p (mp_srcptr, mp_size_t);
|
||||
|
||||
mp_limb_t mpn_add_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
|
||||
mp_limb_t mpn_add_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
|
||||
@ -84,6 +99,8 @@ mp_limb_t mpn_submul_1 (mp_ptr, mp_srcptr, mp_size_t, mp_limb_t);
|
||||
mp_limb_t mpn_mul (mp_ptr, mp_srcptr, mp_size_t, mp_srcptr, mp_size_t);
|
||||
void mpn_mul_n (mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
|
||||
void mpn_sqr (mp_ptr, mp_srcptr, mp_size_t);
|
||||
int mpn_perfect_square_p (mp_srcptr, mp_size_t);
|
||||
mp_size_t mpn_sqrtrem (mp_ptr, mp_ptr, mp_srcptr, mp_size_t);
|
||||
|
||||
mp_limb_t mpn_lshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
|
||||
mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
|
||||
@ -91,6 +108,11 @@ mp_limb_t mpn_rshift (mp_ptr, mp_srcptr, mp_size_t, unsigned int);
|
||||
mp_bitcnt_t mpn_scan0 (mp_srcptr, mp_bitcnt_t);
|
||||
mp_bitcnt_t mpn_scan1 (mp_srcptr, mp_bitcnt_t);
|
||||
|
||||
void mpn_com (mp_ptr, mp_srcptr, mp_size_t);
|
||||
mp_limb_t mpn_neg (mp_ptr, mp_srcptr, mp_size_t);
|
||||
|
||||
mp_bitcnt_t mpn_popcount (mp_srcptr, mp_size_t);
|
||||
|
||||
mp_limb_t mpn_invert_3by2 (mp_limb_t, mp_limb_t);
|
||||
#define mpn_invert_limb(x) mpn_invert_3by2 ((x), 0)
|
||||
|
||||
@ -127,6 +149,10 @@ void mpz_mul_si (mpz_t, const mpz_t, long int);
|
||||
void mpz_mul_ui (mpz_t, const mpz_t, unsigned long int);
|
||||
void mpz_mul (mpz_t, const mpz_t, const mpz_t);
|
||||
void mpz_mul_2exp (mpz_t, const mpz_t, mp_bitcnt_t);
|
||||
void mpz_addmul_ui (mpz_t, const mpz_t, unsigned long int);
|
||||
void mpz_addmul (mpz_t, const mpz_t, const mpz_t);
|
||||
void mpz_submul_ui (mpz_t, const mpz_t, unsigned long int);
|
||||
void mpz_submul (mpz_t, const mpz_t, const mpz_t);
|
||||
|
||||
void mpz_cdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
|
||||
void mpz_fdiv_qr (mpz_t, mpz_t, const mpz_t, const mpz_t);
|
||||
@ -150,6 +176,7 @@ void mpz_mod (mpz_t, const mpz_t, const mpz_t);
|
||||
void mpz_divexact (mpz_t, const mpz_t, const mpz_t);
|
||||
|
||||
int mpz_divisible_p (const mpz_t, const mpz_t);
|
||||
int mpz_congruent_p (const mpz_t, const mpz_t, const mpz_t);
|
||||
|
||||
unsigned long mpz_cdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
|
||||
unsigned long mpz_fdiv_qr_ui (mpz_t, mpz_t, const mpz_t, unsigned long);
|
||||
@ -179,6 +206,7 @@ int mpz_invert (mpz_t, const mpz_t, const mpz_t);
|
||||
|
||||
void mpz_sqrtrem (mpz_t, mpz_t, const mpz_t);
|
||||
void mpz_sqrt (mpz_t, const mpz_t);
|
||||
int mpz_perfect_square_p (const mpz_t);
|
||||
|
||||
void mpz_pow_ui (mpz_t, const mpz_t, unsigned long);
|
||||
void mpz_ui_pow_ui (mpz_t, unsigned long, unsigned long);
|
||||
@ -191,6 +219,8 @@ int mpz_root (mpz_t, const mpz_t, unsigned long);
|
||||
void mpz_fac_ui (mpz_t, unsigned long);
|
||||
void mpz_bin_uiui (mpz_t, unsigned long, unsigned long);
|
||||
|
||||
int mpz_probab_prime_p (const mpz_t, int);
|
||||
|
||||
int mpz_tstbit (const mpz_t, mp_bitcnt_t);
|
||||
void mpz_setbit (mpz_t, mp_bitcnt_t);
|
||||
void mpz_clrbit (mpz_t, mp_bitcnt_t);
|
||||
@ -214,6 +244,15 @@ double mpz_get_d (const mpz_t);
|
||||
size_t mpz_size (const mpz_t);
|
||||
mp_limb_t mpz_getlimbn (const mpz_t, mp_size_t);
|
||||
|
||||
void mpz_realloc2 (mpz_t, mp_bitcnt_t);
|
||||
mp_srcptr mpz_limbs_read (mpz_srcptr);
|
||||
mp_ptr mpz_limbs_modify (mpz_t, mp_size_t);
|
||||
mp_ptr mpz_limbs_write (mpz_t, mp_size_t);
|
||||
void mpz_limbs_finish (mpz_t, mp_size_t);
|
||||
mpz_srcptr mpz_roinit_n (mpz_t, mp_srcptr, mp_size_t);
|
||||
|
||||
#define MPZ_ROINIT_N(xp, xs) {{0, (xs),(xp) }}
|
||||
|
||||
void mpz_set_si (mpz_t, signed long int);
|
||||
void mpz_set_ui (mpz_t, unsigned long int);
|
||||
void mpz_set (mpz_t, const mpz_t);
|
||||
|
@ -80,7 +80,8 @@ testFamiliesData = [
|
||||
( 26, ANY )]),
|
||||
("bignum", "Bignum",
|
||||
[ ( 1, ANY ),
|
||||
( 2, ANY )]),
|
||||
( 2, ANY ),
|
||||
( 3, C_CG )]),
|
||||
("bounded", "Bounded",
|
||||
[ ( 1, ANY )]),
|
||||
("buffer", "Buffer",
|
||||
|
23
test/bignum003/bignum003.idr
Normal file
23
test/bignum003/bignum003.idr
Normal file
@ -0,0 +1,23 @@
|
||||
allZeroesFromTo : Int -> Int -> String -> Bool
|
||||
allZeroesFromTo n m str
|
||||
= if n < m
|
||||
then if strIndex str n == '0'
|
||||
then allZeroesFromTo (n+1) m str
|
||||
else False
|
||||
else True
|
||||
|
||||
isNthPowerOfTen : Int -> String -> Bool
|
||||
isNthPowerOfTen n str
|
||||
= let m = cast (length str) in
|
||||
if m == n+1
|
||||
then strHead str == '1' && allZeroesFromTo 1 m str
|
||||
else False
|
||||
|
||||
main : IO ()
|
||||
main = do
|
||||
let x = power 10 12468
|
||||
let sx = show x
|
||||
printLn (isNthPowerOfTen 12468 sx)
|
||||
let y = power 10 12470
|
||||
let sy = show y
|
||||
printLn (isNthPowerOfTen 12470 sy)
|
2
test/bignum003/expected
Normal file
2
test/bignum003/expected
Normal file
@ -0,0 +1,2 @@
|
||||
True
|
||||
True
|
4
test/bignum003/run
Executable file
4
test/bignum003/run
Executable file
@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
${IDRIS:-idris} $@ bignum003.idr -o bignum003
|
||||
./bignum003
|
||||
rm -f bignum003 *.ibc
|
Loading…
Reference in New Issue
Block a user