/* Template Code of a Random Number Generator for Vitis HLS Mersenne Twister : an example of 32-bit MT-19937 module */ /* This code is based on the following code by T.Nishimura and M.Matsumoto. http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/MT2002/CODES/mt19937ar.c */ /* Copyright (C) 2022, Yuto Asaumi and Tomonori Izumi, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Any feedback is very welcome. http://www.ritsumei.ac.jp/se/re/izumilab/dist/RNG/ */ #include #include #include #include #define MATRIX_A 0x9908b0dfu /* constant vector a */ #define UPPER_MASK 0x80000000u /* most significant w-r bits */ #define LOWER_MASK 0x7fffffffu /* least significant r bits */ typedef ap_uint<32> AXI_VALUE; typedef hls::stream AXI_STREAM; void random_generator(AXI_STREAM& random_out){ #pragma HLS PIPELINE #pragma HLS INTERFACE axis port=random_out #pragma HLS INTERFACE ap_ctrl_none port=return /* seeds generated by a seed 19660809 */ static AXI_VALUE mt0 = 19660809; static AXI_VALUE mt1_396[396] = { 3466581134, 1504268451, 2288091629, 1586113103, 1296592587, 2154024888, 933330537, 540807285, 2880707378, 518744570, 2355993005, 373639447, 2434016544, 1623185528, 3453495500, 1439162555, 1815425907, 2846051852, 1553287577, 2389729548, 3853043867, 2333637646, 1868520659, 453640434, 605151635, 4224363033, 1790117469, 3817651816, 1901814612, 881438119, 2614157826, 1885022752, 812271654, 4220833056, 1131522546, 2574260227, 2585330058, 2556912334, 943531683, 3158473079, 2272036946, 4243888058, 3821067560, 1101022499, 3844634775, 3448424082, 4126863460, 1568208851, 4229376011, 1372001114, 3093517850, 1659621292, 3806823542, 1718411103, 1880059469, 2352033844, 3148802951, 883955891, 932612570, 991956286, 4285360051, 1752635566, 1945855050, 2900866263, 84104522, 1341866100, 584190828, 82791904, 2755041701, 2840985129, 3944696382, 1170075481, 3563947521, 3992645908, 2057792862, 254737863, 130719440, 2577266782, 3217045659, 2639213997, 3217282396, 4077157480, 3527891338, 1674552417, 2122529333, 2874657498, 1092132751, 553382238, 421759087, 2115058469, 3278805391, 2376237208, 1411720991, 3463019828, 2216810002, 3612869808, 3038946816, 1670418732, 2582993956, 2569515362, 70605637, 3519541151, 543163635, 3475832647, 2084051517, 265319958, 1326079257, 1720750820, 1978237382, 4228166385, 3996916713, 2752331202, 2139430193, 2557686882, 3139111507, 3194448745, 2335614124, 326244124, 740629123, 1130926375, 2381879927, 2733673891, 3491367424, 2478214827, 738048554, 4074827024, 2971775998, 662822636, 3204267933, 2991534397, 2807178590, 3471505872, 992705476, 2948577498, 511149503, 938736611, 1279515672, 4164826983, 4025245439, 268064248, 1066247013, 4078273639, 974307331, 1983667391, 2762229127, 3769015051, 1743418043, 301649398, 3118852259, 2845438235, 1869709428, 2495171521, 754663304, 1356323138, 2638385930, 288049860, 2893243377, 162036349, 242235632, 2404312912, 2938775035, 3217177823, 2151631060, 3167503122, 1642726901, 4163890666, 1587121812, 1106640497, 3762770649, 3815791276, 715419318, 2702775418, 706825221, 2700389287, 3185968584, 1700750178, 2257007040, 2705683516, 558762793, 2179094753, 2450754884, 2101031764, 1512488512, 3974158685, 1654329551, 1087234048, 1243671072, 791611585, 1921301986, 758277709, 364679712, 4235773792, 1980736976, 2829075767, 2360847276, 3910720106, 2291096882, 1706980534, 1973091322, 517717455, 1240377972, 3918847475, 605094011, 995361875, 2159747212, 3122779860, 3006023997, 4167343531, 128523289, 3597694895, 2686337967, 870533397, 4133163806, 2968784967, 4209137936, 2424131671, 532754274, 2369856644, 518594489, 3724707289, 2442557919, 2292210191, 2848947456, 1271948458, 1405369176, 1000147967, 2019177342, 3181267199, 1064120758, 2577990580, 4104445365, 2544623542, 3748828909, 58518224, 1705112827, 4023742862, 1876247182, 1705691737, 722660775, 322380499, 237545776, 954560738, 2853993501, 3565326383, 937324113, 1140973547, 2901905993, 1594088335, 1978047999, 1752908848, 3111592272, 1846741078, 2337921616, 2693203288, 3029878657, 4142813871, 3371739869, 2979936408, 1139589061, 1835240792, 3629803042, 1933112587, 3688957945, 3705860266, 2029151414, 1686678589, 3552317623, 2467228688, 230082087, 406326641, 2540029860, 2961808270, 2606517069, 316669501, 18412580, 2793117512, 767511367, 1857205017, 1409319311, 3697941534, 3216219530, 1877143490, 1764553738, 3718458483, 993556813, 1665995135, 238441941, 3828472617, 1813705395, 3445687132, 497888414, 1116023162, 2283352492, 4048074956, 2118169042, 1333072999, 2548409191, 3855451395, 1229093419, 2311105982, 2250385497, 2978979861, 4277631298, 3293421781, 2878143391, 1049709347, 1115513090, 1064755811, 2646822212, 1733424084, 631583040, 2588389240, 2139522651, 4067372220, 4060602262, 2899637253, 3335325440, 2629394541, 2160555018, 402722920, 1561010761, 4206170026, 2289016816, 138452414, 1310387003, 521591080, 1314002447, 1452421838, 478663668, 2914006158, 1383573639, 3054449706, 1140915477, 3076492850, 2669333055, 1601663073, 669169457, 3201466535, 275255148, 625140464, 4083546629, 4164082612, 1504092042, 2330678831, 389158426, 2269946780, 2250120369, 1411522299, 3303015423, 4201082826, 2141356, 3523226940, 3271660092, 1520134973, 710437903, 1327475279, 3116625963, 49459859, 4048953446, 3440629313, 977984371, 2468225225, 3019674754, 1033836524, 2869280137, 1585458245, 3323845699, 711337648, 3016848609, 4284848641, 814667325, 816589957, 2875440878, 3355091090, 2512778156, 407061278, 256281167, 3808549285, 2294199801, 3766362243, 4104606717, 2246644404, 3673278797, 792800070, 1002998303, 3269596861, 2828052601, 1788431627, 1811660919, 1603016212, 547943632, 3210471320, 4218014539, 3179199730, 11336763, 2698516435}; static AXI_VALUE mt397_623[227] = { 3971432450, 2710817523, 260115364, 2929745220, 3424207919, 2024239854, 790315230, 2575918890, 3447856221, 605388972, 3540767091, 2429457096, 3918792011, 3811680002, 4122101248, 2229418955, 3988728042, 2209273483, 780727980, 4139736956, 4094993852, 1964088573, 937020687, 2266230671, 4276812102, 1056786143, 1760775842, 2273854199, 2504690002, 1259601466, 697164274, 2558699558, 3766721505, 2642834392, 1023500465, 522248325, 532857642, 3496901444, 2927666102, 440921016, 3985471821, 2816318332, 991348845, 4164577721, 2377531931, 3438704279, 1891499551, 3267711378, 3614886130, 3187491283, 1977274932, 3041283497, 1744447800, 307283519, 1940844446, 3818496639, 3304421553, 2001592832, 1504949548, 2148962697, 999805088, 4125815018, 2734472888, 1054788398, 4276340979, 888584318, 562313605, 2861949001, 1211540072, 2002208319, 3719816777, 1741780742, 1132967576, 3480350003, 2352039367, 2989094289, 2046726104, 3612926839, 2952188575, 2170691277, 3118198920, 3544564304, 63510430, 1766580534, 2197244820, 1545476624, 1447715736, 534064705, 1419971210, 2462354877, 2342528322, 3470652456, 912873952, 3452524362, 522486456, 107220100, 398956033, 3070998867, 3137566692, 4073874862, 690402354, 4161919916, 1130714366, 3094348175, 1004540822, 1526530596, 4143622032, 764856823, 1750327148, 597916923, 3695158274, 3689365345, 2530571687, 2444212503, 1878213704, 75760589, 79247074, 3550452524, 4199375502, 1478514853, 2964242105, 1411405005, 3753288323, 1904516744, 3823689494, 2144682579, 4158590565, 2524923466, 3368764533, 2802789020, 3642515557, 2190647374, 1840638989, 678203086, 3672282969, 3118129814, 413702777, 981382355, 3632743510, 2336388257, 1981301096, 2826043783, 2657994900, 96627530, 2015686735, 2773389028, 851540189, 437122641, 2827651350, 3515861766, 1250150428, 2699066005, 2823564984, 3928357768, 963027710, 799686238, 1176300351, 3120496544, 769036053, 2915807861, 2837453344, 4136928152, 3225608534, 2857133497, 3199350776, 2619007444, 1850778017, 2544789076, 1089234979, 1374505376, 1047279548, 1621728100, 3268828690, 1432434671, 2781573409, 616849419, 3710699924, 1907251665, 1552531791, 1810760710, 88102916, 2576932822, 4158964967, 3436950584, 935243916, 4002059650, 3375761196, 1383202515, 404090147, 2088979481, 2553143235, 555651697, 444940770, 3959616120, 910852054, 279443390, 1678470727, 577763824, 2558463491, 4138078649, 588745399, 1887873929, 2275768063, 232806697, 1163468934, 3863156381, 98860209, 2931223857, 3548439164, 814241145, 2111208476, 248872657, 3201295318, 868047110, 3045218241, 129563987, 4241645348, 1643932617, 280793167, 3385342099, 238248761, 3706628327, 2965809247, 3842323485, 952344643, 3150387165, 1550566506}; AXI_VALUE y; AXI_VALUE v; AXI_VALUE number; static int i2 = 0; static int i3 = 0; v = (mt0 & UPPER_MASK) | (mt1_396[i2] & LOWER_MASK); number = mt397_623[i3] ^ (v >> 1) ^ ((v & 1u) == 0 ? 0u : MATRIX_A); mt0 = mt1_396[i2]; mt1_396[i2] = mt397_623[i3]; mt397_623[i3] = number; y = number; y ^= (y >> 11); y ^= (y << 7) & 0x9d2c5680u; y ^= (y << 15) & 0xefc60000u; y ^= (y >> 18); i2 = (i2 >= 395) ? 0 : i2+1; i3 = (i3 >= 226) ? 0 : i3+1; random_out << y; }