From d6b96cd470cbbdcbe0b1c7ca038d008736c75897 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sun, 22 Dec 2019 21:13:54 -0800 Subject: [PATCH] halve size of bitset for cpu compatibility --- .../{BitSet1024.cs => BitSet512.cs} | 100 +++++++++--------- encompass-cs/Collections/ComponentBitSet.cs | 8 +- encompass-cs/Collections/ComponentStore.cs | 2 +- encompass-cs/Engine.cs | 8 +- encompass-cs/EntitySetQuery.cs | 12 +-- test/BitSet1024Test.cs | 24 ++--- 6 files changed, 77 insertions(+), 77 deletions(-) rename encompass-cs/Collections/{BitSet1024.cs => BitSet512.cs} (55%) diff --git a/encompass-cs/Collections/BitSet1024.cs b/encompass-cs/Collections/BitSet512.cs similarity index 55% rename from encompass-cs/Collections/BitSet1024.cs rename to encompass-cs/Collections/BitSet512.cs index 239cd79..ea0593a 100644 --- a/encompass-cs/Collections/BitSet1024.cs +++ b/encompass-cs/Collections/BitSet512.cs @@ -2,28 +2,28 @@ namespace Encompass.Collections { - public static class BitSet1024Builder + public static class BitSet512Builder { - public static BitSet1024 Zeroes() + public static BitSet512 Zeroes() { - return new BitSet1024(Vector256Builder.Zeroes(), Vector256Builder.Zeroes(), Vector256Builder.Zeroes(), Vector256Builder.Zeroes()); + return new BitSet512(Vector128Builder.Zeroes(), Vector128Builder.Zeroes(), Vector128Builder.Zeroes(), Vector128Builder.Zeroes()); } - public static BitSet1024 Ones() + public static BitSet512 Ones() { - return new BitSet1024(Vector256Builder.Ones(), Vector256Builder.Ones(), Vector256Builder.Ones(), Vector256Builder.Ones()); + return new BitSet512(Vector128Builder.Ones(), Vector128Builder.Ones(), Vector128Builder.Ones(), Vector128Builder.Ones()); } } - public static class Vector256Builder + public static class Vector128Builder { - static readonly uint[] zeroes = new uint[8]; // max size of a Vector is 8 uints - static readonly uint[] ones = new uint[8] { uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue }; - static uint[] builderInts = new uint[8]; + static readonly uint[] zeroes = new uint[4]; // max size of a Vector is 8 uints + static readonly uint[] ones = new uint[4] { uint.MaxValue, uint.MaxValue, uint.MaxValue, uint.MaxValue }; + static uint[] builderInts = new uint[4]; private static void ResetBuilder() { - for (var i = 0; i < 8; i++) + for (var i = 0; i < 4; i++) { builderInts[i] = 0; } @@ -41,21 +41,21 @@ namespace Encompass.Collections public static Vector Build(int index) { - if (index > 255) { throw new System.ArgumentOutOfRangeException(nameof(index)); } + if (index > 127) { throw new System.ArgumentOutOfRangeException(nameof(index)); } ResetBuilder(); builderInts[index / 32] |= (uint)(1 << (index % 32)); return new Vector(builderInts); } } - public struct BitSet1024 + public struct BitSet512 { public Vector A { get; } public Vector B { get; } public Vector C { get; } public Vector D { get; } - internal BitSet1024(Vector a, Vector b, Vector c, Vector d) + internal BitSet512(Vector a, Vector b, Vector c, Vector d) { A = a; B = b; @@ -63,38 +63,38 @@ namespace Encompass.Collections D = d; } - public BitSet1024 And(BitSet1024 other) + public BitSet512 And(BitSet512 other) { - return new BitSet1024(A & other.A, B & other.B, C & other.C, D & other.D); + return new BitSet512(A & other.A, B & other.B, C & other.C, D & other.D); } - public BitSet1024 Or(BitSet1024 other) + public BitSet512 Or(BitSet512 other) { - return new BitSet1024(A | other.A, B | other.B, C | other.C, D | other.D); + return new BitSet512(A | other.A, B | other.B, C | other.C, D | other.D); } - public BitSet1024 Not() + public BitSet512 Not() { - return new BitSet1024(~A, ~B, ~C, ~D); + return new BitSet512(~A, ~B, ~C, ~D); } - public BitSet1024 Set(int index) + public BitSet512 Set(int index) { - if (index < 256) + if (index < 128) { - return new BitSet1024(A | Vector256Builder.Build(index % 256), B, C, D); + return new BitSet512(A | Vector128Builder.Build(index % 128), B, C, D); + } + else if (index < 256) + { + return new BitSet512(A, B | Vector128Builder.Build(index % 128), C, D); + } + else if (index < 384) + { + return new BitSet512(A, B, C | Vector128Builder.Build(index % 128), D); } else if (index < 512) { - return new BitSet1024(A, B | Vector256Builder.Build(index % 256), C, D); - } - else if (index < 768) - { - return new BitSet1024(A, B, C | Vector256Builder.Build(index % 256), D); - } - else if (index < 1024) - { - return new BitSet1024(A, B, C, D | Vector256Builder.Build(index % 256)); + return new BitSet512(A, B, C, D | Vector128Builder.Build(index % 128)); } else { @@ -102,23 +102,23 @@ namespace Encompass.Collections } } - public BitSet1024 UnSet(int index) + public BitSet512 UnSet(int index) { - if (index < 256) + if (index < 128) { - return new BitSet1024(A & ~Vector256Builder.Build(index % 256), B, C, D); + return new BitSet512(A & ~Vector128Builder.Build(index % 256), B, C, D); + } + else if (index < 256) + { + return new BitSet512(A, B & ~Vector128Builder.Build(index % 256), C, D); + } + else if (index < 384) + { + return new BitSet512(A, B, C & ~Vector128Builder.Build(index % 256), D); } else if (index < 512) { - return new BitSet1024(A, B & ~Vector256Builder.Build(index % 256), C, D); - } - else if (index < 768) - { - return new BitSet1024(A, B, C & ~Vector256Builder.Build(index % 256), D); - } - else if (index < 1024) - { - return new BitSet1024(A, B, C, D & ~Vector256Builder.Build(index % 256)); + return new BitSet512(A, B, C, D & ~Vector128Builder.Build(index % 256)); } else { @@ -128,20 +128,20 @@ namespace Encompass.Collections public bool Get(int index) { - var vectorIndex = index % 256; - if (index < 256) + var vectorIndex = index % 128; + if (index < 128) { return (A[vectorIndex / 32] & (uint)(1 << vectorIndex % 32)) != 0; } - else if (index < 512) + else if (index < 256) { return (B[vectorIndex / 32] & (uint)(1 << vectorIndex % 32)) != 0; } - else if (index < 768) + else if (index < 384) { return (C[vectorIndex / 32] & (uint)(1 << vectorIndex % 32)) != 0; } - else if (index < 1024) + else if (index < 512) { return (D[vectorIndex / 32] & (uint)(1 << vectorIndex % 32)) != 0; } @@ -153,7 +153,7 @@ namespace Encompass.Collections public bool AllTrue() { - for (var i = 0; i < 8; i++) + for (var i = 0; i < 4; i++) { if (A[i] != uint.MaxValue) { return false; } if (B[i] != uint.MaxValue) { return false; } @@ -165,7 +165,7 @@ namespace Encompass.Collections public bool AllFalse() { - for (var i = 0; i < 8; i++) + for (var i = 0; i < 4; i++) { if (A[i] != 0) { return false; } if (B[i] != 0) { return false; } diff --git a/encompass-cs/Collections/ComponentBitSet.cs b/encompass-cs/Collections/ComponentBitSet.cs index 63433dd..5dad445 100644 --- a/encompass-cs/Collections/ComponentBitSet.cs +++ b/encompass-cs/Collections/ComponentBitSet.cs @@ -7,7 +7,7 @@ namespace Encompass { internal class ComponentBitSet { - Dictionary entities = new Dictionary(); + Dictionary entities = new Dictionary(); Dictionary TypeToIndex { get; } public ComponentBitSet(Dictionary typeToIndex) @@ -27,7 +27,7 @@ namespace Encompass public void AddEntity(Entity entity) { - entities.Add(entity, BitSet1024Builder.Zeroes()); + entities.Add(entity, BitSet512Builder.Zeroes()); } public void Set(Entity entity) where TComponent : struct, IComponent @@ -52,9 +52,9 @@ namespace Encompass } } - public BitSet1024 EntityBitArray(Entity entity) + public BitSet512 EntityBitArray(Entity entity) { - return entities.ContainsKey(entity) ? entities[entity] : BitSet1024Builder.Zeroes(); + return entities.ContainsKey(entity) ? entities[entity] : BitSet512Builder.Zeroes(); } } } diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index 5d3f09a..90280c5 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -52,7 +52,7 @@ namespace Encompass return Stores.ContainsKey(type) && Stores[type].Has(entity); } - public BitSet1024 EntityBitArray(Entity entity) + public BitSet512 EntityBitArray(Entity entity) { return ComponentBitSet.EntityBitArray(entity); } diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 8e0a8ae..b61752d 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -642,25 +642,25 @@ namespace Encompass /// internal void BuildEntityQuery() { - var withMask = BitSet1024Builder.Zeroes(); + var withMask = BitSet512Builder.Zeroes(); foreach (var type in queryWithTypes) { withMask = withMask.Set(componentUpdateManager.TypeToIndex[type]); } - var withoutMask = BitSet1024Builder.Zeroes(); + var withoutMask = BitSet512Builder.Zeroes(); foreach (var type in queryWithoutTypes) { withoutMask = withoutMask.Set(componentUpdateManager.TypeToIndex[type]); } - var pendingMask = BitSet1024Builder.Zeroes(); + var pendingMask = BitSet512Builder.Zeroes(); foreach (var type in readPendingTypes) { pendingMask = pendingMask.Set(componentUpdateManager.TypeToIndex[type]); } - var existingMask = BitSet1024Builder.Zeroes(); + var existingMask = BitSet512Builder.Zeroes(); foreach (var type in readTypes) { existingMask = existingMask.Set(componentUpdateManager.TypeToIndex[type]); diff --git a/encompass-cs/EntitySetQuery.cs b/encompass-cs/EntitySetQuery.cs index 899ddcc..219c712 100644 --- a/encompass-cs/EntitySetQuery.cs +++ b/encompass-cs/EntitySetQuery.cs @@ -6,13 +6,13 @@ namespace Encompass { internal struct EntitySetQuery { - private BitSet1024 WithPendingMask { get; } - private BitSet1024 WithExistingMask { get; } - private BitSet1024 WithoutPendingMask { get; } - private BitSet1024 WithoutExistingMask { get; } - private BitSet1024 NotWithMask { get; } + private BitSet512 WithPendingMask { get; } + private BitSet512 WithExistingMask { get; } + private BitSet512 WithoutPendingMask { get; } + private BitSet512 WithoutExistingMask { get; } + private BitSet512 NotWithMask { get; } - internal EntitySetQuery(BitSet1024 withPendingMask, BitSet1024 withExistingMask, BitSet1024 withoutPendingMask, BitSet1024 withoutExistingMask, BitSet1024 notWithMask) + internal EntitySetQuery(BitSet512 withPendingMask, BitSet512 withExistingMask, BitSet512 withoutPendingMask, BitSet512 withoutExistingMask, BitSet512 notWithMask) { WithPendingMask = withPendingMask; WithExistingMask = withExistingMask; diff --git a/test/BitSet1024Test.cs b/test/BitSet1024Test.cs index 81e1a4b..9eeac8a 100644 --- a/test/BitSet1024Test.cs +++ b/test/BitSet1024Test.cs @@ -4,50 +4,50 @@ using NUnit.Framework; namespace Tests { - public class BitSet1024Test + public class BitSet512Test { [Test] public void Zeroes() { - var bitSet = BitSet1024Builder.Zeroes(); + var bitSet = BitSet512Builder.Zeroes(); bitSet.AllFalse().Should().BeTrue(); } [Test] public void Ones() { - var bitSet = BitSet1024Builder.Ones(); + var bitSet = BitSet512Builder.Ones(); bitSet.AllTrue().Should().BeTrue(); } [Test] public void Set() { - var bitSet = BitSet1024Builder.Zeroes().Set(5); + var bitSet = BitSet512Builder.Zeroes().Set(5); bitSet.AllFalse().Should().BeFalse(); - bitSet = BitSet1024Builder.Zeroes().Set(278); + bitSet = BitSet512Builder.Zeroes().Set(132); bitSet.AllFalse().Should().BeFalse(); - bitSet = BitSet1024Builder.Zeroes().Set(569); + bitSet = BitSet512Builder.Zeroes().Set(268); bitSet.AllFalse().Should().BeFalse(); - bitSet = BitSet1024Builder.Zeroes().Set(1023); + bitSet = BitSet512Builder.Zeroes().Set(450); bitSet.AllFalse().Should().BeFalse(); } [Test] public void UnSet() { - var bitSet = BitSet1024Builder.Ones().UnSet(562); - bitSet.Get(562).Should().BeFalse(); - bitSet.Set(562).AllTrue().Should().BeTrue(); + var bitSet = BitSet512Builder.Ones().UnSet(285); + bitSet.Get(285).Should().BeFalse(); + bitSet.Set(285).AllTrue().Should().BeTrue(); } [Test] public void Get() { - var bitSet = BitSet1024Builder.Zeroes().Set(359); + var bitSet = BitSet512Builder.Zeroes().Set(359); bitSet.Get(359).Should().BeTrue(); bitSet.UnSet(359).AllFalse().Should().BeTrue(); } @@ -55,7 +55,7 @@ namespace Tests [Test] public void Not() { - var bitSet = BitSet1024Builder.Ones().Not(); + var bitSet = BitSet512Builder.Ones().Not(); bitSet.AllFalse().Should().BeTrue(); } }