2012-07-27 19:11:45 +02:00
|
|
|
/*
|
|
|
|
* This software is licensed under the terms of the MIT-License
|
2017-02-11 04:52:08 +01:00
|
|
|
* See COPYING for further information.
|
2012-07-27 19:11:45 +02:00
|
|
|
* ---
|
2019-01-23 21:10:43 +01:00
|
|
|
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
|
|
|
|
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@alienslab.net>.
|
2012-07-27 19:11:45 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-25 20:45:11 +01:00
|
|
|
#include "taisei.h"
|
|
|
|
|
2017-02-06 07:59:39 +01:00
|
|
|
#include <time.h>
|
2019-03-18 05:41:12 +01:00
|
|
|
|
2017-02-05 21:16:50 +01:00
|
|
|
#include "global.h"
|
2012-08-04 03:30:40 +02:00
|
|
|
#include "random.h"
|
2012-07-27 19:11:45 +02:00
|
|
|
|
2017-12-04 04:26:27 +01:00
|
|
|
static RandomState *tsrand_current;
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint64_t splitmix64(uint64_t *state) {
|
|
|
|
// from http://xoshiro.di.unimi.it/splitmix64.c
|
2012-07-27 19:11:45 +02:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint64_t z = (*state += 0x9e3779b97f4a7c15);
|
|
|
|
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
|
|
|
|
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
|
|
|
|
return z ^ (z >> 31);
|
|
|
|
}
|
2017-12-04 04:26:27 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint64_t makeseed(void) {
|
|
|
|
static uint64_t s;
|
|
|
|
return splitmix64(&s) ^ SDL_GetPerformanceCounter();
|
|
|
|
}
|
2017-12-04 04:26:27 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
static inline uint64_t rotl(uint64_t x, int k) {
|
|
|
|
return (x << k) | (x >> (64 - k));
|
|
|
|
}
|
2017-12-04 04:26:27 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
static uint64_t xoshiro256plus(uint64_t s[4]) {
|
|
|
|
// from http://xoshiro.di.unimi.it/xoshiro256plus.c
|
2012-07-27 19:11:45 +02:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
const uint64_t result_plus = s[0] + s[3];
|
|
|
|
const uint64_t t = s[1] << 17;
|
2017-02-05 21:16:50 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
s[2] ^= s[0];
|
|
|
|
s[3] ^= s[1];
|
|
|
|
s[1] ^= s[2];
|
|
|
|
s[0] ^= s[3];
|
|
|
|
s[2] ^= t;
|
|
|
|
s[3] = rotl(s[3], 45);
|
2012-07-27 19:11:45 +02:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
return result_plus;
|
|
|
|
}
|
2017-12-04 04:26:27 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint32_t tsrand_p(RandomState *rnd) {
|
|
|
|
assert(!rnd->locked);
|
|
|
|
return xoshiro256plus(rnd->state) >> 32;
|
|
|
|
}
|
2017-12-04 04:26:27 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint64_t tsrand64_p(RandomState *rnd) {
|
|
|
|
assert(!rnd->locked);
|
|
|
|
return xoshiro256plus(rnd->state);
|
|
|
|
}
|
2017-12-04 04:26:27 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
void tsrand_seed_p(RandomState *rnd, uint64_t seed) {
|
|
|
|
rnd->state[0] = splitmix64(&seed);
|
|
|
|
rnd->state[1] = splitmix64(&seed);
|
|
|
|
rnd->state[2] = splitmix64(&seed);
|
|
|
|
rnd->state[3] = splitmix64(&seed);
|
2017-12-04 04:26:27 +01:00
|
|
|
}
|
2017-02-05 21:16:50 +01:00
|
|
|
|
2017-12-04 04:26:27 +01:00
|
|
|
void tsrand_switch(RandomState *rnd) {
|
|
|
|
tsrand_current = rnd;
|
|
|
|
}
|
2017-02-05 21:16:50 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
void tsrand_init(RandomState *rnd, uint64_t seed) {
|
2017-12-04 04:26:27 +01:00
|
|
|
memset(rnd, 0, sizeof(RandomState));
|
|
|
|
tsrand_seed_p(rnd, seed);
|
2012-07-27 19:11:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
void tsrand_seed(uint64_t seed) {
|
2012-07-27 19:11:45 +02:00
|
|
|
tsrand_seed_p(tsrand_current, seed);
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:16:50 +01:00
|
|
|
uint32_t tsrand(void) {
|
2012-07-27 19:11:45 +02:00
|
|
|
return tsrand_p(tsrand_current);
|
|
|
|
}
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint64_t tsrand64(void) {
|
|
|
|
return tsrand64_p(tsrand_current);
|
2012-07-27 19:11:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
static inline double makedouble(uint64_t x) {
|
|
|
|
// Range: [0..1)
|
|
|
|
return (x >> 11) * (1.0 / (UINT64_C(1) << 53));
|
|
|
|
}
|
|
|
|
|
|
|
|
double frand(void) {
|
|
|
|
return makedouble(tsrand64());
|
|
|
|
}
|
|
|
|
|
|
|
|
double nfrand(void) {
|
2017-12-04 04:26:27 +01:00
|
|
|
return frand() * 2.0 - 1.0;
|
2012-07-27 20:59:24 +02:00
|
|
|
}
|
|
|
|
|
2012-08-04 02:49:12 +02:00
|
|
|
// we use this to support multiple rands in a single statement without breaking replays across different builds
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
static uint64_t tsrand_array[TSRAND_ARRAY_LIMIT];
|
2012-08-04 03:30:40 +02:00
|
|
|
static int tsrand_array_elems;
|
2017-02-05 21:16:50 +01:00
|
|
|
static uint64_t tsrand_fillflags = 0;
|
|
|
|
|
2018-04-12 16:08:48 +02:00
|
|
|
static void tsrand_error(const char *file, const char *func, uint line, const char *fmt, ...) {
|
2017-02-05 21:16:50 +01:00
|
|
|
char buf[2048] = { 0 };
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2017-03-13 17:03:51 +01:00
|
|
|
log_fatal("%s(): %s [%s:%u]", func, buf, file, line);
|
2017-02-05 21:16:50 +01:00
|
|
|
}
|
|
|
|
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
#define TSRANDERR(...) tsrand_error(file, __func__, line, __VA_ARGS__)
|
2017-02-05 21:16:50 +01:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
void _tsrand_fill_p(RandomState *rnd, int amount, const char *file, uint line) {
|
2017-02-05 21:16:50 +01:00
|
|
|
if(tsrand_fillflags) {
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
TSRANDERR("Some indices left unused from the previous call");
|
2017-02-05 21:16:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-08-04 02:49:12 +02:00
|
|
|
|
2012-08-04 03:30:40 +02:00
|
|
|
tsrand_array_elems = amount;
|
2019-03-09 17:19:42 +01:00
|
|
|
tsrand_fillflags = (UINT64_C(1) << amount) - 1;
|
2017-02-05 21:16:50 +01:00
|
|
|
|
|
|
|
for(int i = 0; i < amount; ++i) {
|
2019-03-09 17:19:42 +01:00
|
|
|
tsrand_array[i] = tsrand64_p(rnd);
|
2017-02-05 21:16:50 +01:00
|
|
|
}
|
2012-08-04 02:49:12 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
void _tsrand_fill(int amount, const char *file, uint line) {
|
|
|
|
_tsrand_fill_p(tsrand_current, amount, file, line);
|
2012-08-04 02:49:12 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint64_t _tsrand64_a(int idx, const char *file, uint line) {
|
2017-02-05 21:16:50 +01:00
|
|
|
if(idx >= tsrand_array_elems || idx < 0) {
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
TSRANDERR("Index out of range (%i / %i)", idx, tsrand_array_elems);
|
2017-02-05 21:16:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
if(tsrand_fillflags & (UINT64_C(1) << idx)) {
|
|
|
|
tsrand_fillflags &= ~(UINT64_C(1) << idx);
|
2017-02-05 21:16:50 +01:00
|
|
|
return tsrand_array[idx];
|
|
|
|
}
|
|
|
|
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
TSRANDERR("Index %i used multiple times", idx);
|
2017-02-05 21:16:50 +01:00
|
|
|
return 0;
|
2012-08-04 02:49:12 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
uint32_t _tsrand_a(int idx, const char *file, uint line) {
|
|
|
|
return _tsrand64_a(idx, file, line) >> 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
double _afrand(int idx, const char *file, uint line) {
|
|
|
|
return makedouble(_tsrand64_a(idx, file, line));
|
2012-08-04 02:49:12 +02:00
|
|
|
}
|
2012-08-04 02:56:51 +02:00
|
|
|
|
2019-03-09 17:19:42 +01:00
|
|
|
double _anfrand(int idx, const char *file, uint line) {
|
|
|
|
return _afrand(idx, file, line) * 2 - 1;
|
2012-08-04 02:56:51 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 21:16:50 +01:00
|
|
|
void tsrand_lock(RandomState *rnd) {
|
2017-02-11 04:52:08 +01:00
|
|
|
rnd->locked = true;
|
2017-02-05 21:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void tsrand_unlock(RandomState *rnd) {
|
2017-02-11 04:52:08 +01:00
|
|
|
rnd->locked = false;
|
2017-02-05 21:16:50 +01:00
|
|
|
}
|