Spooky hash, smarter animqueue diffs
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
CXX=g++ -std=c++17 -Wall -g -Iinc -Icpp
|
||||
|
||||
CPP_FILES=\
|
||||
cpp/spookyv2.cpp\
|
||||
cpp/util.cpp\
|
||||
cpp/luastack.cpp\
|
||||
cpp/traceback.cpp\
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <map>
|
||||
#include "luastack.hpp"
|
||||
#include "animqueue.hpp"
|
||||
#include "streambuffer.hpp"
|
||||
|
||||
AnimStep::AnimStep() {
|
||||
clear();
|
||||
@@ -10,7 +11,6 @@ AnimStep::AnimStep() {
|
||||
AnimStep::~AnimStep() {}
|
||||
|
||||
|
||||
|
||||
void AnimStep::clear() {
|
||||
id_ = 0;
|
||||
bits_ = 0;
|
||||
@@ -252,12 +252,25 @@ bool AnimStep::echoes(const AnimStep &prev) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
AnimQueue::AnimQueue() {
|
||||
AnimQueue::AnimQueue(util::WorldType wt) {
|
||||
world_type_ = wt;
|
||||
size_limit_ = 10; // Default size limit.
|
||||
clear_steps();
|
||||
version_number_ = (wt == util::WORLD_TYPE_MASTER) ? 1 : 0;
|
||||
}
|
||||
|
||||
bool AnimQueue::exactly_equal(const AnimQueue &other) const {
|
||||
void AnimQueue::mutated() {
|
||||
if (world_type_ == util::WORLD_TYPE_MASTER) {
|
||||
version_number_ += 1;
|
||||
} else {
|
||||
version_number_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool AnimQueue::size_and_steps_equal(const AnimQueue &other) const {
|
||||
if (size_limit_ != other.size_limit_) {
|
||||
return false;
|
||||
}
|
||||
if (steps_.size() != other.steps_.size()) {
|
||||
return false;
|
||||
}
|
||||
@@ -277,19 +290,18 @@ void AnimQueue::clear_steps() {
|
||||
init.action_ = "";
|
||||
init.graphic_ = "";
|
||||
init.plane_ = "";
|
||||
mutated();
|
||||
}
|
||||
|
||||
void AnimQueue::set_size_limit(int n) {
|
||||
assert(n >= 2);
|
||||
assert(n >= 1);
|
||||
if (size_limit_ == n) return;
|
||||
size_limit_ = n;
|
||||
}
|
||||
|
||||
void AnimQueue::keep_only(int n) {
|
||||
if (n < 1) n = 1;
|
||||
while (int(steps_.size()) > n) {
|
||||
while (int(steps_.size()) > size_limit_) {
|
||||
steps_.pop_front();
|
||||
}
|
||||
steps_.front().keep_state_only();
|
||||
mutated();
|
||||
}
|
||||
|
||||
void AnimQueue::add(int64_t id, const AnimStep &step) {
|
||||
@@ -297,15 +309,23 @@ void AnimQueue::add(int64_t id, const AnimStep &step) {
|
||||
copy.echo(steps_.back());
|
||||
copy.id_ = id;
|
||||
steps_.push_back(copy);
|
||||
keep_only(size_limit_);
|
||||
while (int(steps_.size()) > size_limit_) {
|
||||
steps_.pop_front();
|
||||
}
|
||||
steps_.front().keep_state_only();
|
||||
mutated();
|
||||
}
|
||||
|
||||
bool AnimQueue::valid() const {
|
||||
// Animqueue must have at least one step, and no more than 255.
|
||||
// Size limit must be between 2 and 250
|
||||
if ((size_limit_ < 1) || (size_limit_ > 250)) {
|
||||
return false;
|
||||
}
|
||||
// Animqueue must have at least one step, and no more than 250.
|
||||
if (steps_.empty()) {
|
||||
return false;
|
||||
}
|
||||
if (steps_.size() > 255) {
|
||||
if (steps_.size() > 250) {
|
||||
return false;
|
||||
}
|
||||
// First action should be blank.
|
||||
@@ -327,16 +347,18 @@ bool AnimQueue::valid() const {
|
||||
|
||||
void AnimQueue::serialize(StreamBuffer *sb) {
|
||||
assert(valid()); // can't serialize an invalid animqueue.
|
||||
sb->write_int32(size_limit_);
|
||||
sb->write_size(steps_.size());
|
||||
sb->write_int64(version_number_);
|
||||
sb->write_uint8(size_limit_);
|
||||
sb->write_uint8(steps_.size());
|
||||
for (const AnimStep &step : steps_) {
|
||||
step.write_into(sb);
|
||||
}
|
||||
}
|
||||
|
||||
void AnimQueue::deserialize(StreamBuffer *sb) {
|
||||
size_limit_ = sb->read_int32();
|
||||
size_t nsteps = sb->read_size();
|
||||
version_number_ = sb->read_int64();
|
||||
size_limit_ = sb->read_uint8();
|
||||
size_t nsteps = sb->read_uint8();
|
||||
steps_.resize(nsteps);
|
||||
for (size_t i = 0; i < nsteps; i++) {
|
||||
AnimStep &step = steps_[i];
|
||||
@@ -349,15 +371,22 @@ bool AnimQueue::make_patch(const AnimQueue &auth, StreamBuffer *sb) const {
|
||||
// Sanity check.
|
||||
assert(valid());
|
||||
assert(auth.valid());
|
||||
|
||||
// Fast path to detect equivalence.
|
||||
if (version_number_ == auth.version_number_) {
|
||||
sb->write_uint8(0);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Special case: if we're already a perfect match, output 0 bytes.
|
||||
if (exactly_equal(auth)) {
|
||||
// Special case: if we're already a perfect match.
|
||||
if (size_and_steps_equal(auth)) {
|
||||
sb->write_uint8(0);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write the first element.
|
||||
sb->write_uint8(auth.steps_.size());
|
||||
sb->write_uint8(auth.size_limit_);
|
||||
const AnimStep &first = auth.steps_[0];
|
||||
int match = 0;
|
||||
while ((match < int(steps_.size())) && (!steps_[match].state_equal(first))) {
|
||||
@@ -401,7 +430,7 @@ void AnimQueue::apply_patch(StreamBuffer *sb) {
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_limit_ = sb->read_uint8();
|
||||
// Decode the diff, stop at eof.
|
||||
std::deque<AnimStep> old = std::move(steps_);
|
||||
steps_.clear();
|
||||
@@ -422,6 +451,12 @@ void AnimQueue::apply_patch(StreamBuffer *sb) {
|
||||
steps_[0].keep_state_only();
|
||||
}
|
||||
}
|
||||
mutated();
|
||||
}
|
||||
|
||||
void AnimQueue::update_version(const AnimQueue &auth) {
|
||||
assert(size_and_steps_equal(auth));
|
||||
version_number_ = auth.version_number_;
|
||||
}
|
||||
|
||||
const AnimStep &AnimQueue::back() const {
|
||||
@@ -431,9 +466,9 @@ const AnimStep &AnimQueue::back() const {
|
||||
LuaDefine(unittests_animqueue, "c") {
|
||||
// Check initial state.
|
||||
AnimStep stp;
|
||||
AnimQueue aq;
|
||||
AnimQueue aq(util::WORLD_TYPE_MASTER);
|
||||
StreamBuffer sb;
|
||||
AnimQueue aqds;
|
||||
AnimQueue aqds(util::WORLD_TYPE_S_SYNC);
|
||||
aq.set_size_limit(3);
|
||||
|
||||
LuaAssert(L, aq.valid());
|
||||
@@ -568,7 +603,7 @@ LuaDefine(unittests_animqueue, "c") {
|
||||
sb.clear();
|
||||
LuaAssert(L, aqds.make_patch(aq, &sb));
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, aqds.exactly_equal(aq));
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
|
||||
// Add another action.
|
||||
stp.clear();
|
||||
@@ -585,21 +620,31 @@ LuaDefine(unittests_animqueue, "c") {
|
||||
// Apply the diffs, 4 extra bytes should remain.
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, sb.write_count() - sb.read_count() == 4);
|
||||
LuaAssert(L, aqds.exactly_equal(aq));
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
|
||||
// Change the queue size limit.
|
||||
aq.set_size_limit(13);
|
||||
|
||||
// Generate diffs, but add 4 extra bytes.
|
||||
sb.clear();
|
||||
LuaAssert(L, !aqds.size_and_steps_equal(aq));
|
||||
LuaAssert(L, aqds.make_patch(aq, &sb));
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
|
||||
// compare again, should be no differences.
|
||||
sb.clear();
|
||||
LuaAssert(L, !aqds.make_patch(aq, &sb));
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, aqds.exactly_equal(aq));
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
|
||||
// Discard all but the last action.
|
||||
aq.keep_only(1);
|
||||
aq.set_size_limit(1);
|
||||
|
||||
sb.clear();
|
||||
LuaAssert(L, aqds.make_patch(aq, &sb));
|
||||
aqds.apply_patch(&sb);
|
||||
LuaAssert(L, aqds.exactly_equal(aq));
|
||||
LuaAssert(L, aqds.size_and_steps_equal(aq));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,27 @@
|
||||
// set_plane, or an other setter. These setters are meant to only be used
|
||||
// immediately after calling 'add' to populate the new step.
|
||||
//
|
||||
// VERSION NUMBERS
|
||||
//
|
||||
// The version number field: if the version number in the synchronous model is
|
||||
// equal to the version number in the master model, then the two animqueues are
|
||||
// guaranteed to be equal. Here's how we achieve that invariant:
|
||||
//
|
||||
// * In the master model, the version number starts at 1 and is incremented
|
||||
// every time the animation queue is mutated.
|
||||
//
|
||||
// * In the synchronous model, the version number is set to zero every time the
|
||||
// animation queue is mutated. Note that master version numbers are never
|
||||
// zero. Applying a patch also sets the version number to zero.
|
||||
//
|
||||
// * Serializing and deserializing causes the version number to be saved and
|
||||
// restored in both master and synchronous models.
|
||||
//
|
||||
// * The routine 'update_version' should be called after difference
|
||||
// transmission. This copies the version number from the master to the
|
||||
// synchronous model. This is guaranteed to be safe because we just finished
|
||||
// difference transmission, therefore, the animatio queues should match.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef ANIMQUEUE_HPP
|
||||
@@ -138,25 +159,28 @@ public:
|
||||
|
||||
class AnimQueue {
|
||||
private:
|
||||
util::WorldType world_type_;
|
||||
int32_t size_limit_;
|
||||
std::deque<AnimStep> steps_;
|
||||
int64_t version_number_;
|
||||
|
||||
// Called whenever the animation queue is mutated.
|
||||
// serialization and deserialization
|
||||
void mutated();
|
||||
public:
|
||||
AnimQueue();
|
||||
AnimQueue(util::WorldType wt);
|
||||
|
||||
bool exactly_equal(const AnimQueue &aq) const;
|
||||
|
||||
// Simple getters.
|
||||
const AnimStep &nth(int n) const { return steps_[n]; }
|
||||
size_t size() const { return steps_.size(); }
|
||||
int32_t size_limit() const { return size_limit_; }
|
||||
|
||||
// Discard all but the most recent N steps.
|
||||
void keep_only(int n);
|
||||
// Return true if the size limit and steps are identical.
|
||||
bool size_and_steps_equal(const AnimQueue &aq) const;
|
||||
|
||||
// Clear the steps.
|
||||
// Clear the steps. Doesn't affect size_limit.
|
||||
// The resulting steps aren't empty. There will be one
|
||||
// step in the queue, which will be on the plane "", at (0,0,0).
|
||||
// Doesn't affect size_limit.
|
||||
void clear_steps();
|
||||
|
||||
// Mutator to create a new step.
|
||||
@@ -167,13 +191,10 @@ public:
|
||||
void deserialize(StreamBuffer *sb);
|
||||
|
||||
// Difference transmission
|
||||
//
|
||||
// When there are no differences, make_patch returns false but
|
||||
// still writes out a valid patch.
|
||||
//
|
||||
bool make_patch(const AnimQueue &auth, StreamBuffer *sb) const;
|
||||
void apply_patch(StreamBuffer *sb);
|
||||
|
||||
void update_version(const AnimQueue &auth);
|
||||
|
||||
// Get the final resting place after all animations are complete.
|
||||
const AnimStep &back() const;
|
||||
|
||||
|
||||
@@ -19,24 +19,21 @@ IdGlobalPool::IdGlobalPool() {
|
||||
salvaged_.clear();
|
||||
next_batch_ = 0;
|
||||
next_id_ = 0;
|
||||
queue_fill_ = 10;
|
||||
}
|
||||
|
||||
IdGlobalPool::~IdGlobalPool() {
|
||||
}
|
||||
|
||||
void IdGlobalPool::init_master(int qf) {
|
||||
void IdGlobalPool::init_master() {
|
||||
salvaged_.clear();
|
||||
next_batch_ = 0x0001000000000000;
|
||||
next_id_ = 0x0010000000000000;
|
||||
queue_fill_ = qf;
|
||||
}
|
||||
|
||||
void IdGlobalPool::init_synch(int qf) {
|
||||
void IdGlobalPool::init_synch() {
|
||||
salvaged_.clear();
|
||||
next_batch_ = 0;
|
||||
next_id_ = 0x001E000000000000;
|
||||
queue_fill_ = qf;
|
||||
}
|
||||
|
||||
int64_t IdGlobalPool::get_one() {
|
||||
@@ -87,7 +84,6 @@ int64_t IdGlobalPool::alloc_id_for_thread(lua_State *L) {
|
||||
void IdGlobalPool::serialize(StreamBuffer *sb) {
|
||||
sb->write_int64(next_batch_);
|
||||
sb->write_int64(next_id_);
|
||||
sb->write_int32(queue_fill_);
|
||||
sb->write_size(salvaged_.size());
|
||||
for (int64_t batch : salvaged_) {
|
||||
sb->write_int64(batch);
|
||||
@@ -97,7 +93,6 @@ void IdGlobalPool::serialize(StreamBuffer *sb) {
|
||||
void IdGlobalPool::deserialize(StreamBuffer *sb) {
|
||||
next_batch_ = sb->read_int64();
|
||||
next_id_ = sb->read_int64();
|
||||
queue_fill_ = sb->read_int32();
|
||||
size_t salvaged_size = sb->read_size();
|
||||
salvaged_.resize(salvaged_size);
|
||||
for (int i=0; i < int(salvaged_size); i++) {
|
||||
@@ -107,19 +102,27 @@ void IdGlobalPool::deserialize(StreamBuffer *sb) {
|
||||
|
||||
IdPlayerPool::IdPlayerPool(IdGlobalPool *g) {
|
||||
global_ = g;
|
||||
fifo_enabled_ = false;
|
||||
fifo_capacity_ = 0;
|
||||
}
|
||||
|
||||
IdPlayerPool::~IdPlayerPool() {
|
||||
}
|
||||
|
||||
void IdPlayerPool::enable_fifo() {
|
||||
fifo_enabled_ = true;
|
||||
void IdPlayerPool::set_fifo_capacity(int n) {
|
||||
assert((n >= 0) && (n <= 250));
|
||||
fifo_capacity_ = n;
|
||||
while (int(ranges_.size()) > n) {
|
||||
global_->salvage(ranges_.back());
|
||||
ranges_.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
void IdPlayerPool::disable_fifo() {
|
||||
unqueue();
|
||||
fifo_enabled_ = false;
|
||||
void IdPlayerPool::refill() {
|
||||
while (int(ranges_.size()) < fifo_capacity_) {
|
||||
int64_t batch = global_->get_batch();
|
||||
if (batch == 0) break;
|
||||
ranges_.push_back(batch);
|
||||
}
|
||||
}
|
||||
|
||||
void IdPlayerPool::test_push_back(int64_t range) {
|
||||
@@ -134,26 +137,8 @@ void IdPlayerPool::test_clear_ranges() {
|
||||
ranges_.clear();
|
||||
}
|
||||
|
||||
void IdPlayerPool::refill() {
|
||||
if (fifo_enabled_) {
|
||||
while (int(ranges_.size()) < global_->queue_fill()) {
|
||||
int64_t batch = global_->get_batch();
|
||||
if (batch == 0) break;
|
||||
ranges_.push_back(batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IdPlayerPool::unqueue() {
|
||||
while (!ranges_.empty()) {
|
||||
global_->salvage(ranges_.front());
|
||||
ranges_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
int64_t IdPlayerPool::get_batch() {
|
||||
int fill = fifo_enabled_ ? global_->queue_fill() : 0;
|
||||
while (int(ranges_.size()) < fill + 1) {
|
||||
while (int(ranges_.size()) < fifo_capacity_ + 1) {
|
||||
int64_t batch = global_->get_batch();
|
||||
if (batch == 0) break;
|
||||
ranges_.push_back(batch);
|
||||
@@ -177,24 +162,24 @@ void IdPlayerPool::prepare_thread(lua_State *L) {
|
||||
}
|
||||
|
||||
void IdPlayerPool::serialize(StreamBuffer *sb) {
|
||||
sb->write_bool(fifo_enabled_);
|
||||
sb->write_size(ranges_.size());
|
||||
sb->write_uint8(fifo_capacity_);
|
||||
sb->write_uint8(ranges_.size());
|
||||
for (int64_t batch : ranges_) {
|
||||
sb->write_int64(batch);
|
||||
}
|
||||
}
|
||||
|
||||
void IdPlayerPool::deserialize(StreamBuffer *sb) {
|
||||
fifo_enabled_ = sb->read_bool();
|
||||
size_t ranges_size = sb->read_size();
|
||||
fifo_capacity_ = sb->read_uint8();
|
||||
int ranges_size = sb->read_uint8();
|
||||
ranges_.resize(ranges_size);
|
||||
for (int i=0; i < int(ranges_size); i++) {
|
||||
for (int i=0; i < ranges_size; i++) {
|
||||
ranges_[i] = sb->read_int64();
|
||||
}
|
||||
}
|
||||
|
||||
bool IdPlayerPool::exactly_equal(const IdPlayerPool &other) const {
|
||||
if (fifo_enabled_ != other.fifo_enabled_) return false;
|
||||
if (fifo_capacity_ != other.fifo_capacity_) return false;
|
||||
if (ranges_.size() != other.ranges_.size()) return false;
|
||||
for (int i = 0; i < int(ranges_.size()); i++) {
|
||||
if (ranges_[i] != other.ranges_[i]) {
|
||||
@@ -205,32 +190,25 @@ bool IdPlayerPool::exactly_equal(const IdPlayerPool &other) const {
|
||||
}
|
||||
|
||||
bool IdPlayerPool::valid() const {
|
||||
if ((!fifo_enabled_) && (ranges_.size() > 0)) return false;
|
||||
if (ranges_.size() > 250) return false;
|
||||
if ((fifo_capacity_ < 0) || (fifo_capacity_ > 250)) return false;
|
||||
if (int(ranges_.size()) > fifo_capacity_) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IdPlayerPool::make_patch(const IdPlayerPool &auth, StreamBuffer *sb) const {
|
||||
assert(valid());
|
||||
assert(auth.valid());
|
||||
assert(global_->queue_fill() == auth.global_->queue_fill());
|
||||
|
||||
// The first byte.
|
||||
// 0 no differences
|
||||
// 1 fifo disabled and ranges empty
|
||||
// 2+ fifo enabled and N-2 ranges.
|
||||
//
|
||||
// The fifo capacity cannot be 255, so we use this as special
|
||||
// to indicate that no changes are present.
|
||||
if (exactly_equal(auth)) {
|
||||
sb->write_uint8(0);
|
||||
sb->write_uint8(255);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!auth.fifo_enabled_) {
|
||||
sb->write_uint8(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
sb->write_uint8(auth.ranges_.size() + 2);
|
||||
// Write the fifo capacity and nranges
|
||||
sb->write_uint8(auth.fifo_capacity_);
|
||||
sb->write_uint8(auth.ranges_.size());
|
||||
|
||||
// Build up an index of the known IDs.
|
||||
std::map<int64_t, int> index;
|
||||
@@ -238,7 +216,6 @@ bool IdPlayerPool::make_patch(const IdPlayerPool &auth, StreamBuffer *sb) const
|
||||
index[ranges_[i]] = i;
|
||||
}
|
||||
|
||||
|
||||
// Write the ranges, but encode known IDs in one byte.
|
||||
for (int i = 0; i < int(auth.ranges_.size()); i++) {
|
||||
int64_t n = auth.ranges_[i];
|
||||
@@ -256,20 +233,12 @@ bool IdPlayerPool::make_patch(const IdPlayerPool &auth, StreamBuffer *sb) const
|
||||
|
||||
void IdPlayerPool::apply_patch(StreamBuffer *sb) {
|
||||
// read the header byte
|
||||
int fifo_cap = sb->read_uint8();
|
||||
if (fifo_cap == 255) {
|
||||
return;
|
||||
}
|
||||
fifo_capacity_ = fifo_cap;
|
||||
int nranges = sb->read_uint8();
|
||||
if (nranges == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (nranges == 1) {
|
||||
fifo_enabled_ = false;
|
||||
ranges_.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
fifo_enabled_ = true;
|
||||
nranges -= 2;
|
||||
|
||||
std::deque<int64_t> old = std::move(ranges_);
|
||||
ranges_.clear();
|
||||
for (int i = 0; i < nranges; i++) {
|
||||
@@ -291,26 +260,26 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
StreamBuffer sb;
|
||||
|
||||
// Synchronous pools produce IDs starting at 0x001E000000000000
|
||||
gp.init_synch(3);
|
||||
gp.init_synch();
|
||||
LuaAssert(L, gp.get_one() == 0x001E000000000000);
|
||||
LuaAssert(L, gp.get_one() == 0x001E000000000001);
|
||||
LuaAssert(L, gp.get_one() == 0x001E000000000002);
|
||||
|
||||
// Master pools produce IDs starting at 0x0010000000000000
|
||||
gp.init_master(3);
|
||||
gp.init_master();
|
||||
LuaAssert(L, gp.get_one() == 0x0010000000000000);
|
||||
LuaAssert(L, gp.get_one() == 0x0010000000000001);
|
||||
LuaAssert(L, gp.get_one() == 0x0010000000000002);
|
||||
|
||||
// Synchronous pools produce only null batches.
|
||||
gp.init_synch(3);
|
||||
gp.init_synch();
|
||||
LuaAssert(L, gp.get_batch() == 0);
|
||||
LuaAssert(L, gp.get_batch() == 0);
|
||||
gp.salvage(nthbatch(5));
|
||||
LuaAssert(L, gp.get_batch() == 0);
|
||||
|
||||
// Simple fetch batches with a few salvages.
|
||||
gp.init_master(3);
|
||||
gp.init_master();
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(2));
|
||||
@@ -321,14 +290,14 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(3));
|
||||
|
||||
// Salvage of a zero-batch does nothing.
|
||||
gp.init_master(3);
|
||||
gp.init_master();
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
gp.salvage(0);
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(2));
|
||||
|
||||
// Salvage of a partial batch.
|
||||
gp.init_master(3);
|
||||
gp.init_master();
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
gp.salvage(nthbatch(142) + 10);
|
||||
@@ -336,7 +305,7 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(2));
|
||||
|
||||
// Salvage of a half-empty batch does nothing.
|
||||
gp.init_master(3);
|
||||
gp.init_master();
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
gp.salvage(nthbatch(142) + 145);
|
||||
@@ -344,8 +313,8 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
|
||||
// In the synchronous model, refill should do nothing.
|
||||
pp.test_clear_ranges();
|
||||
pp.enable_fifo();
|
||||
gp.init_synch(3);
|
||||
gp.init_synch();
|
||||
pp.set_fifo_capacity(3);
|
||||
pp.refill();
|
||||
LuaAssert(L, pp.size() == 0);
|
||||
LuaAssert(L, pp.get_batch() == 0);
|
||||
@@ -354,9 +323,9 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
|
||||
// In the master model, with fifo disabled. Fifo should remain
|
||||
// empty, but batches should be returned.
|
||||
gp.init_master();
|
||||
pp.test_clear_ranges();
|
||||
pp.disable_fifo();
|
||||
gp.init_master(3);
|
||||
pp.set_fifo_capacity(0);
|
||||
pp.refill();
|
||||
LuaAssert(L, pp.size() == 0);
|
||||
LuaAssert(L, pp.get_batch() == nthbatch(0));
|
||||
@@ -364,9 +333,9 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
LuaAssert(L, pp.get_batch() == nthbatch(1));
|
||||
|
||||
// Test refill from master (with enabled fifo).
|
||||
gp.init_master();
|
||||
pp.test_clear_ranges();
|
||||
pp.enable_fifo();
|
||||
gp.init_master(3);
|
||||
pp.set_fifo_capacity(3);
|
||||
pp.refill();
|
||||
LuaAssert(L, ranges_equal(pp.ranges_, nthbatch(0), nthbatch(1), nthbatch(2)));
|
||||
|
||||
@@ -377,16 +346,16 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
// Test unqueueing the batches.
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(4));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(5));
|
||||
pp.unqueue();
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(3));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(2));
|
||||
pp.set_fifo_capacity(0);
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(2));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(3));
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(6));
|
||||
|
||||
// Try preparing a thread and salvaging a thread.
|
||||
gp.init_master();
|
||||
pp.test_clear_ranges();
|
||||
pp.enable_fifo();
|
||||
gp.init_master(3);
|
||||
pp.set_fifo_capacity(3);
|
||||
lua_setnextid(L, 0);
|
||||
pp.prepare_thread(L);
|
||||
LuaAssert(L, lua_getnextid(L) == nthbatch(0));
|
||||
@@ -400,8 +369,8 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(1));
|
||||
|
||||
// Allocate IDs from inside a thread.
|
||||
gp.init_master();
|
||||
lua_setnextid(L, 0xFD);
|
||||
gp.init_master(3);
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFD);
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFE);
|
||||
LuaAssert(L, gp.alloc_id_for_thread(L) == 0xFF);
|
||||
@@ -409,45 +378,42 @@ LuaDefine(unittests_idalloc, "c") {
|
||||
LuaAssert(L, lua_getnextid(L) == 0);
|
||||
|
||||
// Serialize and deserialize a global pool.
|
||||
gp.init_master(3);
|
||||
gpds.init_master(10);
|
||||
gp.init_master();
|
||||
gpds.init_master();
|
||||
LuaAssert(L, gp.get_one() == 0x0010000000000000);
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
gp.salvage(nthbatch(182));
|
||||
gp.salvage(nthbatch(183));
|
||||
gp.serialize(&sb);
|
||||
gpds.deserialize(&sb);
|
||||
LuaAssert(L, gpds.queue_fill() == 3);
|
||||
LuaAssert(L, gpds.get_one() == 0x0010000000000001);
|
||||
LuaAssert(L, gpds.get_batch() == nthbatch(183));
|
||||
LuaAssert(L, gpds.get_batch() == nthbatch(182));
|
||||
LuaAssert(L, gpds.get_batch() == nthbatch(1));
|
||||
|
||||
// Serialize and deserialize a player pool.
|
||||
gp.init_master(3);
|
||||
gpds.init_synch(5);
|
||||
gp.init_master();
|
||||
gpds.init_synch();
|
||||
LuaAssert(L, gp.get_batch() == nthbatch(0));
|
||||
pp.test_clear_ranges();
|
||||
pp.enable_fifo();
|
||||
pp.set_fifo_capacity(3);
|
||||
pp.refill();
|
||||
LuaAssert(L, pp.fifo_enabled());
|
||||
LuaAssert(L, pp.size() == 3);
|
||||
ppds.test_clear_ranges();
|
||||
pp.serialize(&sb);
|
||||
ppds.deserialize(&sb);
|
||||
LuaAssert(L, ppds.fifo_enabled());
|
||||
LuaAssert(L, ppds.get_fifo_capacity()==3);
|
||||
LuaAssert(L, ppds.size() == 3);
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(1));
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(2));
|
||||
LuaAssert(L, ppds.get_batch() == nthbatch(3));
|
||||
|
||||
// Difference transmit compare two empty pools.
|
||||
gp.init_master(3);
|
||||
gpds.init_master(3);
|
||||
gp.init_master();
|
||||
gpds.init_master();
|
||||
pp.test_clear_ranges();
|
||||
ppds.test_clear_ranges();
|
||||
pp.enable_fifo();
|
||||
ppds.enable_fifo();
|
||||
pp.set_fifo_capacity(3);
|
||||
ppds.set_fifo_capacity(3);
|
||||
|
||||
// Check case: no differences.
|
||||
sb.clear();
|
||||
|
||||
@@ -76,7 +76,6 @@ private:
|
||||
std::vector<int64_t> salvaged_;
|
||||
int64_t next_batch_;
|
||||
int64_t next_id_;
|
||||
int32_t queue_fill_;
|
||||
friend int unittests_idalloc(lua_State *L);
|
||||
|
||||
public:
|
||||
@@ -89,10 +88,10 @@ public:
|
||||
~IdGlobalPool();
|
||||
|
||||
// Initialize the pool for use in a master model.
|
||||
void init_master(int qf);
|
||||
void init_master();
|
||||
|
||||
// Initialize the pool for use in a synchronous model.
|
||||
void init_synch(int qf);
|
||||
void init_synch();
|
||||
|
||||
// Create a single unique ID. Ids allocated this way are
|
||||
// unlikely to be predicted correctly.
|
||||
@@ -103,10 +102,6 @@ public:
|
||||
// In a synchronous model, the batch is always zero (invalid).
|
||||
int64_t get_batch();
|
||||
|
||||
// When a player pool refills its fifo queue, it refills it
|
||||
// with this many batches.
|
||||
int queue_fill() const { return queue_fill_; }
|
||||
|
||||
// Try to return the specified batch to the global pool.
|
||||
// The salvage operation quietly does nothing if the batch is
|
||||
// zero, or the batch contains fewer than 128 IDs.
|
||||
@@ -128,7 +123,7 @@ public:
|
||||
class IdPlayerPool {
|
||||
private:
|
||||
IdGlobalPool *global_;
|
||||
bool fifo_enabled_;
|
||||
int fifo_capacity_;
|
||||
std::deque<int64_t> ranges_;
|
||||
friend int unittests_idalloc(lua_State *L);
|
||||
|
||||
@@ -142,10 +137,9 @@ public:
|
||||
IdPlayerPool(IdGlobalPool *g);
|
||||
~IdPlayerPool();
|
||||
|
||||
// Enable or disable the fifo.
|
||||
void enable_fifo();
|
||||
void disable_fifo();
|
||||
bool fifo_enabled() { return fifo_enabled_; }
|
||||
// Set the fifo capacity. Max=255.
|
||||
void set_fifo_capacity(int n);
|
||||
int get_fifo_capacity() const { return fifo_capacity_; }
|
||||
|
||||
// Return all batches from the fifo to the global pool.
|
||||
void unqueue();
|
||||
|
||||
355
luprex/core/cpp/spookyv2.cpp
Normal file
355
luprex/core/cpp/spookyv2.cpp
Normal file
@@ -0,0 +1,355 @@
|
||||
// Spooky Hash
|
||||
// A 128-bit noncryptographic hash, for checksums and table lookup
|
||||
// By Bob Jenkins. Public domain.
|
||||
// Oct 31 2010: published framework, disclaimer ShortHash isn't right
|
||||
// Nov 7 2010: disabled ShortHash
|
||||
// Oct 31 2011: replace End, ShortMix, ShortEnd, enable ShortHash again
|
||||
// April 10 2012: buffer overflow on platforms without unaligned reads
|
||||
// July 12 2012: was passing out variables in final to in/out in short
|
||||
// July 30 2012: I reintroduced the buffer overflow
|
||||
// August 5 2012: SpookyV2: d = should be d += in short hash, and remove extra mix from long hash
|
||||
|
||||
#include <memory.h>
|
||||
#include "spookyv2.hpp"
|
||||
|
||||
#define ALLOW_UNALIGNED_READS 1
|
||||
|
||||
#define INLINE __forceinline
|
||||
|
||||
// number of uint64_t's in internal state
|
||||
static const size_t sc_numVars = 12;
|
||||
|
||||
// size of the internal state
|
||||
static const size_t sc_blockSize = sc_numVars*8;
|
||||
|
||||
// size of buffer of unhashed data, in bytes
|
||||
static const size_t sc_bufSize = 2*sc_blockSize;
|
||||
|
||||
//
|
||||
// sc_const: a constant which:
|
||||
// * is not zero
|
||||
// * is odd
|
||||
// * is a not-very-regular mix of 1's and 0's
|
||||
// * does not need any other special mathematical properties
|
||||
//
|
||||
static const uint64_t sc_const = 0xdeadbeefdeadbeefLL;
|
||||
|
||||
//
|
||||
// left rotate a 64-bit value by k bytes
|
||||
//
|
||||
static INLINE uint64_t Rot64(uint64_t x, int k)
|
||||
{
|
||||
return (x << k) | (x >> (64 - k));
|
||||
}
|
||||
|
||||
//
|
||||
// This is used if the input is 96 bytes long or longer.
|
||||
//
|
||||
// The internal state is fully overwritten every 96 bytes.
|
||||
// Every input bit appears to cause at least 128 bits of entropy
|
||||
// before 96 other bytes are combined, when run forward or backward
|
||||
// For every input bit,
|
||||
// Two inputs differing in just that input bit
|
||||
// Where "differ" means xor or subtraction
|
||||
// And the base value is random
|
||||
// When run forward or backwards one Mix
|
||||
// I tried 3 pairs of each; they all differed by at least 212 bits.
|
||||
//
|
||||
static INLINE void Mix(
|
||||
const uint64_t *data,
|
||||
uint64_t &s0, uint64_t &s1, uint64_t &s2, uint64_t &s3,
|
||||
uint64_t &s4, uint64_t &s5, uint64_t &s6, uint64_t &s7,
|
||||
uint64_t &s8, uint64_t &s9, uint64_t &s10,uint64_t &s11)
|
||||
{
|
||||
s0 += data[0]; s2 ^= s10; s11 ^= s0; s0 = Rot64(s0,11); s11 += s1;
|
||||
s1 += data[1]; s3 ^= s11; s0 ^= s1; s1 = Rot64(s1,32); s0 += s2;
|
||||
s2 += data[2]; s4 ^= s0; s1 ^= s2; s2 = Rot64(s2,43); s1 += s3;
|
||||
s3 += data[3]; s5 ^= s1; s2 ^= s3; s3 = Rot64(s3,31); s2 += s4;
|
||||
s4 += data[4]; s6 ^= s2; s3 ^= s4; s4 = Rot64(s4,17); s3 += s5;
|
||||
s5 += data[5]; s7 ^= s3; s4 ^= s5; s5 = Rot64(s5,28); s4 += s6;
|
||||
s6 += data[6]; s8 ^= s4; s5 ^= s6; s6 = Rot64(s6,39); s5 += s7;
|
||||
s7 += data[7]; s9 ^= s5; s6 ^= s7; s7 = Rot64(s7,57); s6 += s8;
|
||||
s8 += data[8]; s10 ^= s6; s7 ^= s8; s8 = Rot64(s8,55); s7 += s9;
|
||||
s9 += data[9]; s11 ^= s7; s8 ^= s9; s9 = Rot64(s9,54); s8 += s10;
|
||||
s10 += data[10]; s0 ^= s8; s9 ^= s10; s10 = Rot64(s10,22); s9 += s11;
|
||||
s11 += data[11]; s1 ^= s9; s10 ^= s11; s11 = Rot64(s11,46); s10 += s0;
|
||||
}
|
||||
|
||||
//
|
||||
// Mix all 12 inputs together so that h0, h1 are a hash of them all.
|
||||
//
|
||||
// For two inputs differing in just the input bits
|
||||
// Where "differ" means xor or subtraction
|
||||
// And the base value is random, or a counting value starting at that bit
|
||||
// The final result will have each bit of h0, h1 flip
|
||||
// For every input bit,
|
||||
// with probability 50 +- .3%
|
||||
// For every pair of input bits,
|
||||
// with probability 50 +- 3%
|
||||
//
|
||||
// This does not rely on the last Mix() call having already mixed some.
|
||||
// Two iterations was almost good enough for a 64-bit result, but a
|
||||
// 128-bit result is reported, so End() does three iterations.
|
||||
//
|
||||
static INLINE void EndPartial(
|
||||
uint64_t &h0, uint64_t &h1, uint64_t &h2, uint64_t &h3,
|
||||
uint64_t &h4, uint64_t &h5, uint64_t &h6, uint64_t &h7,
|
||||
uint64_t &h8, uint64_t &h9, uint64_t &h10,uint64_t &h11)
|
||||
{
|
||||
h11+= h1; h2 ^= h11; h1 = Rot64(h1,44);
|
||||
h0 += h2; h3 ^= h0; h2 = Rot64(h2,15);
|
||||
h1 += h3; h4 ^= h1; h3 = Rot64(h3,34);
|
||||
h2 += h4; h5 ^= h2; h4 = Rot64(h4,21);
|
||||
h3 += h5; h6 ^= h3; h5 = Rot64(h5,38);
|
||||
h4 += h6; h7 ^= h4; h6 = Rot64(h6,33);
|
||||
h5 += h7; h8 ^= h5; h7 = Rot64(h7,10);
|
||||
h6 += h8; h9 ^= h6; h8 = Rot64(h8,13);
|
||||
h7 += h9; h10^= h7; h9 = Rot64(h9,38);
|
||||
h8 += h10; h11^= h8; h10= Rot64(h10,53);
|
||||
h9 += h11; h0 ^= h9; h11= Rot64(h11,42);
|
||||
h10+= h0; h1 ^= h10; h0 = Rot64(h0,54);
|
||||
}
|
||||
|
||||
static INLINE void End(
|
||||
const uint64_t *data,
|
||||
uint64_t &h0, uint64_t &h1, uint64_t &h2, uint64_t &h3,
|
||||
uint64_t &h4, uint64_t &h5, uint64_t &h6, uint64_t &h7,
|
||||
uint64_t &h8, uint64_t &h9, uint64_t &h10,uint64_t &h11)
|
||||
{
|
||||
h0 += data[0]; h1 += data[1]; h2 += data[2]; h3 += data[3];
|
||||
h4 += data[4]; h5 += data[5]; h6 += data[6]; h7 += data[7];
|
||||
h8 += data[8]; h9 += data[9]; h10 += data[10]; h11 += data[11];
|
||||
EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
|
||||
EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
|
||||
EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
|
||||
}
|
||||
|
||||
//
|
||||
// The goal is for each bit of the input to expand into 128 bits of
|
||||
// apparent entropy before it is fully overwritten.
|
||||
// n trials both set and cleared at least m bits of h0 h1 h2 h3
|
||||
// n: 2 m: 29
|
||||
// n: 3 m: 46
|
||||
// n: 4 m: 57
|
||||
// n: 5 m: 107
|
||||
// n: 6 m: 146
|
||||
// n: 7 m: 152
|
||||
// when run forwards or backwards
|
||||
// for all 1-bit and 2-bit diffs
|
||||
// with diffs defined by either xor or subtraction
|
||||
// with a base of all zeros plus a counter, or plus another bit, or random
|
||||
//
|
||||
static INLINE void ShortMix(uint64_t &h0, uint64_t &h1, uint64_t &h2, uint64_t &h3)
|
||||
{
|
||||
h2 = Rot64(h2,50); h2 += h3; h0 ^= h2;
|
||||
h3 = Rot64(h3,52); h3 += h0; h1 ^= h3;
|
||||
h0 = Rot64(h0,30); h0 += h1; h2 ^= h0;
|
||||
h1 = Rot64(h1,41); h1 += h2; h3 ^= h1;
|
||||
h2 = Rot64(h2,54); h2 += h3; h0 ^= h2;
|
||||
h3 = Rot64(h3,48); h3 += h0; h1 ^= h3;
|
||||
h0 = Rot64(h0,38); h0 += h1; h2 ^= h0;
|
||||
h1 = Rot64(h1,37); h1 += h2; h3 ^= h1;
|
||||
h2 = Rot64(h2,62); h2 += h3; h0 ^= h2;
|
||||
h3 = Rot64(h3,34); h3 += h0; h1 ^= h3;
|
||||
h0 = Rot64(h0,5); h0 += h1; h2 ^= h0;
|
||||
h1 = Rot64(h1,36); h1 += h2; h3 ^= h1;
|
||||
}
|
||||
|
||||
//
|
||||
// Mix all 4 inputs together so that h0, h1 are a hash of them all.
|
||||
//
|
||||
// For two inputs differing in just the input bits
|
||||
// Where "differ" means xor or subtraction
|
||||
// And the base value is random, or a counting value starting at that bit
|
||||
// The final result will have each bit of h0, h1 flip
|
||||
// For every input bit,
|
||||
// with probability 50 +- .3% (it is probably better than that)
|
||||
// For every pair of input bits,
|
||||
// with probability 50 +- .75% (the worst case is approximately that)
|
||||
//
|
||||
static INLINE void ShortEnd(uint64_t &h0, uint64_t &h1, uint64_t &h2, uint64_t &h3)
|
||||
{
|
||||
h3 ^= h2; h2 = Rot64(h2,15); h3 += h2;
|
||||
h0 ^= h3; h3 = Rot64(h3,52); h0 += h3;
|
||||
h1 ^= h0; h0 = Rot64(h0,26); h1 += h0;
|
||||
h2 ^= h1; h1 = Rot64(h1,51); h2 += h1;
|
||||
h3 ^= h2; h2 = Rot64(h2,28); h3 += h2;
|
||||
h0 ^= h3; h3 = Rot64(h3,9); h0 += h3;
|
||||
h1 ^= h0; h0 = Rot64(h0,47); h1 += h0;
|
||||
h2 ^= h1; h1 = Rot64(h1,54); h2 += h1;
|
||||
h3 ^= h2; h2 = Rot64(h2,32); h3 += h2;
|
||||
h0 ^= h3; h3 = Rot64(h3,25); h0 += h3;
|
||||
h1 ^= h0; h0 = Rot64(h0,63); h1 += h0;
|
||||
}
|
||||
|
||||
//
|
||||
// short hash ... it could be used on any message,
|
||||
// but it's used by Spooky just for short messages.
|
||||
//
|
||||
static void Short(
|
||||
const void *message,
|
||||
size_t length,
|
||||
uint64_t *hash1,
|
||||
uint64_t *hash2)
|
||||
{
|
||||
uint64_t buf[2*sc_numVars];
|
||||
union
|
||||
{
|
||||
const uint8_t *p8;
|
||||
uint32_t *p32;
|
||||
uint64_t *p64;
|
||||
size_t i;
|
||||
} u;
|
||||
|
||||
u.p8 = (const uint8_t *)message;
|
||||
|
||||
if (!ALLOW_UNALIGNED_READS && (u.i & 0x7))
|
||||
{
|
||||
memcpy(buf, message, length);
|
||||
u.p64 = buf;
|
||||
}
|
||||
|
||||
size_t remainder = length%32;
|
||||
uint64_t a=*hash1;
|
||||
uint64_t b=*hash2;
|
||||
uint64_t c=sc_const;
|
||||
uint64_t d=sc_const;
|
||||
|
||||
if (length > 15)
|
||||
{
|
||||
const uint64_t *end = u.p64 + (length/32)*4;
|
||||
|
||||
// handle all complete sets of 32 bytes
|
||||
for (; u.p64 < end; u.p64 += 4)
|
||||
{
|
||||
c += u.p64[0];
|
||||
d += u.p64[1];
|
||||
ShortMix(a,b,c,d);
|
||||
a += u.p64[2];
|
||||
b += u.p64[3];
|
||||
}
|
||||
|
||||
//Handle the case of 16+ remaining bytes.
|
||||
if (remainder >= 16)
|
||||
{
|
||||
c += u.p64[0];
|
||||
d += u.p64[1];
|
||||
ShortMix(a,b,c,d);
|
||||
u.p64 += 2;
|
||||
remainder -= 16;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the last 0..15 bytes, and its length
|
||||
d += ((uint64_t)length) << 56;
|
||||
switch (remainder)
|
||||
{
|
||||
case 15:
|
||||
d += ((uint64_t)u.p8[14]) << 48;
|
||||
case 14:
|
||||
d += ((uint64_t)u.p8[13]) << 40;
|
||||
case 13:
|
||||
d += ((uint64_t)u.p8[12]) << 32;
|
||||
case 12:
|
||||
d += u.p32[2];
|
||||
c += u.p64[0];
|
||||
break;
|
||||
case 11:
|
||||
d += ((uint64_t)u.p8[10]) << 16;
|
||||
case 10:
|
||||
d += ((uint64_t)u.p8[9]) << 8;
|
||||
case 9:
|
||||
d += (uint64_t)u.p8[8];
|
||||
case 8:
|
||||
c += u.p64[0];
|
||||
break;
|
||||
case 7:
|
||||
c += ((uint64_t)u.p8[6]) << 48;
|
||||
case 6:
|
||||
c += ((uint64_t)u.p8[5]) << 40;
|
||||
case 5:
|
||||
c += ((uint64_t)u.p8[4]) << 32;
|
||||
case 4:
|
||||
c += u.p32[0];
|
||||
break;
|
||||
case 3:
|
||||
c += ((uint64_t)u.p8[2]) << 16;
|
||||
case 2:
|
||||
c += ((uint64_t)u.p8[1]) << 8;
|
||||
case 1:
|
||||
c += (uint64_t)u.p8[0];
|
||||
break;
|
||||
case 0:
|
||||
c += sc_const;
|
||||
d += sc_const;
|
||||
}
|
||||
ShortEnd(a,b,c,d);
|
||||
*hash1 = a;
|
||||
*hash2 = b;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// do the whole hash in one call
|
||||
void SpookyHash::Hash128(
|
||||
const void *message,
|
||||
size_t length,
|
||||
uint64_t *hash1,
|
||||
uint64_t *hash2)
|
||||
{
|
||||
if (length < sc_bufSize)
|
||||
{
|
||||
Short(message, length, hash1, hash2);
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11;
|
||||
uint64_t buf[sc_numVars];
|
||||
uint64_t *end;
|
||||
union
|
||||
{
|
||||
const uint8_t *p8;
|
||||
uint64_t *p64;
|
||||
size_t i;
|
||||
} u;
|
||||
size_t remainder;
|
||||
|
||||
h0=h3=h6=h9 = *hash1;
|
||||
h1=h4=h7=h10 = *hash2;
|
||||
h2=h5=h8=h11 = sc_const;
|
||||
|
||||
u.p8 = (const uint8_t *)message;
|
||||
end = u.p64 + (length/sc_blockSize)*sc_numVars;
|
||||
|
||||
// handle all whole sc_blockSize blocks of bytes
|
||||
if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0))
|
||||
{
|
||||
while (u.p64 < end)
|
||||
{
|
||||
Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
|
||||
u.p64 += sc_numVars;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (u.p64 < end)
|
||||
{
|
||||
memcpy(buf, u.p64, sc_blockSize);
|
||||
Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
|
||||
u.p64 += sc_numVars;
|
||||
}
|
||||
}
|
||||
|
||||
// handle the last partial block of sc_blockSize bytes
|
||||
remainder = (length - ((const uint8_t *)end-(const uint8_t *)message));
|
||||
memcpy(buf, end, remainder);
|
||||
memset(((uint8_t *)buf)+remainder, 0, sc_blockSize-remainder);
|
||||
((uint8_t *)buf)[sc_blockSize-1] = remainder;
|
||||
|
||||
// do some final mixing
|
||||
End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11);
|
||||
*hash1 = h0;
|
||||
*hash2 = h1;
|
||||
}
|
||||
|
||||
47
luprex/core/cpp/spookyv2.hpp
Normal file
47
luprex/core/cpp/spookyv2.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// SpookyHash: a 128-bit noncryptographic hash function
|
||||
// By Bob Jenkins, public domain
|
||||
// Oct 31 2010: alpha, framework + SpookyHash::Mix appears right
|
||||
// Oct 31 2011: alpha again, Mix only good to 2^^69 but rest appears right
|
||||
// Dec 31 2011: beta, improved Mix, tested it for 2-bit deltas
|
||||
// Feb 2 2012: production, same bits as beta
|
||||
// Feb 5 2012: adjusted definitions of uint* to be more portable
|
||||
// Mar 30 2012: 3 bytes/cycle, not 4. Alpha was 4 but wasn't thorough enough.
|
||||
// August 5 2012: SpookyV2 (different results)
|
||||
//
|
||||
// Up to 3 bytes/cycle for long messages. Reasonably fast for short messages.
|
||||
// All 1 or 2 bit deltas achieve avalanche within 1% bias per output bit.
|
||||
//
|
||||
// This was developed for and tested on 64-bit x86-compatible processors.
|
||||
// It assumes the processor is little-endian. There is a macro
|
||||
// controlling whether unaligned reads are allowed (by default they are).
|
||||
// This should be an equally good hash on big-endian machines, but it will
|
||||
// compute different results on them than on little-endian machines.
|
||||
//
|
||||
// Google's CityHash has similar specs to SpookyHash, and CityHash is faster
|
||||
// on new Intel boxes. MD4 and MD5 also have similar specs, but they are orders
|
||||
// of magnitude slower. CRCs are two or more times slower, but unlike
|
||||
// SpookyHash, they have nice math for combining the CRCs of pieces to form
|
||||
// the CRCs of wholes. There are also cryptographic hashes, but those are even
|
||||
// slower than MD5.
|
||||
//
|
||||
|
||||
#include <stddef.h>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
class SpookyHash
|
||||
{
|
||||
public:
|
||||
//
|
||||
// SpookyHash: hash a single message in one call, produce 128-bit output
|
||||
//
|
||||
static void Hash128(
|
||||
const void *message, // message to hash
|
||||
size_t length, // length of message in bytes
|
||||
uint64_t *hash1, // input seed0, output hash0
|
||||
uint64_t *hash2); // input seed1, output hash1
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "streambuffer.hpp"
|
||||
#include "spookyv2.hpp"
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
@@ -151,6 +152,12 @@ void StreamBuffer::write_double(double d) {
|
||||
write_cursor_ += 8;
|
||||
}
|
||||
|
||||
void StreamBuffer::write_hashvalue(const util::HashValue &hv) {
|
||||
make_space(16);
|
||||
memcpy(write_cursor_, &hv, 16);
|
||||
write_cursor_ += 16;
|
||||
}
|
||||
|
||||
void StreamBuffer::write_bytes(const char *s, int64_t len) {
|
||||
make_space(len);
|
||||
memcpy(write_cursor_, s, len);
|
||||
@@ -224,6 +231,14 @@ int64_t StreamBuffer::read_int64() {
|
||||
return v;
|
||||
}
|
||||
|
||||
util::HashValue StreamBuffer::read_hashvalue() {
|
||||
check_available(16);
|
||||
util::HashValue hv;
|
||||
memcpy(&hv, read_cursor_, 16);
|
||||
read_cursor_ += 16;
|
||||
return hv;
|
||||
}
|
||||
|
||||
float StreamBuffer::read_float() {
|
||||
check_available(4);
|
||||
float f;
|
||||
@@ -296,6 +311,13 @@ void StreamBuffer::unwrite_to(int64_t wr_count) {
|
||||
write_cursor_ = buf_lo_ + (wr_count - pre_read_count_);
|
||||
}
|
||||
|
||||
util::HashValue StreamBuffer::hash() const {
|
||||
uint64_t hash1 = 0x82A7912E7893AC87;
|
||||
uint64_t hash2 = 0x81D402740DE458F3;
|
||||
SpookyHash::Hash128(read_cursor_, write_cursor_ - read_cursor_, &hash1, &hash2);
|
||||
return std::make_pair(hash1, hash2);
|
||||
}
|
||||
|
||||
int StreamBuffer::lua_writer(lua_State *L, const void* p, size_t sz, void* ud) {
|
||||
StreamBuffer *sb = (StreamBuffer *)ud;
|
||||
sb->write_bytes((const char *)p, sz);
|
||||
|
||||
@@ -211,10 +211,12 @@
|
||||
#define STREAMBUFFER_HPP
|
||||
|
||||
#include "luastack.hpp"
|
||||
#include "util.hpp"
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
|
||||
class StreamException
|
||||
{
|
||||
@@ -316,6 +318,7 @@ public:
|
||||
void write_int64(int64_t v);
|
||||
void write_float(float f);
|
||||
void write_double(double d);
|
||||
void write_hashvalue(const util::HashValue &hv);
|
||||
void write_uint8(uint8_t v) { write_int8(v); }
|
||||
void write_uint16(uint16_t v) { write_int16(v); }
|
||||
void write_uint32(uint32_t v) { write_int32(v); }
|
||||
@@ -345,6 +348,7 @@ public:
|
||||
int64_t read_int64();
|
||||
float read_float();
|
||||
double read_double();
|
||||
util::HashValue read_hashvalue();
|
||||
uint8_t read_uint8() { return read_int8(); }
|
||||
uint16_t read_uint16() { return read_int16(); }
|
||||
uint32_t read_uint32() { return read_int32(); }
|
||||
@@ -375,6 +379,9 @@ public:
|
||||
// Rewind the write cursor to a previous position.
|
||||
void unwrite_to(int64_t write_count);
|
||||
|
||||
// Calculate a noncryptographic but good hash of what's in the buffer.
|
||||
util::HashValue hash() const;
|
||||
|
||||
// Use the stream buffer as a lua_Writer.
|
||||
static int lua_writer(lua_State *L, const void* p, size_t sz, void* ud);
|
||||
void *lua_writer_ud();
|
||||
|
||||
@@ -177,8 +177,7 @@ void TextGame::check_redirects() {
|
||||
|
||||
void TextGame::run()
|
||||
{
|
||||
world_.reset(new World);
|
||||
world_->init_standalone();
|
||||
world_.reset(new World(util::WORLD_TYPE_STANDALONE));
|
||||
actor_id_ = world_->create_login_actor();
|
||||
std::cerr << "Login actor ID: " << actor_id_ << std::endl;
|
||||
console_.clear();
|
||||
|
||||
@@ -119,19 +119,6 @@ double distance_squared(double x1, double y1, double x2, double y2) {
|
||||
c ^= b; c -= hash_rot(b,24); \
|
||||
}
|
||||
|
||||
uint32_t hash3(uint32_t a, uint32_t b, uint32_t c) {
|
||||
hash_final(a, b, c);
|
||||
return c;
|
||||
}
|
||||
|
||||
double hash_to_float(double lo, double hi, uint32_t a, uint32_t b, uint32_t c) {
|
||||
double result = hash3(a, b, c); // Lossless.
|
||||
result *= (1.0 / 0xFFFFFFFF);
|
||||
result *= (hi-lo);
|
||||
result += lo;
|
||||
return result;
|
||||
}
|
||||
|
||||
std::ostream & operator << (std::ostream &out, const XYZ &xyz) {
|
||||
out << "(" << xyz.x << "," << xyz.y << "," << xyz.z << ")";
|
||||
return out;
|
||||
|
||||
@@ -11,8 +11,16 @@
|
||||
|
||||
namespace util {
|
||||
|
||||
enum WorldType {
|
||||
WORLD_TYPE_STANDALONE,
|
||||
WORLD_TYPE_C_SYNC,
|
||||
WORLD_TYPE_S_SYNC,
|
||||
WORLD_TYPE_MASTER,
|
||||
};
|
||||
|
||||
using stringvec = std::vector<std::string>;
|
||||
using stringset = std::set<std::string>;
|
||||
using HashValue = std::pair<uint64_t, uint64_t>;
|
||||
|
||||
// String to lowercase/uppercase
|
||||
std::string tolower(std::string input);
|
||||
@@ -41,12 +49,6 @@ std::string get_file_fingerprint(const std::string &path);
|
||||
// Calculate distance between two points
|
||||
double distance_squared(double x1, double y1, double x2, double y2);
|
||||
|
||||
// Return a pseudorandom number which is a hash function of A,B,C.
|
||||
uint32_t hash3(uint32_t a, uint32_t b, uint32_t c);
|
||||
|
||||
// Return a pseudorandom float between lo and hi inclusive.
|
||||
double hash_to_float(double lo, double hi, uint32_t a, uint32_t b, uint32_t c);
|
||||
|
||||
// An XYZ coordinate, general purpose.
|
||||
struct XYZ {
|
||||
float x, y, z;
|
||||
@@ -56,6 +58,6 @@ struct XYZ {
|
||||
bool operator !=(const XYZ &o) const { return x!=o.x || y != o.y || z!=o.z; }
|
||||
};
|
||||
std::ostream & operator << (std::ostream &out, const XYZ &xyz);
|
||||
|
||||
|
||||
} // namespace util
|
||||
#endif // UTIL_HPP
|
||||
|
||||
@@ -26,9 +26,16 @@ World *World::fetch_global_pointer(lua_State *L) {
|
||||
World::~World() {
|
||||
}
|
||||
|
||||
World::World() {
|
||||
World::World(util::WorldType wt) {
|
||||
// Master world model by default.
|
||||
world_type_ = wt;
|
||||
|
||||
// Initialize the ID allocator in master mode.
|
||||
id_global_pool_.init_master(10);
|
||||
if (wt == util::WORLD_TYPE_MASTER) {
|
||||
id_global_pool_.init_master();
|
||||
} else {
|
||||
id_global_pool_.init_synch();
|
||||
}
|
||||
|
||||
// Prepare to manipulate the lua state.
|
||||
LuaVar world;
|
||||
@@ -47,11 +54,20 @@ World::World() {
|
||||
source_db_.init(state());
|
||||
source_db_.rebuild();
|
||||
|
||||
// Do standalone initializations.
|
||||
if (world_type_ == util::WORLD_TYPE_STANDALONE) {
|
||||
// Load the lua source from disk then rebuild the environment.
|
||||
source_db_.update();
|
||||
source_db_.rebuild();
|
||||
|
||||
// Run unit tests.
|
||||
source_db_.run_unittests();
|
||||
}
|
||||
LS.result();
|
||||
assert (lua_gettop(state()) == 0);
|
||||
assert (stack_is_clear());
|
||||
}
|
||||
|
||||
Tangible::Tangible(World *w, int64_t id) : world_(w), id_player_pool_(&w->id_global_pool_) {
|
||||
Tangible::Tangible(World *w, int64_t id) : world_(w), anim_queue_(w->world_type_), id_player_pool_(&w->id_global_pool_) {
|
||||
plane_item_.set_id(id);
|
||||
plane_item_.track(&w->plane_map_);
|
||||
}
|
||||
@@ -72,18 +88,6 @@ void Tangible::deserialize(StreamBuffer *sb) {
|
||||
update_plane_item();
|
||||
}
|
||||
|
||||
void World::init_standalone() {
|
||||
assert(stack_is_clear());
|
||||
|
||||
// Load the lua source from disk then rebuild the environment.
|
||||
source_db_.update();
|
||||
source_db_.rebuild();
|
||||
|
||||
// Run unit tests.
|
||||
source_db_.run_unittests();
|
||||
|
||||
assert(stack_is_clear());
|
||||
}
|
||||
|
||||
Tangible *World::tangible_get(int64_t id) {
|
||||
auto iter = tangibles_.find(id);
|
||||
@@ -203,7 +207,7 @@ int64_t World::create_login_actor() {
|
||||
LS.getmetatable(mt, database);
|
||||
LS.rawset(mt, "__index", classtab);
|
||||
LS.result();
|
||||
tan->id_player_pool_.enable_fifo();
|
||||
tan->configure_id_pool_for_actor();
|
||||
assert(stack_is_clear());
|
||||
return tan->id();
|
||||
}
|
||||
@@ -597,7 +601,7 @@ LuaDefine(tangible_redirect, "c") {
|
||||
w->redirects_[tan1->id()] = 0;
|
||||
} else {
|
||||
Tangible *tan2 = w->tangible_get(L, actor2.index());
|
||||
tan2->be_an_actor();
|
||||
tan2->configure_id_pool_for_actor();
|
||||
w->redirects_[tan1->id()] = tan2->id();
|
||||
}
|
||||
if (bulldoze) {
|
||||
|
||||
@@ -69,13 +69,15 @@ public:
|
||||
int64_t id() { return plane_item_.id(); }
|
||||
|
||||
void update_plane_item();
|
||||
bool is_an_actor() { return id_player_pool_.fifo_enabled(); }
|
||||
void be_an_actor() { id_player_pool_.enable_fifo(); }
|
||||
bool is_an_actor() { return (id_player_pool_.get_fifo_capacity() > 0); }
|
||||
void configure_id_pool_for_actor() { id_player_pool_.set_fifo_capacity(20); }
|
||||
};
|
||||
|
||||
class World {
|
||||
public:
|
||||
|
||||
// Type of model
|
||||
util::WorldType world_type_;
|
||||
|
||||
// A lua intepreter with snapshot function.
|
||||
//
|
||||
LuaSnap lua_snap_;
|
||||
@@ -119,7 +121,7 @@ public:
|
||||
// is stored in world::lua_state_. A significant amount of
|
||||
// initial setup is done by this constructor.
|
||||
//
|
||||
World();
|
||||
World(util::WorldType wt);
|
||||
|
||||
// Destructor.
|
||||
//
|
||||
@@ -127,10 +129,6 @@ public:
|
||||
//
|
||||
~World();
|
||||
|
||||
// Initialize for single-player mode.
|
||||
//
|
||||
void init_standalone();
|
||||
|
||||
// get_lua_state
|
||||
//
|
||||
// Get the lua interpreter associated with this world model.
|
||||
|
||||
Reference in New Issue
Block a user