Skip to content

Commit

Permalink
kernel: fix RandomIntegerMT on 64 bit big endian
Browse files Browse the repository at this point in the history
Fixes #2292
  • Loading branch information
fingolfin committed Jul 11, 2018
1 parent 224979d commit 4a3af77
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 135 deletions.
13 changes: 10 additions & 3 deletions src/integer.c
Expand Up @@ -2655,7 +2655,7 @@ Obj FuncRandomIntegerMT(Obj self, Obj mtstr, Obj nrbits)
{
Obj res;
Int i, n, q, r, qoff, len;
UInt4 *mt, rand;
UInt4 *mt;
UInt4 *pt;
while (! IsStringConv(mtstr)) {
mtstr = ErrorReturnObj(
Expand Down Expand Up @@ -2707,14 +2707,21 @@ Obj FuncRandomIntegerMT(Obj self, Obj mtstr, Obj nrbits)
pt = (UInt4*) ADDR_INT(res);
mt = (UInt4*) CHARS_STRING(mtstr);
for (i = 0; i < qoff; i++, pt++) {
rand = (UInt4) nextrandMT_int32(mt);
*pt = rand;
*pt = nextrandMT_int32(mt);
}
if (r != 0) {
/* we generated too many random bits -- chop of the extra bits */
pt = (UInt4*) ADDR_INT(res);
pt[qoff-1] = pt[qoff-1] & ((UInt4)(-1) >> (32-r));
}
#if defined(SYS_IS_64_BIT) && defined(WORDS_BIGENDIAN)
// swap the halves of the 64bit words to match the
// little endian resp. 32 bit versions of this code
pt = (UInt4 *)ADDR_INT(res);
for (i = 0; i < qoff; i += 2, pt += 2) {
SWAP(UInt4, pt[0], pt[1]);
}
#endif
/* shrink bag if necessary */
res = GMP_NORMALIZE(res);
/* convert result if small int */
Expand Down
71 changes: 0 additions & 71 deletions tst/test32bit/randlist.tst

This file was deleted.

159 changes: 98 additions & 61 deletions tst/test64bit/randlist.tst
Expand Up @@ -6,72 +6,109 @@
#Y Copyright (C) 2016, The GAP Group
##
gap> START_TEST("randlist.tst");
gap> l := [1..2^(8*GAPInfo.BytesPerVariable-4)-1];
gap> l4 := [1..2^28-1];
[ 1 .. 268435455 ]
gap> l8 := [1..2^60-1];
[ 1 .. 1152921504606846975 ]
gap> bits := Concatenation([27..33],[59..65],[95..97],[127..129]);;

#
gap> Reset(GlobalMersenneTwister, 1);;
gap> List([1..10], i-> Random(1, 2^70));
[ 389420078096435786116, 1135477438879460122740, 425191437180319830675,
1083900231106243887320, 600872374094649387692, 39438917363375403157,
860639563472417594821, 1064153947161477828981, 140455377727366448871,
198677943275448576238 ]
gap> List([1..10], i-> Random(l));
[ 13449613059603563, 750280845826784809, 604497940593164266,
195957948672214877, 363541010878257, 252402567376707281, 370012941179900926,
417909953390052908, 963123934602894316, 270776873282813509 ]
gap> List([1..10], i-> Random(1,10^(3*i)));
[ 200, 711160, 554290144, 155776019526, 905142534515704, 885417939216212735,
932340683886669498401, 455800814114634556722688,
791206551199537424739336406, 382559260149794825284857428764 ]
gap> List(bits, i-> Random(1, 2^i));
[ 18829054, 164251624, 474614219, 233454245, 213348187, 3257085298,
1185099587, 100418839541388781, 78936154011671244, 1951632504588564735,
1891846953557814001, 7380160538170766753, 4588549330382399543,
20212399778158554723, 25871717046293081269812882956,
74959154262646398646743466671, 52390258479956963774859779424,
46150081993482768205533540973984006122,
264304607032107166297612477092494666822,
369126435272860177990538082224405421325 ]
gap> List([1..10], i-> Random(1, 10^(3*i)));
[ 796, 488110, 337484392, 594009012741, 658311342730164, 718543835420643146,
192531588881827984252, 93448310516637261223301, 897306488169252919346512382,
373729190135314870329223545530 ]
gap> List([1..10], i-> Random(l4));
[ 143468941, 48373072, 9645662, 124739889, 124982356, 178658837, 173118677,
117495931, 138389463, 242860897 ]
gap> List([1..10], i-> Random(l8));
[ 347927869732747975, 519413397450930047, 898055001130687869,
163763776991392067, 969052338240557373, 1038155658715349861,
493205527438070762, 28433991423533450, 173244587732492565,
990629399330982901 ]

#
gap> Reset(GlobalRandomSource, 1);;
gap> List([1..10], i-> Random(GlobalRandomSource, 1, 2^70));
[ 1128431629504922105781, 1101744982711025966526, 975266616221323178376,
678285771456235093359, 337368903834409162500, 1067481644129172537859,
666712591436546208470, 471773889319658808870, 27293053954741592585,
894173925476383505759 ]
gap> List([1..10], i-> Random(GlobalRandomSource, l));
[ 172731053183754005, 399538608969287265, 528332786381188118,
719153818282315693, 341000865208262150, 994286057174428461,
975057260589483054, 343216463929586102, 60169085279927990,
236724101465774021 ]
gap> List([1..10], i-> Random(GlobalRandomSource, l)) mod 2^28;
[ 82408175, 222596625, 2843657, 140496253, 243545930, 128569490, 62441909,
102134268, 47094864, 154341103 ]
gap> List([1..10], i-> Random(GlobalRandomSource, 1,10^(3*i)));
[ 871, 264536, 392436441, 73737636655, 456525824458936, 890212987711716841,
234476903267325497175, 596997600529202583174722,
290628328961307271818818898, 17261260615457929009978390979 ]
gap> List(bits, i-> Random(GlobalRandomSource, 1, 2^i));
[ 128287824, 93339996, 497514298, 126333050, 888008348, 3272686668,
3284765239, 246384569571494206, 899121525533970334, 2058504735678170118,
1295847095154625366, 1831951483667165481, 12413749580545794189,
21112300994467179461, 16479214171808335931562195799,
47077177305952446651710749862, 93051612446083908105560589039,
17595829529914834395994704246711425132,
189028623575818650885511742806844572497,
442802932679156364466206827982691923819 ]
gap> List([1..10], i-> Random(GlobalRandomSource, 1, 10^(3*i)));
[ 292, 540747, 68367363, 782766307890, 207951565811836, 269515013492647614,
168133019703503347058, 46702024622849426851426, 498676940076039327334874754,
960893403060023160487783291143 ]
gap> List([1..10], i-> Random(GlobalRandomSource, l4));
[ 190962548, 17964291, 180726950, 185199085, 213556766, 109018187, 55718740,
216423086, 151137000, 48013178 ]
gap> List([1..10], i-> Random(GlobalRandomSource, l8));
[ 1128201256618036672, 890212987711716841, 228981350846997556,
962603471308182302, 697651637792723842, 464389630980588785,
463611844822187319, 195151234274091472, 339385016873630147,
670406272576191605 ]

#
gap> rs := RandomSource(IsMersenneTwister, 1);
<RandomSource in IsMersenneTwister>
gap> List([1..10], i-> Random(rs, 1, 2^70));
[ 389420078096435786116, 1135477438879460122740, 425191437180319830675,
1083900231106243887320, 600872374094649387692, 39438917363375403157,
860639563472417594821, 1064153947161477828981, 140455377727366448871,
198677943275448576238 ]
gap> List([1..10], i-> Random(rs, l));
[ 13449613059603563, 750280845826784809, 604497940593164266,
195957948672214877, 363541010878257, 252402567376707281, 370012941179900926,
417909953390052908, 963123934602894316, 270776873282813509 ]
gap> List([1..10], i-> Random(rs, 1,10^(3*i)));
[ 200, 711160, 554290144, 155776019526, 905142534515704, 885417939216212735,
932340683886669498401, 455800814114634556722688,
791206551199537424739336406, 382559260149794825284857428764 ]
gap> List(bits, i-> Random(rs, 1, 2^i));
[ 18829054, 164251624, 474614219, 233454245, 213348187, 3257085298,
1185099587, 100418839541388781, 78936154011671244, 1951632504588564735,
1891846953557814001, 7380160538170766753, 4588549330382399543,
20212399778158554723, 25871717046293081269812882956,
74959154262646398646743466671, 52390258479956963774859779424,
46150081993482768205533540973984006122,
264304607032107166297612477092494666822,
369126435272860177990538082224405421325 ]
gap> List([1..10], i-> Random(rs, 1, 10^(3*i)));
[ 796, 488110, 337484392, 594009012741, 658311342730164, 718543835420643146,
192531588881827984252, 93448310516637261223301, 897306488169252919346512382,
373729190135314870329223545530 ]
gap> List([1..10], i-> Random(rs, l4));
[ 143468941, 48373072, 9645662, 124739889, 124982356, 178658837, 173118677,
117495931, 138389463, 242860897 ]
gap> List([1..10], i-> Random(rs, l8));
[ 347927869732747975, 519413397450930047, 898055001130687869,
163763776991392067, 969052338240557373, 1038155658715349861,
493205527438070762, 28433991423533450, 173244587732492565,
990629399330982901 ]

#
gap> rs := RandomSource(IsGAPRandomSource, 1);
<RandomSource in IsGAPRandomSource>
gap> List([1..10], i-> Random(rs, 1, 2^70));
[ 1128431629504922105781, 1101744982711025966526, 975266616221323178376,
678285771456235093359, 337368903834409162500, 1067481644129172537859,
666712591436546208470, 471773889319658808870, 27293053954741592585,
894173925476383505759 ]
gap> List([1..10], i-> Random(rs, l));
[ 172731053183754005, 399538608969287265, 528332786381188118,
719153818282315693, 341000865208262150, 994286057174428461,
975057260589483054, 343216463929586102, 60169085279927990,
236724101465774021 ]
gap> List([1..10], i-> Random(rs, l)) mod 2^28;
[ 82408175, 222596625, 2843657, 140496253, 243545930, 128569490, 62441909,
102134268, 47094864, 154341103 ]
gap> List([1..10], i-> Random(rs, 1,10^(3*i)));
[ 871, 264536, 392436441, 73737636655, 456525824458936, 890212987711716841,
234476903267325497175, 596997600529202583174722,
290628328961307271818818898, 17261260615457929009978390979 ]
gap> List(bits, i-> Random(rs, 1, 2^i));
[ 128287824, 93339996, 497514298, 126333050, 888008348, 3272686668,
3284765239, 246384569571494206, 899121525533970334, 2058504735678170118,
1295847095154625366, 1831951483667165481, 12413749580545794189,
21112300994467179461, 16479214171808335931562195799,
47077177305952446651710749862, 93051612446083908105560589039,
17595829529914834395994704246711425132,
189028623575818650885511742806844572497,
442802932679156364466206827982691923819 ]
gap> List([1..10], i-> Random(rs, 1, 10^(3*i)));
[ 292, 540747, 68367363, 782766307890, 207951565811836, 269515013492647614,
168133019703503347058, 46702024622849426851426, 498676940076039327334874754,
960893403060023160487783291143 ]
gap> List([1..10], i-> Random(rs, l4));
[ 190962548, 17964291, 180726950, 185199085, 213556766, 109018187, 55718740,
216423086, 151137000, 48013178 ]
gap> List([1..10], i-> Random(rs, l8));
[ 1128201256618036672, 890212987711716841, 228981350846997556,
962603471308182302, 697651637792723842, 464389630980588785,
463611844822187319, 195151234274091472, 339385016873630147,
670406272576191605 ]

#
gap> STOP_TEST( "randlist.tst", 1);
92 changes: 92 additions & 0 deletions tst/testinstall/randlist.tst
@@ -0,0 +1,92 @@
#############################################################################
##
#W randlist.tst GAP library Frank Lübeck
##
##
#Y Copyright (C) 2016, The GAP Group
##
gap> START_TEST("randlist.tst");
gap> l4 := [1..2^28-1];
[ 1 .. 268435455 ]
gap> bits := Concatenation([27..33],[59..65],[95..97],[127..129]);;

#
gap> Reset(GlobalMersenneTwister, 1);;
gap> List(bits, i-> Random(1, 2^i));
[ 18829054, 164251624, 474614219, 233454245, 213348187, 3257085298,
1185099587, 100418839541388781, 78936154011671244, 1951632504588564735,
1891846953557814001, 7380160538170766753, 4588549330382399543,
20212399778158554723, 25871717046293081269812882956,
74959154262646398646743466671, 52390258479956963774859779424,
46150081993482768205533540973984006122,
264304607032107166297612477092494666822,
369126435272860177990538082224405421325 ]
gap> List([1..10], i-> Random(1, 10^(3*i)));
[ 796, 488110, 337484392, 594009012741, 658311342730164, 718543835420643146,
192531588881827984252, 93448310516637261223301, 897306488169252919346512382,
373729190135314870329223545530 ]
gap> List([1..10], i-> Random(l4));
[ 143468941, 48373072, 9645662, 124739889, 124982356, 178658837, 173118677,
117495931, 138389463, 242860897 ]

#
gap> Reset(GlobalRandomSource, 1);;
gap> List(bits, i-> Random(GlobalRandomSource, 1, 2^i));
[ 128287824, 93339996, 497514298, 126333050, 888008348, 3272686668,
3284765239, 246384569571494206, 899121525533970334, 2058504735678170118,
1295847095154625366, 1831951483667165481, 12413749580545794189,
21112300994467179461, 16479214171808335931562195799,
47077177305952446651710749862, 93051612446083908105560589039,
17595829529914834395994704246711425132,
189028623575818650885511742806844572497,
442802932679156364466206827982691923819 ]
gap> List([1..10], i-> Random(GlobalRandomSource, 1, 10^(3*i)));
[ 292, 540747, 68367363, 782766307890, 207951565811836, 269515013492647614,
168133019703503347058, 46702024622849426851426, 498676940076039327334874754,
960893403060023160487783291143 ]
gap> List([1..10], i-> Random(GlobalRandomSource, l4));
[ 190962548, 17964291, 180726950, 185199085, 213556766, 109018187, 55718740,
216423086, 151137000, 48013178 ]

#
gap> rs := RandomSource(IsMersenneTwister, 1);
<RandomSource in IsMersenneTwister>
gap> List(bits, i-> Random(rs, 1, 2^i));
[ 18829054, 164251624, 474614219, 233454245, 213348187, 3257085298,
1185099587, 100418839541388781, 78936154011671244, 1951632504588564735,
1891846953557814001, 7380160538170766753, 4588549330382399543,
20212399778158554723, 25871717046293081269812882956,
74959154262646398646743466671, 52390258479956963774859779424,
46150081993482768205533540973984006122,
264304607032107166297612477092494666822,
369126435272860177990538082224405421325 ]
gap> List([1..10], i-> Random(rs, 1, 10^(3*i)));
[ 796, 488110, 337484392, 594009012741, 658311342730164, 718543835420643146,
192531588881827984252, 93448310516637261223301, 897306488169252919346512382,
373729190135314870329223545530 ]
gap> List([1..10], i-> Random(rs, l4));
[ 143468941, 48373072, 9645662, 124739889, 124982356, 178658837, 173118677,
117495931, 138389463, 242860897 ]

#
gap> rs := RandomSource(IsGAPRandomSource, 1);
<RandomSource in IsGAPRandomSource>
gap> List(bits, i-> Random(rs, 1, 2^i));
[ 128287824, 93339996, 497514298, 126333050, 888008348, 3272686668,
3284765239, 246384569571494206, 899121525533970334, 2058504735678170118,
1295847095154625366, 1831951483667165481, 12413749580545794189,
21112300994467179461, 16479214171808335931562195799,
47077177305952446651710749862, 93051612446083908105560589039,
17595829529914834395994704246711425132,
189028623575818650885511742806844572497,
442802932679156364466206827982691923819 ]
gap> List([1..10], i-> Random(rs, 1, 10^(3*i)));
[ 292, 540747, 68367363, 782766307890, 207951565811836, 269515013492647614,
168133019703503347058, 46702024622849426851426, 498676940076039327334874754,
960893403060023160487783291143 ]
gap> List([1..10], i-> Random(rs, l4));
[ 190962548, 17964291, 180726950, 185199085, 213556766, 109018187, 55718740,
216423086, 151137000, 48013178 ]

#
gap> STOP_TEST( "randlist.tst", 1);

0 comments on commit 4a3af77

Please sign in to comment.