diff --git a/3rdparty/tinymt/tinymt64.cc b/3rdparty/tinymt/tinymt64.cc new file mode 100644 index 000000000..fa5a06c1e --- /dev/null +++ b/3rdparty/tinymt/tinymt64.cc @@ -0,0 +1,129 @@ +/** + * @file tinymt64.c + * + * @brief 64-bit Tiny Mersenne Twister only 127 bit internal state + * + * @author Mutsuo Saito (Hiroshima University) + * @author Makoto Matsumoto (The University of Tokyo) + * + * Copyright (C) 2011 Mutsuo Saito, Makoto Matsumoto, + * Hiroshima University and The University of Tokyo. + * All rights reserved. + * + * The 3-clause BSD License is applied to this software, see + * LICENSE.txt + */ +#include "tinymt64.h" + +#define MIN_LOOP 8 + +/** + * This function represents a function used in the initialization + * by init_by_array + * @param[in] x 64-bit integer + * @return 64-bit integer + */ +static uint64_t ini_func1(uint64_t x) { + return (x ^ (x >> 59)) * UINT64_C(2173292883993); +} + +/** + * This function represents a function used in the initialization + * by init_by_array + * @param[in] x 64-bit integer + * @return 64-bit integer + */ +static uint64_t ini_func2(uint64_t x) { + return (x ^ (x >> 59)) * UINT64_C(58885565329898161); +} + +/** + * This function certificate the period of 2^127-1. + * @param random tinymt state vector. + */ +static void period_certification(tinymt64_t * random) { + if ((random->status[0] & TINYMT64_MASK) == 0 && + random->status[1] == 0) { + random->status[0] = 'T'; + random->status[1] = 'M'; + } +} + +/** + * This function initializes the internal state array with a 64-bit + * unsigned integer seed. + * @param random tinymt state vector. + * @param seed a 64-bit unsigned integer used as a seed. + */ +void tinymt64_init(tinymt64_t * random, uint64_t seed) { + random->status[0] = seed ^ ((uint64_t)random->mat1 << 32); + random->status[1] = random->mat2 ^ random->tmat; + for (int i = 1; i < MIN_LOOP; i++) { + random->status[i & 1] ^= i + UINT64_C(6364136223846793005) + * (random->status[(i - 1) & 1] + ^ (random->status[(i - 1) & 1] >> 62)); + } + period_certification(random); +} + +/** + * This function initializes the internal state array, + * with an array of 64-bit unsigned integers used as seeds + * @param random tinymt state vector. + * @param init_key the array of 64-bit integers, used as a seed. + * @param key_length the length of init_key. + */ +void tinymt64_init_by_array(tinymt64_t * random, const uint64_t init_key[], + int key_length) { + const int lag = 1; + const int mid = 1; + const int size = 4; + int i, j; + int count; + uint64_t r; + uint64_t st[4]; + + st[0] = 0; + st[1] = random->mat1; + st[2] = random->mat2; + st[3] = random->tmat; + if (key_length + 1 > MIN_LOOP) { + count = key_length + 1; + } else { + count = MIN_LOOP; + } + r = ini_func1(st[0] ^ st[mid % size] + ^ st[(size - 1) % size]); + st[mid % size] += r; + r += key_length; + st[(mid + lag) % size] += r; + st[0] = r; + count--; + for (i = 1, j = 0; (j < count) && (j < key_length); j++) { + r = ini_func1(st[i] ^ st[(i + mid) % size] ^ st[(i + size - 1) % size]); + st[(i + mid) % size] += r; + r += init_key[j] + i; + st[(i + mid + lag) % size] += r; + st[i] = r; + i = (i + 1) % size; + } + for (; j < count; j++) { + r = ini_func1(st[i] ^ st[(i + mid) % size] ^ st[(i + size - 1) % size]); + st[(i + mid) % size] += r; + r += i; + st[(i + mid + lag) % size] += r; + st[i] = r; + i = (i + 1) % size; + } + for (j = 0; j < size; j++) { + r = ini_func2(st[i] + st[(i + mid) % size] + st[(i + size - 1) % size]); + st[(i + mid) % size] ^= r; + r -= i; + st[(i + mid + lag) % size] ^= r; + st[i] = r; + i = (i + 1) % size; + } + random->status[0] = st[0] ^ st[1]; + random->status[1] = st[2] ^ st[3]; + period_certification(random); +} diff --git a/premake4.lua b/premake4.lua index 6e43e107e..2e67da7bb 100644 --- a/premake4.lua +++ b/premake4.lua @@ -7,9 +7,6 @@ solution "Circa" objdir "build/obj" includedirs { "include", "src", "3rdparty" } - configuration "*.cpp" - buildoptions { "-std=c++11" } - configuration "Release" flags { "OptimizeSpeed" } @@ -27,7 +24,7 @@ solution "Circa" "src/generated/all_builtin_types.cpp", "src/generated/setup_builtin_functions.cpp", "src/generated/stdlib_script_text.cpp", - "3rdparty/tinymt/tinymt64.c" + "3rdparty/tinymt/tinymt64.cc" } configuration "Debug" diff --git a/src/common_headers.h b/src/common_headers.h index 0deff1457..39c6e7cb1 100644 --- a/src/common_headers.h +++ b/src/common_headers.h @@ -2,6 +2,9 @@ #pragma once +// tinymt include headers need this: +#define __STDC_CONSTANT_MACROS + #ifdef _MSC_VER // Special handling for Windows diff --git a/src/hashtable.cpp b/src/hashtable.cpp index d5f22cb84..7a176be01 100644 --- a/src/hashtable.cpp +++ b/src/hashtable.cpp @@ -9,7 +9,7 @@ #include "names.h" #include "string_type.h" #include "symbols.h" -#include "tagged_Value.h" +#include "tagged_value.h" #include "type.h" namespace circa { diff --git a/src/rand.cpp b/src/rand.cpp index c6e1d777e..07b5df02c 100644 --- a/src/rand.cpp +++ b/src/rand.cpp @@ -1,5 +1,7 @@ // Copyright (c) Andrew Fischer. See LICENSE file for license terms. +#include "common_headers.h" + #include "rand.h" namespace circa { diff --git a/src/static_lib.make b/src/static_lib.make index fb7a2b684..f0f888dc2 100644 --- a/src/static_lib.make +++ b/src/static_lib.make @@ -365,8 +365,8 @@ $(OBJDIR)/setup_builtin_functions.o: generated/setup_builtin_functions.cpp $(OBJDIR)/stdlib_script_text.o: generated/stdlib_script_text.cpp @echo $(notdir $<) $(SILENT) $(CXX) $(CXXFLAGS) -o "$@" -c "$<" -$(OBJDIR)/tinymt64.o: ../3rdparty/tinymt/tinymt64.c +$(OBJDIR)/tinymt64.o: ../3rdparty/tinymt/tinymt64.cc @echo $(notdir $<) - $(SILENT) $(CC) $(CFLAGS) -o "$@" -c "$<" + $(SILENT) $(CXX) $(CXXFLAGS) -o "$@" -c "$<" -include $(OBJECTS:%.o=%.d)