vha_mt19937.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. A C-program for MT19937, with initialization improved 2002/1/26.
  3. Coded by Takuji Nishimura and Makoto Matsumoto.
  4. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  5. All rights reserved.
  6. Copyright (C) 2005, Mutsuo Saito,
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. The names of its contributors may not be used to endorse or promote
  17. products derived from this software without specific prior written
  18. permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. Any feedback is very welcome.
  31. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  32. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  33. Original code available at:
  34. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
  35. */
  36. /*
  37. *****************************************************************************
  38. * Copyright (c) Imagination Technologies Ltd.
  39. *
  40. * The contents of this file are subject to the MIT license as set out below.
  41. *
  42. * Permission is hereby granted, free of charge, to any person obtaining a
  43. * copy of this software and associated documentation files (the "Software"),
  44. * to deal in the Software without restriction, including without limitation
  45. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  46. * and/or sell copies of the Software, and to permit persons to whom the
  47. * Software is furnished to do so, subject to the following conditions:
  48. *
  49. * The above copyright notice and this permission notice shall be included in
  50. * all copies or substantial portions of the Software.
  51. *
  52. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  53. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  54. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  55. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  56. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  57. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  58. * THE SOFTWARE.
  59. *
  60. * Alternatively, the contents of this file may be used under the terms of the
  61. * GNU General Public License Version 2 ("GPL")in which case the provisions of
  62. * GPL are applicable instead of those above.
  63. *
  64. * If you wish to allow use of your version of this file only under the terms
  65. * of GPL, and not to allow others to use your version of this file under the
  66. * terms of the MIT license, indicate your decision by deleting the provisions
  67. * above and replace them with the notice and other provisions required by GPL
  68. * as set out in the file called "GPLHEADER" included in this distribution. If
  69. * you do not delete the provisions above, a recipient may use your version of
  70. * this file under the terms of either the MIT license or GPL.
  71. *
  72. * This License is also included in this distribution in the file called
  73. * "MIT_COPYING".
  74. *
  75. *****************************************************************************/
  76. #include <linux/kernel.h>
  77. #include <linux/slab.h>
  78. #include "vha_mt19937.h"
  79. /* Period parameters */
  80. #define N 624
  81. #define M 397
  82. #define MATRIX_A 0x9908b0dfUL /* constant vector a */
  83. #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
  84. #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
  85. struct vha_mt19937_ctx
  86. {
  87. uint32_t mt[N]; /*!< The array for the state vector. */
  88. int32_t mti; /*!< */
  89. uint32_t mag01[2]; /*!< mag01[x] = x * MATRIX_A for x=0,1 */
  90. };
  91. int vha_mt19937_init(uint32_t seed, void **handle)
  92. {
  93. struct vha_mt19937_ctx *ctx;
  94. /* Check input params. */
  95. if (NULL == handle) {
  96. pr_err("%s: invalid handle\n", __func__);
  97. return -EINVAL;
  98. }
  99. /* Allocate SFMT context. */
  100. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  101. if (ctx == NULL) {
  102. pr_err("%s: failed to allocate context\n", __func__);
  103. return -ENOMEM;
  104. }
  105. /* Initialise SFMT context with the seed. */
  106. ctx->mt[0]= seed & 0xffffffffUL;
  107. for (ctx->mti = 1; ctx->mti < N; ctx->mti++) {
  108. ctx->mt[ctx->mti] =
  109. (1812433253UL * (ctx->mt[ctx->mti - 1] ^ (ctx->mt[ctx->mti - 1] >> 30)) + ctx->mti);
  110. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  111. /* In the previous versions, MSBs of the seed affect */
  112. /* only MSBs of the array mt[]. */
  113. /* 2002/01/09 modified by Makoto Matsumoto */
  114. ctx->mt[ctx->mti] &= 0xffffffffUL;
  115. /* for >32 bit machines */
  116. }
  117. ctx->mag01[0] = 0x0UL;
  118. ctx->mag01[1] = MATRIX_A;
  119. *handle = ctx;
  120. return 0;
  121. }
  122. int vha_mt19937_deinit(void *handle)
  123. {
  124. struct vha_mt19937_ctx *ctx = (struct vha_mt19937_ctx*)handle;
  125. /* Check input params. */
  126. if (NULL == handle) {
  127. pr_err("%s: invalid handle\n", __func__);
  128. return -EINVAL;
  129. }
  130. /* Free the SFMT context. */
  131. kfree(ctx);
  132. return 0;
  133. }
  134. int vha_mt19937_gen_uint32(void *handle, uint32_t *rand_val)
  135. {
  136. struct vha_mt19937_ctx *ctx = (struct vha_mt19937_ctx*)handle;
  137. uint32_t y;
  138. /* Check input params. */
  139. if (NULL == handle) {
  140. pr_err("%s: invalid handle\n", __func__);
  141. return -EINVAL;
  142. }
  143. if (NULL == rand_val) {
  144. pr_err("%s: invalid rand_val\n", __func__);
  145. return -EINVAL;
  146. }
  147. /* Generate N words at one time. */
  148. if (ctx->mti >= N) {
  149. int kk;
  150. for (kk = 0; kk < (N - M); kk++) {
  151. y = (ctx->mt[kk] & UPPER_MASK) | (ctx->mt[kk + 1] & LOWER_MASK);
  152. ctx->mt[kk] = ctx->mt[kk + M] ^ (y >> 1) ^ ctx->mag01[y & 0x1UL];
  153. }
  154. for (; kk < (N - 1); kk++) {
  155. y = (ctx->mt[kk] & UPPER_MASK) | (ctx->mt[kk + 1] & LOWER_MASK);
  156. ctx->mt[kk] = ctx->mt[kk + (M - N)] ^ (y >> 1) ^ ctx->mag01[y & 0x1UL];
  157. }
  158. y = (ctx->mt[N - 1] & UPPER_MASK) | (ctx->mt[0] & LOWER_MASK);
  159. ctx->mt[N - 1] = ctx->mt[M - 1] ^ (y >> 1) ^ ctx->mag01[y & 0x1UL];
  160. ctx->mti = 0;
  161. }
  162. y = ctx->mt[ctx->mti++];
  163. /* Tempering */
  164. y ^= (y >> 11);
  165. y ^= (y << 7) & 0x9d2c5680UL;
  166. y ^= (y << 15) & 0xefc60000UL;
  167. y ^= (y >> 18);
  168. *rand_val = y;
  169. return 0;
  170. }
  171. int vha_mt19937_gen_range(void *handle, uint32_t min, uint32_t max, uint32_t *rand_val)
  172. {
  173. int ret;
  174. uint32_t range_width;
  175. /* Generate 32-bit random value. */
  176. ret = vha_mt19937_gen_uint32(handle, rand_val);
  177. if (ret != 0)
  178. return ret;
  179. /* Calculate the range. */
  180. range_width = abs(max - min);
  181. if (min > max)
  182. {
  183. min = max;
  184. }
  185. /* Calculate the random value within range. */
  186. *rand_val = (range_width > 0) ? ((*rand_val % (range_width + 1)) + min) : min;
  187. return 0;
  188. }