summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFedja Beader <fedja@protonmail.ch>2025-05-03 14:56:08 +0000
committerJesusalva Jesusalva <jesusalva@tmw2.org>2025-05-03 14:56:08 +0000
commit99b39d2a2f82c4e1612569a8beac5b9024b1293d (patch)
treea0d68d06c21a90e09fc39bb8a2a1aced2ed524fc
parentab7ee05b385c3da2e5437379f4511192fbc72618 (diff)
downloadserverdata-99b39d2a2f82c4e1612569a8beac5b9024b1293d.tar.gz
serverdata-99b39d2a2f82c4e1612569a8beac5b9024b1293d.tar.bz2
serverdata-99b39d2a2f82c4e1612569a8beac5b9024b1293d.tar.xz
serverdata-99b39d2a2f82c4e1612569a8beac5b9024b1293d.zip
Simplify bitmask_count
Untested * Herc really doesen't like us setting the upper bit **** ml/serverdata!171
-rw-r--r--npc/dev/test.txt7
-rw-r--r--npc/functions/bitwise.txt24
2 files changed, 15 insertions, 16 deletions
diff --git a/npc/dev/test.txt b/npc/dev/test.txt
index b430dd57d..2b4b195ce 100644
--- a/npc/dev/test.txt
+++ b/npc/dev/test.txt
@@ -638,6 +638,13 @@ function script HerculesSelfTestHelper {
callsub(OnCheck, "min()", min(5, -10, 8, 3, -2, 1000), -10);
callsub(OnCheck, "max()", max(5, -10, 8, 3, -2, 1000), 1000);
+ // bit flag helpers
+ callsub(OnCheck, "bitmask_count(0)", bitmask_count(0), 0);
+ callsub(OnCheck, "bitmask_count(1)", bitmask_count(1), 1);
+ // first bit falls out of type int32_t... turns into a script error.
+ //callsub(OnCheck, "bitmask_count( << 30)", bitmask_count(3 << 30), 1);
+ callsub(OnCheck, "bitmask_count(0x55555555)", bitmask_count(0x55555555), 16);
+
// Constants
callsub(OnCheck, "'true' constant", true, 1);
diff --git a/npc/functions/bitwise.txt b/npc/functions/bitwise.txt
index eadfb30ce..38021e715 100644
--- a/npc/functions/bitwise.txt
+++ b/npc/functions/bitwise.txt
@@ -47,25 +47,17 @@ function script bitwise_set {
}
// bitmask_count(<int>)
-// returns the number of bits set in <int> (up to 4096?)
+// returns the number of bits set in <int>
function script bitmask_count {
.@n = getarg(0); // Number evaluated
- .@p=0; // Bits set/unset
- .@s=0; // Stack and Check
- .@m=0; // Memory
-
- // Loop only as needed
- while (.@s < .@n) {
- // Safeguard (2,147,483,647 is INT_MAX)
- if (.@m >= 31)
- break;
- // Actual calculation
- .@s=2**.@m;
- if (.@n & .@s)
- .@p++;
- .@m++;
+ .@ones = 0; // Bits set
+
+ while (.@n > 0) {
+ if (.@n & 1)
+ .@ones ++;
+ .@n >>= 1;
}
- return .@p;
+ return .@ones;
}
/////////////////////////////////////////////////////////////////////////////////