@@ -1403,7 +1403,7 @@ static inline uint64_t gen_mask(uint64_t max)
1403
1403
*/
1404
1404
1405
1405
1406
- inline uint64_t random_bounded_uint64 (aug_state * state , uint64_t off , uint64_t rng , uint64_t mask )
1406
+ static inline uint64_t bounded_uint64 (aug_state * state , uint64_t off , uint64_t rng , uint64_t mask )
1407
1407
{
1408
1408
uint64_t val ;
1409
1409
if (rng == 0 )
@@ -1418,8 +1418,13 @@ inline uint64_t random_bounded_uint64(aug_state *state, uint64_t off, uint64_t r
1418
1418
return off + val ;
1419
1419
}
1420
1420
1421
+ uint64_t random_bounded_uint64 (aug_state * state , uint64_t off , uint64_t rng , uint64_t mask )
1422
+ {
1423
+ return bounded_uint64 (state , off , rng , mask );
1424
+ }
1421
1425
1422
- inline uint32_t random_buffered_bounded_uint32 (aug_state * state , uint32_t off , uint32_t rng , uint32_t mask , int * bcnt , uint32_t * buf )
1426
+
1427
+ static inline uint32_t bounded_uint32 (aug_state * state , uint32_t off , uint32_t rng , uint32_t mask )
1423
1428
{
1424
1429
/*
1425
1430
* The buffer and buffer count are not used here but are included to allow
@@ -1435,7 +1440,15 @@ inline uint32_t random_buffered_bounded_uint32(aug_state *state, uint32_t off, u
1435
1440
return off + val ;
1436
1441
}
1437
1442
1438
- inline uint16_t random_buffered_bounded_uint16 (aug_state * state , uint16_t off , uint16_t rng , uint16_t mask , int * bcnt , uint32_t * buf )
1443
+ uint32_t random_buffered_bounded_uint32 (aug_state * state , uint32_t off , uint32_t rng , uint32_t mask , int * bcnt , uint32_t * buf )
1444
+ {
1445
+ /*
1446
+ * Unused bcnt and buf are here only to allow templating with other uint generators
1447
+ */
1448
+ return bounded_uint32 (state , off , rng , mask );
1449
+ }
1450
+
1451
+ static inline buffered_bounded_uint16 (aug_state * state , uint16_t off , uint16_t rng , uint16_t mask , int * bcnt , uint32_t * buf )
1439
1452
{
1440
1453
uint16_t val ;
1441
1454
if (rng == 0 )
@@ -1455,7 +1468,12 @@ inline uint16_t random_buffered_bounded_uint16(aug_state *state, uint16_t off, u
1455
1468
return off + val ;
1456
1469
}
1457
1470
1458
- inline uint8_t random_buffered_bounded_uint8 (aug_state * state , uint8_t off , uint8_t rng , uint8_t mask , int * bcnt , uint32_t * buf )
1471
+ uint16_t random_buffered_bounded_uint16 (aug_state * state , uint16_t off , uint16_t rng , uint16_t mask , int * bcnt , uint32_t * buf )
1472
+ {
1473
+ return buffered_bounded_uint16 (state , off , rng , mask , bcnt , buf );
1474
+ }
1475
+
1476
+ static inline uint8_t buffered_bounded_uint8 (aug_state * state , uint8_t off , uint8_t rng , uint8_t mask , int * bcnt , uint32_t * buf )
1459
1477
{
1460
1478
uint8_t val ;
1461
1479
if (rng == 0 )
@@ -1474,6 +1492,31 @@ inline uint8_t random_buffered_bounded_uint8(aug_state *state, uint8_t off, uint
1474
1492
return off + val ;
1475
1493
}
1476
1494
1495
+ uint8_t random_buffered_bounded_uint8 (aug_state * state , uint8_t off , uint8_t rng , uint8_t mask , int * bcnt , uint32_t * buf )
1496
+ {
1497
+ return buffered_bounded_uint8 (state , off , rng , mask , bcnt , buf );
1498
+ }
1499
+
1500
+ static inline npy_bool buffered_bounded_bool (aug_state * state , npy_bool off , npy_bool rng , npy_bool mask , int * bcnt , uint32_t * buf )
1501
+ {
1502
+ if (rng == 0 )
1503
+ return off ;
1504
+ if (!(bcnt [0 ])) {
1505
+ buf [0 ] = random_uint32 (state );
1506
+ bcnt [0 ] = 31 ;
1507
+ }
1508
+ else {
1509
+ buf [0 ] >>= 1 ;
1510
+ bcnt [0 ] -= 1 ;
1511
+ }
1512
+ return (buf [0 ] & 0x00000001UL ) != 0 ;
1513
+ }
1514
+
1515
+ npy_bool random_buffered_bounded_bool (aug_state * state , npy_bool off , npy_bool rng , npy_bool mask , int * bcnt , uint32_t * buf )
1516
+ {
1517
+ return buffered_bounded_bool (state , off , rng , mask , bcnt , buf );
1518
+ }
1519
+
1477
1520
void random_bounded_uint64_fill (aug_state * state , uint64_t off , uint64_t rng , npy_intp cnt , uint64_t * out )
1478
1521
{
1479
1522
uint64_t mask ;
@@ -1482,7 +1525,7 @@ void random_bounded_uint64_fill(aug_state *state, uint64_t off, uint64_t rng, np
1482
1525
/* Smallest bit mask >= max */
1483
1526
mask = gen_mask (rng );
1484
1527
for (i = 0 ; i < cnt ; i ++ ) {
1485
- out [i ] = random_bounded_uint64 (state , off , rng , mask );
1528
+ out [i ] = bounded_uint64 (state , off , rng , mask );
1486
1529
}
1487
1530
}
1488
1531
@@ -1493,15 +1536,15 @@ void random_bounded_uint64_fill(aug_state *state, uint64_t off, uint64_t rng, np
1493
1536
*/
1494
1537
void random_bounded_uint32_fill (aug_state * state , uint32_t off , uint32_t rng , npy_intp cnt , uint32_t * out )
1495
1538
{
1496
- uint32_t val , mask ;
1539
+ uint32_t mask ;
1497
1540
npy_intp i ;
1498
1541
uint32_t buf = 0 ;
1499
1542
int bcnt = 0 ;
1500
1543
1501
1544
/* Smallest bit mask >= max */
1502
1545
mask = (uint32_t )gen_mask (rng );
1503
1546
for (i = 0 ; i < cnt ; i ++ ) {
1504
- out [i ] = random_buffered_bounded_uint32 (state , off , rng , mask , & bcnt , & buf );
1547
+ out [i ] = bounded_uint32 (state , off , rng , mask , & bcnt , & buf );
1505
1548
}
1506
1549
}
1507
1550
@@ -1520,7 +1563,7 @@ void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t rng, np
1520
1563
/* Smallest bit mask >= max */
1521
1564
mask = (uint16_t )gen_mask (rng );
1522
1565
for (i = 0 ; i < cnt ; i ++ ) {
1523
- out [i ] = random_buffered_bounded_uint16 (state , off , rng , mask , & bcnt , & buf );
1566
+ out [i ] = buffered_bounded_uint16 (state , off , rng , mask , & bcnt , & buf );
1524
1567
}
1525
1568
}
1526
1569
@@ -1538,7 +1581,7 @@ void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, npy_i
1538
1581
/* Smallest bit mask >= max */
1539
1582
mask = (uint8_t )gen_mask (rng );
1540
1583
for (i = 0 ; i < cnt ; i ++ ) {
1541
- out [i ] = random_buffered_bounded_uint8 (state , off , rng , mask , & bcnt , & buf );
1584
+ out [i ] = buffered_bounded_uint8 (state , off , rng , mask , & bcnt , & buf );
1542
1585
}
1543
1586
1544
1587
}
@@ -1550,27 +1593,13 @@ void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, npy_i
1550
1593
*/
1551
1594
void random_bounded_bool_fill (aug_state * state , npy_bool off , npy_bool rng , npy_intp cnt , npy_bool * out )
1552
1595
{
1596
+ npy_bool mask = 0 ;
1553
1597
npy_intp i ;
1554
1598
uint32_t buf = 0 ;
1555
1599
int bcnt = 0 ;
1556
1600
1557
- if (rng == 0 ) {
1558
- for (i = 0 ; i < cnt ; i ++ ) {
1559
- out [i ] = off ;
1560
- }
1561
- return ;
1562
- }
1563
-
1564
1601
for (i = 0 ; i < cnt ; i ++ ) {
1565
- if (!bcnt ) {
1566
- buf = random_uint32 (state );
1567
- bcnt = 31 ;
1568
- }
1569
- else {
1570
- buf >>= 1 ;
1571
- bcnt -- ;
1572
- }
1573
- out [i ] = (buf & 0x00000001 ) != 0 ;
1602
+ out [i ] = buffered_bounded_bool (state , off , rng , mask , & bcnt , & buf );
1574
1603
}
1575
1604
}
1576
1605
0 commit comments