From 22f0725aac300ed9b249f995df7889f6c203d1e0 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 22:48:29 +0000 Subject: vim-patch:8.1.2342: random number generator in Vim script is slow Problem: Random number generator in Vim script is slow. Solution: Add rand() and srand(). (Yasuhiro Matsumoto, closes vim/vim#1277) https://github.com/vim/vim/commit/06b0b4bc27077013e9b4b48fd1d9b33e543ccf99 Add missing method call usage to builtin.txt. vim_time and test_settime is N/A. Add a modeline to test_random.vim. Use typval_T* over listitem_T* vars so we don't need to use TV_LIST_ITEM_TV all over the place... Remove NULL list checks (tv_list_len covers this). --- src/nvim/eval.lua | 2 + src/nvim/eval/funcs.c | 95 ++++++++++++++++++++++++++++++++++++++++ src/nvim/testdir/test_random.vim | 31 +++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/nvim/testdir/test_random.vim (limited to 'src') diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 18967b80f2..ef2b8a0cc0 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -273,6 +273,7 @@ return { pyeval={args=1, base=1, func="f_py3eval"}, pyxeval={args=1, base=1, func="f_py3eval"}, perleval={args=1, base=1}, + rand={args={0, 1}, base=1}, range={args={1, 3}, base=1}, readdir={args={1, 2}, base=1}, readfile={args={1, 3}, base=1}, @@ -348,6 +349,7 @@ return { spellsuggest={args={1, 3}, base=1}, split={args={1, 3}, base=1}, sqrt={args=1, base=1, func="float_op_wrapper", data="&sqrt"}, + srand={args={0, 1}, base=1}, stdpath={args=1}, str2float={args=1, base=1}, str2list={args={1, 2}, base=1}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 50030f2994..96d8c93db3 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -22,6 +22,7 @@ #include "nvim/eval/encode.h" #include "nvim/eval/executor.h" #include "nvim/eval/funcs.h" +#include "nvim/eval/typval.h" #include "nvim/eval/userfunc.h" #include "nvim/ex_cmds2.h" #include "nvim/ex_docmd.h" @@ -6978,6 +6979,81 @@ static void f_py3eval(typval_T *argvars, typval_T *rettv, FunPtr fptr) script_host_eval("python3", argvars, rettv); } +/// "rand()" function +static void f_rand(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + uint32_t w; +#define SHUFFLE_XORSHIFT128 \ + const uint32_t t = x ^ (x << 11); \ + x = y; \ + y = z; \ + z = w; \ + w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); + + if (argvars[0].v_type == VAR_UNKNOWN) { + static bool rand_seed_initialized = false; + static uint32_t xyzw[4] = { 123456789, 362436069, 521288629, 88675123 }; + + // When argument is not given, return random number initialized + // statically. + if (!rand_seed_initialized) { + xyzw[0] = time(NULL); + rand_seed_initialized = true; + } + + uint32_t x = xyzw[0]; + uint32_t y = xyzw[1]; + uint32_t z = xyzw[2]; + w = xyzw[3]; + SHUFFLE_XORSHIFT128; + xyzw[0] = x; + xyzw[1] = y; + xyzw[2] = z; + xyzw[3] = w; + } else if (argvars[0].v_type == VAR_LIST) { + list_T *const l = argvars[0].vval.v_list; + if (tv_list_len(l) != 4) { + goto theend; + } + + typval_T *const tvx = TV_LIST_ITEM_TV(tv_list_find(l, 0L)); + typval_T *const tvy = TV_LIST_ITEM_TV(tv_list_find(l, 1L)); + typval_T *const tvz = TV_LIST_ITEM_TV(tv_list_find(l, 2L)); + typval_T *const tvw = TV_LIST_ITEM_TV(tv_list_find(l, 3L)); + if (tvx->v_type != VAR_NUMBER) { + goto theend; + } + if (tvy->v_type != VAR_NUMBER) { + goto theend; + } + if (tvz->v_type != VAR_NUMBER) { + goto theend; + } + if (tvw->v_type != VAR_NUMBER) { + goto theend; + } + uint32_t x = tvx->vval.v_number; + uint32_t y = tvy->vval.v_number; + uint32_t z = tvz->vval.v_number; + w = tvw->vval.v_number; + SHUFFLE_XORSHIFT128; + tvx->vval.v_number = (varnumber_T)x; + tvy->vval.v_number = (varnumber_T)y; + tvz->vval.v_number = (varnumber_T)z; + tvw->vval.v_number = (varnumber_T)w; + } else { + goto theend; + } + +#undef SHUFFLE_XORSHIFT128 + rettv->v_type = VAR_NUMBER; + rettv->vval.v_number = (varnumber_T)w; + return; + +theend: + semsg(_(e_invarg2), tv_get_string(&argvars[0])); +} + /// "perleval()" function static void f_perleval(typval_T *argvars, typval_T *rettv, FunPtr fptr) { @@ -10449,6 +10525,25 @@ static void f_stdpath(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } +/// "srand()" function +static void f_srand(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + tv_list_alloc_ret(rettv, 4); + if (argvars[0].v_type == VAR_UNKNOWN) { + tv_list_append_number(rettv->vval.v_list, (varnumber_T)time(NULL)); + } else { + bool error = false; + const uint32_t x = tv_get_number_chk(&argvars[0], &error); + if (error) { + return; + } + tv_list_append_number(rettv->vval.v_list, x); + } + tv_list_append_number(rettv->vval.v_list, 362436069); + tv_list_append_number(rettv->vval.v_list, 521288629); + tv_list_append_number(rettv->vval.v_list, 88675123); +} + /* * "str2float()" function */ diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim new file mode 100644 index 0000000000..bde034a96c --- /dev/null +++ b/src/nvim/testdir/test_random.vim @@ -0,0 +1,31 @@ +" Tests for srand() and rand() + +func Test_Rand() + let r = srand(123456789) + call assert_equal([123456789, 362436069, 521288629, 88675123], r) + call assert_equal(3701687786, rand(r)) + call assert_equal(458299110, rand(r)) + call assert_equal(2500872618, rand(r)) + call assert_equal(3633119408, rand(r)) + call assert_equal(516391518, rand(r)) + + " Nvim does not support test_settime + " call test_settime(12341234) + " let s = srand() + " call assert_equal(s, srand()) + " call test_settime(12341235) + " call assert_notequal(s, srand()) + + call srand() + let v = rand() + call assert_notequal(v, rand()) + + call assert_fails('echo srand([1])', 'E745:') + call assert_fails('echo rand([1, 2, 3])', 'E475:') + call assert_fails('echo rand([[1], 2, 3, 4])', 'E475:') + call assert_fails('echo rand([1, [2], 3, 4])', 'E475:') + call assert_fails('echo rand([1, 2, [3], 4])', 'E475:') + call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:') +endfunc + +" vim: shiftwidth=2 sts=2 expandtab -- cgit From f6a0d5498b5f0d62e10f7ba891bc6ea5e20dad69 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 23:53:55 +0000 Subject: vim-patch:8.1.2343: using time() for srand() is not very random Problem: Using time() for srand() is not very random. Solution: use /dev/urandom if available https://github.com/vim/vim/commit/07e4a197953d12902fb97beb48830a5323a52280 Use os_open and os_close. time_settime is N/A, so some parts of the test are disabled. There's maybe a very, very, very, very small chance the /dev/urandom test fails, but it shouldn't matter. :P --- src/nvim/eval/funcs.c | 32 ++++++++++++++++++++++++++++++-- src/nvim/testdir/test_random.vim | 16 ++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 96d8c93db3..6a0803704d 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -10528,16 +10528,44 @@ static void f_stdpath(typval_T *argvars, typval_T *rettv, FunPtr fptr) /// "srand()" function static void f_srand(typval_T *argvars, typval_T *rettv, FunPtr fptr) { + static int dev_urandom_state = -1; // FAIL or OK once tried + tv_list_alloc_ret(rettv, 4); if (argvars[0].v_type == VAR_UNKNOWN) { - tv_list_append_number(rettv->vval.v_list, (varnumber_T)time(NULL)); + if (dev_urandom_state != FAIL) { + const int fd = os_open("/dev/urandom", O_RDONLY, 0); + struct { + union { + uint32_t number; + char bytes[sizeof(uint32_t)]; + } cont; + } buf; + + // Attempt reading /dev/urandom. + if (fd == -1) { + dev_urandom_state = FAIL; + } else { + buf.cont.number = 0; + if (read(fd, buf.cont.bytes, sizeof(uint32_t)) != sizeof(uint32_t)) { + dev_urandom_state = FAIL; + } else { + dev_urandom_state = OK; + tv_list_append_number(rettv->vval.v_list, (varnumber_T)buf.cont.number); + } + os_close(fd); + } + } + if (dev_urandom_state != OK) { + // Reading /dev/urandom doesn't work, fall back to time(). + tv_list_append_number(rettv->vval.v_list, (varnumber_T)time(NULL)); + } } else { bool error = false; const uint32_t x = tv_get_number_chk(&argvars[0], &error); if (error) { return; } - tv_list_append_number(rettv->vval.v_list, x); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)x); } tv_list_append_number(rettv->vval.v_list, 362436069); tv_list_append_number(rettv->vval.v_list, 521288629); diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index bde034a96c..46091836d4 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -11,10 +11,16 @@ func Test_Rand() " Nvim does not support test_settime " call test_settime(12341234) - " let s = srand() - " call assert_equal(s, srand()) - " call test_settime(12341235) - " call assert_notequal(s, srand()) + let s = srand() + if filereadable('/dev/urandom') + " using /dev/urandom + call assert_notequal(s, srand()) + " else + " " using time() + " call assert_equal(s, srand()) + " call test_settime(12341235) + " call assert_notequal(s, srand()) + endif call srand() let v = rand() @@ -26,6 +32,8 @@ func Test_Rand() call assert_fails('echo rand([1, [2], 3, 4])', 'E475:') call assert_fails('echo rand([1, 2, [3], 4])', 'E475:') call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:') + + " call test_settime(0) endfunc " vim: shiftwidth=2 sts=2 expandtab -- cgit From c97614d98fc7ab040851b7fe1bc4cb575ce8c627 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 9 Jan 2022 23:26:03 +0000 Subject: vim-patch:8.1.2356: rand() does not use the best algorithm Problem: rand() does not use the best algorithm. Solution: use xoshiro128** instead of xorshift. (Kaito Udagawa, closes vim/vim#5279) https://github.com/vim/vim/commit/f8c1f9200c4b50969a8191a4fe0b0d09edb38979 --- src/nvim/eval/funcs.c | 137 +++++++++++++++++++++------------------ src/nvim/testdir/test_random.vim | 12 ++-- 2 files changed, 81 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 6a0803704d..39cbb4823a 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -6982,76 +6982,79 @@ static void f_py3eval(typval_T *argvars, typval_T *rettv, FunPtr fptr) /// "rand()" function static void f_rand(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - uint32_t w; -#define SHUFFLE_XORSHIFT128 \ - const uint32_t t = x ^ (x << 11); \ - x = y; \ - y = z; \ - z = w; \ - w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); + list_T *l = NULL; if (argvars[0].v_type == VAR_UNKNOWN) { - static bool rand_seed_initialized = false; - static uint32_t xyzw[4] = { 123456789, 362436069, 521288629, 88675123 }; - - // When argument is not given, return random number initialized - // statically. - if (!rand_seed_initialized) { - xyzw[0] = time(NULL); - rand_seed_initialized = true; - } - - uint32_t x = xyzw[0]; - uint32_t y = xyzw[1]; - uint32_t z = xyzw[2]; - w = xyzw[3]; - SHUFFLE_XORSHIFT128; - xyzw[0] = x; - xyzw[1] = y; - xyzw[2] = z; - xyzw[3] = w; + static list_T *globl = NULL; + + // When no argument is given use the global seed list. + if (globl == NULL) { + // Initialize the global seed list. + f_srand(argvars, rettv, fptr); + l = rettv->vval.v_list; + if (tv_list_len(l) != 4) { + tv_clear(rettv); + goto theend; + } + globl = l; + } else { + l = globl; + } } else if (argvars[0].v_type == VAR_LIST) { - list_T *const l = argvars[0].vval.v_list; + l = argvars[0].vval.v_list; if (tv_list_len(l) != 4) { goto theend; } - - typval_T *const tvx = TV_LIST_ITEM_TV(tv_list_find(l, 0L)); - typval_T *const tvy = TV_LIST_ITEM_TV(tv_list_find(l, 1L)); - typval_T *const tvz = TV_LIST_ITEM_TV(tv_list_find(l, 2L)); - typval_T *const tvw = TV_LIST_ITEM_TV(tv_list_find(l, 3L)); - if (tvx->v_type != VAR_NUMBER) { - goto theend; - } - if (tvy->v_type != VAR_NUMBER) { - goto theend; - } - if (tvz->v_type != VAR_NUMBER) { - goto theend; - } - if (tvw->v_type != VAR_NUMBER) { - goto theend; - } - uint32_t x = tvx->vval.v_number; - uint32_t y = tvy->vval.v_number; - uint32_t z = tvz->vval.v_number; - w = tvw->vval.v_number; - SHUFFLE_XORSHIFT128; - tvx->vval.v_number = (varnumber_T)x; - tvy->vval.v_number = (varnumber_T)y; - tvz->vval.v_number = (varnumber_T)z; - tvw->vval.v_number = (varnumber_T)w; } else { goto theend; } -#undef SHUFFLE_XORSHIFT128 + typval_T *const tvx = TV_LIST_ITEM_TV(tv_list_find(l, 0L)); + typval_T *const tvy = TV_LIST_ITEM_TV(tv_list_find(l, 1L)); + typval_T *const tvz = TV_LIST_ITEM_TV(tv_list_find(l, 2L)); + typval_T *const tvw = TV_LIST_ITEM_TV(tv_list_find(l, 3L)); + if (tvx->v_type != VAR_NUMBER) { + goto theend; + } + if (tvy->v_type != VAR_NUMBER) { + goto theend; + } + if (tvz->v_type != VAR_NUMBER) { + goto theend; + } + if (tvw->v_type != VAR_NUMBER) { + goto theend; + } + uint32_t x = tvx->vval.v_number; + uint32_t y = tvy->vval.v_number; + uint32_t z = tvz->vval.v_number; + uint32_t w = tvw->vval.v_number; + + // SHUFFLE_XOSHIRO128STARSTAR +#define ROTL(x, k) ((x << k) | (x >> (32 - k))) + const uint32_t result = ROTL(y * 5, 7) * 9; + const uint32_t t = y << 9; + z ^= x; + w ^= y; + y ^= z; + x ^= w; + z ^= t; + w = ROTL(w, 11); +#undef ROTL + + tvx->vval.v_number = (varnumber_T)x; + tvy->vval.v_number = (varnumber_T)y; + tvz->vval.v_number = (varnumber_T)z; + tvw->vval.v_number = (varnumber_T)w; + rettv->v_type = VAR_NUMBER; - rettv->vval.v_number = (varnumber_T)w; + rettv->vval.v_number = (varnumber_T)result; return; theend: semsg(_(e_invarg2), tv_get_string(&argvars[0])); + rettv->v_type = VAR_NUMBER; + rettv->vval.v_number = -1; } /// "perleval()" function @@ -10529,6 +10532,7 @@ static void f_stdpath(typval_T *argvars, typval_T *rettv, FunPtr fptr) static void f_srand(typval_T *argvars, typval_T *rettv, FunPtr fptr) { static int dev_urandom_state = -1; // FAIL or OK once tried + uint32_t x = 0; tv_list_alloc_ret(rettv, 4); if (argvars[0].v_type == VAR_UNKNOWN) { @@ -10550,26 +10554,35 @@ static void f_srand(typval_T *argvars, typval_T *rettv, FunPtr fptr) dev_urandom_state = FAIL; } else { dev_urandom_state = OK; - tv_list_append_number(rettv->vval.v_list, (varnumber_T)buf.cont.number); + x = buf.cont.number; } os_close(fd); } } if (dev_urandom_state != OK) { // Reading /dev/urandom doesn't work, fall back to time(). - tv_list_append_number(rettv->vval.v_list, (varnumber_T)time(NULL)); + x = time(NULL); } } else { bool error = false; - const uint32_t x = tv_get_number_chk(&argvars[0], &error); + x = tv_get_number_chk(&argvars[0], &error); if (error) { return; } - tv_list_append_number(rettv->vval.v_list, (varnumber_T)x); } - tv_list_append_number(rettv->vval.v_list, 362436069); - tv_list_append_number(rettv->vval.v_list, 521288629); - tv_list_append_number(rettv->vval.v_list, 88675123); + + uint32_t z; +#define SPLITMIX32 ( \ + z = (x += 0x9e3779b9), \ + z = (z ^ (z >> 16)) * 0x85ebca6b, \ + z = (z ^ (z >> 13)) * 0xc2b2ae35, \ + z ^ (z >> 16)) + + tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); +#undef SPLITMIX32 } /* diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index 46091836d4..ed98433b94 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -2,12 +2,12 @@ func Test_Rand() let r = srand(123456789) - call assert_equal([123456789, 362436069, 521288629, 88675123], r) - call assert_equal(3701687786, rand(r)) - call assert_equal(458299110, rand(r)) - call assert_equal(2500872618, rand(r)) - call assert_equal(3633119408, rand(r)) - call assert_equal(516391518, rand(r)) + call assert_equal([1573771921, 319883699, 2742014374, 1324369493], r) + call assert_equal(4284103975, rand(r)) + call assert_equal(1001954530, rand(r)) + call assert_equal(2701803082, rand(r)) + call assert_equal(2658065534, rand(r)) + call assert_equal(3104308804, rand(r)) " Nvim does not support test_settime " call test_settime(12341234) -- cgit From cc7ccf6d31d14457070055be07884edaf2eb165f Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 10 Jan 2022 00:49:07 +0000 Subject: vim-patch:8.1.2357: no test with wrong argument for rand() Problem: No test with wrong argument for rand(). Solution: Add a test case. https://github.com/vim/vim/commit/68e9e5f7fccb8038cf0ca5b5d95c85a923152f46 --- src/nvim/testdir/test_random.vim | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index ed98433b94..f953b93d51 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -26,7 +26,11 @@ func Test_Rand() let v = rand() call assert_notequal(v, rand()) + if has('float') + call assert_fails('echo srand(1.2)', 'E805:') + endif call assert_fails('echo srand([1])', 'E745:') + call assert_fails('echo rand("burp")', 'E475:') call assert_fails('echo rand([1, 2, 3])', 'E475:') call assert_fails('echo rand([[1], 2, 3, 4])', 'E475:') call assert_fails('echo rand([1, [2], 3, 4])', 'E475:') -- cgit From 4f7a8991a93ddb1b6ab7cd8a8f21b5197c4612bb Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 10 Jan 2022 10:31:16 +0000 Subject: vim-patch:8.2.0233: crash when using garbagecollect() in between rand() Problem: Crash when using garbagecollect() in between rand(). Solution: Redesign the rand() and srand() implementation. (Yasuhiro Matsumoto, closes vim/vim#5587, closes vim/vim#5588) https://github.com/vim/vim/commit/4f645c54efe33d7a11e314676e503118761f08a7 Omit test_srand_seed. Unmacroify SHUFFLE_XOSHIRO128STARSTAR and SPLITMIX32 while we're at it (leave ROTL alone as it's fairly innocent). --- src/nvim/eval/funcs.c | 237 +++++++++++++++++++++------------------ src/nvim/testdir/test_random.vim | 16 ++- 2 files changed, 141 insertions(+), 112 deletions(-) (limited to 'src') diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 39cbb4823a..b96ff50787 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -6979,73 +6979,129 @@ static void f_py3eval(typval_T *argvars, typval_T *rettv, FunPtr fptr) script_host_eval("python3", argvars, rettv); } +static void init_srand(uint32_t *const x) + FUNC_ATTR_NONNULL_ALL +{ +#ifndef MSWIN + static int dev_urandom_state = NOTDONE; // FAIL or OK once tried + + if (dev_urandom_state != FAIL) { + const int fd = os_open("/dev/urandom", O_RDONLY, 0); + struct { + union { + uint32_t number; + char bytes[sizeof(uint32_t)]; + } contents; + } buf; + + // Attempt reading /dev/urandom. + if (fd == -1) { + dev_urandom_state = FAIL; + } else { + buf.contents.number = 0; + if (read(fd, buf.contents.bytes, sizeof(uint32_t)) != sizeof(uint32_t)) { + dev_urandom_state = FAIL; + } else { + dev_urandom_state = OK; + *x = buf.contents.number; + } + os_close(fd); + } + } + if (dev_urandom_state != OK) { + // Reading /dev/urandom doesn't work, fall back to time(). +#endif + *x = time(NULL); +#ifndef MSWIN + } +#endif +} + +static inline uint32_t splitmix32(uint32_t *const x) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE +{ + uint32_t z = (*x += 0x9e3779b9); + z = (z ^ (z >> 16)) * 0x85ebca6b; + z = (z ^ (z >> 13)) * 0xc2b2ae35; + return z ^ (z >> 16); +} + +static inline uint32_t shuffle_xoshiro128starstar(uint32_t *const x, uint32_t *const y, + uint32_t *const z, uint32_t *const w) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE +{ +#define ROTL(x, k) ((x << k) | (x >> (32 - k))) + const uint32_t result = ROTL(*y * 5, 7) * 9; + const uint32_t t = *y << 9; + *z ^= *x; + *w ^= *y; + *y ^= *z; + *x ^= *w; + *z ^= t; + *w = ROTL(*w, 11); +#undef ROTL + return result; +} + /// "rand()" function static void f_rand(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - list_T *l = NULL; + uint32_t result; if (argvars[0].v_type == VAR_UNKNOWN) { - static list_T *globl = NULL; + static uint32_t gx, gy, gz, gw; + static bool initialized = false; // When no argument is given use the global seed list. - if (globl == NULL) { + if (!initialized) { // Initialize the global seed list. - f_srand(argvars, rettv, fptr); - l = rettv->vval.v_list; - if (tv_list_len(l) != 4) { - tv_clear(rettv); - goto theend; - } - globl = l; - } else { - l = globl; + uint32_t x; + init_srand(&x); + + gx = splitmix32(&x); + gy = splitmix32(&x); + gz = splitmix32(&x); + gw = splitmix32(&x); + initialized = true; } + + result = shuffle_xoshiro128starstar(&gx, &gy, &gz, &gw); } else if (argvars[0].v_type == VAR_LIST) { - l = argvars[0].vval.v_list; + list_T *const l = argvars[0].vval.v_list; if (tv_list_len(l) != 4) { goto theend; } - } else { - goto theend; - } - typval_T *const tvx = TV_LIST_ITEM_TV(tv_list_find(l, 0L)); - typval_T *const tvy = TV_LIST_ITEM_TV(tv_list_find(l, 1L)); - typval_T *const tvz = TV_LIST_ITEM_TV(tv_list_find(l, 2L)); - typval_T *const tvw = TV_LIST_ITEM_TV(tv_list_find(l, 3L)); - if (tvx->v_type != VAR_NUMBER) { - goto theend; - } - if (tvy->v_type != VAR_NUMBER) { - goto theend; - } - if (tvz->v_type != VAR_NUMBER) { - goto theend; - } - if (tvw->v_type != VAR_NUMBER) { - goto theend; - } - uint32_t x = tvx->vval.v_number; - uint32_t y = tvy->vval.v_number; - uint32_t z = tvz->vval.v_number; - uint32_t w = tvw->vval.v_number; + typval_T *const tvx = TV_LIST_ITEM_TV(tv_list_find(l, 0L)); + typval_T *const tvy = TV_LIST_ITEM_TV(tv_list_find(l, 1L)); + typval_T *const tvz = TV_LIST_ITEM_TV(tv_list_find(l, 2L)); + typval_T *const tvw = TV_LIST_ITEM_TV(tv_list_find(l, 3L)); + if (tvx->v_type != VAR_NUMBER) { + goto theend; + } + if (tvy->v_type != VAR_NUMBER) { + goto theend; + } + if (tvz->v_type != VAR_NUMBER) { + goto theend; + } + if (tvw->v_type != VAR_NUMBER) { + goto theend; + } + uint32_t x = tvx->vval.v_number; + uint32_t y = tvy->vval.v_number; + uint32_t z = tvz->vval.v_number; + uint32_t w = tvw->vval.v_number; - // SHUFFLE_XOSHIRO128STARSTAR -#define ROTL(x, k) ((x << k) | (x >> (32 - k))) - const uint32_t result = ROTL(y * 5, 7) * 9; - const uint32_t t = y << 9; - z ^= x; - w ^= y; - y ^= z; - x ^= w; - z ^= t; - w = ROTL(w, 11); -#undef ROTL + result = shuffle_xoshiro128starstar(&x, &y, &z, &w); - tvx->vval.v_number = (varnumber_T)x; - tvy->vval.v_number = (varnumber_T)y; - tvz->vval.v_number = (varnumber_T)z; - tvw->vval.v_number = (varnumber_T)w; + tvx->vval.v_number = (varnumber_T)x; + tvy->vval.v_number = (varnumber_T)y; + tvz->vval.v_number = (varnumber_T)z; + tvw->vval.v_number = (varnumber_T)w; + } else { + goto theend; + } rettv->v_type = VAR_NUMBER; rettv->vval.v_number = (varnumber_T)result; @@ -7057,6 +7113,28 @@ theend: rettv->vval.v_number = -1; } +/// "srand()" function +static void f_srand(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + uint32_t x = 0; + + tv_list_alloc_ret(rettv, 4); + if (argvars[0].v_type == VAR_UNKNOWN) { + init_srand(&x); + } else { + bool error = false; + x = tv_get_number_chk(&argvars[0], &error); + if (error) { + return; + } + } + + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); +} + /// "perleval()" function static void f_perleval(typval_T *argvars, typval_T *rettv, FunPtr fptr) { @@ -10528,63 +10606,6 @@ static void f_stdpath(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } -/// "srand()" function -static void f_srand(typval_T *argvars, typval_T *rettv, FunPtr fptr) -{ - static int dev_urandom_state = -1; // FAIL or OK once tried - uint32_t x = 0; - - tv_list_alloc_ret(rettv, 4); - if (argvars[0].v_type == VAR_UNKNOWN) { - if (dev_urandom_state != FAIL) { - const int fd = os_open("/dev/urandom", O_RDONLY, 0); - struct { - union { - uint32_t number; - char bytes[sizeof(uint32_t)]; - } cont; - } buf; - - // Attempt reading /dev/urandom. - if (fd == -1) { - dev_urandom_state = FAIL; - } else { - buf.cont.number = 0; - if (read(fd, buf.cont.bytes, sizeof(uint32_t)) != sizeof(uint32_t)) { - dev_urandom_state = FAIL; - } else { - dev_urandom_state = OK; - x = buf.cont.number; - } - os_close(fd); - } - } - if (dev_urandom_state != OK) { - // Reading /dev/urandom doesn't work, fall back to time(). - x = time(NULL); - } - } else { - bool error = false; - x = tv_get_number_chk(&argvars[0], &error); - if (error) { - return; - } - } - - uint32_t z; -#define SPLITMIX32 ( \ - z = (x += 0x9e3779b9), \ - z = (z ^ (z >> 16)) * 0x85ebca6b, \ - z = (z ^ (z >> 13)) * 0xc2b2ae35, \ - z ^ (z >> 16)) - - tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); - tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); - tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); - tv_list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32); -#undef SPLITMIX32 -} - /* * "str2float()" function */ diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim index f953b93d51..6d3f7dcfd9 100644 --- a/src/nvim/testdir/test_random.vim +++ b/src/nvim/testdir/test_random.vim @@ -12,7 +12,7 @@ func Test_Rand() " Nvim does not support test_settime " call test_settime(12341234) let s = srand() - if filereadable('/dev/urandom') + if !has('win32') && filereadable('/dev/urandom') " using /dev/urandom call assert_notequal(s, srand()) " else @@ -22,9 +22,11 @@ func Test_Rand() " call assert_notequal(s, srand()) endif - call srand() - let v = rand() - call assert_notequal(v, rand()) + " Nvim does not support test_srand_seed + " call test_srand_seed(123456789) + " call assert_equal(4284103975, rand()) + " call assert_equal(1001954530, rand()) + " call test_srand_seed() if has('float') call assert_fails('echo srand(1.2)', 'E805:') @@ -40,4 +42,10 @@ func Test_Rand() " call test_settime(0) endfunc +func Test_issue_5587() + call rand() + call garbagecollect() + call rand() +endfunc + " vim: shiftwidth=2 sts=2 expandtab -- cgit