aboutsummaryrefslogtreecommitdiffstats
path: root/src/zbset.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2016-03-15 00:20:00 +0100
committerMattias Andrée <maandree@kth.se>2016-03-15 00:20:00 +0100
commit92be5631d8e319babf5cca49f53ea5e692c54793 (patch)
tree30c9a7427219677f6302460e3fb541dc223619a4 /src/zbset.c
parentOptimise zswap (diff)
downloadlibzahl-92be5631d8e319babf5cca49f53ea5e692c54793.tar.gz
libzahl-92be5631d8e319babf5cca49f53ea5e692c54793.tar.bz2
libzahl-92be5631d8e319babf5cca49f53ea5e692c54793.tar.xz
Optimisations
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/zbset.c')
-rw-r--r--src/zbset.c65
1 files changed, 36 insertions, 29 deletions
diff --git a/src/zbset.c b/src/zbset.c
index 2874238..cc7b2b0 100644
--- a/src/zbset.c
+++ b/src/zbset.c
@@ -2,38 +2,45 @@
#include "internals.h"
-void
-zbset(z_t a, z_t b, size_t bit, int action)
-{
- zahl_char_t mask = 1;
- size_t chars;
-
- chars = FLOOR_BITS_TO_CHARS(bit);
- bit = BITS_IN_LAST_CHAR(bit);
- mask <<= bit;
+#define PROLOGUE(MAY_INCREASE)\
+ zahl_char_t mask = 1;\
+ size_t chars = FLOOR_BITS_TO_CHARS(bit);\
+ if (MAY_INCREASE) {\
+ if (zzero(a)) {\
+ a->used = 0;\
+ SET_SIGNUM(a, 1);\
+ }\
+ if (unlikely(chars >= a->used)) {\
+ ENSURE_SIZE(a, chars + 1);\
+ zmemset(a->chars + a->used, 0, chars + 1 - a->used);\
+ a->used = chars + 1;\
+ }\
+ } else if (unlikely(chars >= a->used)) {\
+ return;\
+ }\
+ bit = BITS_IN_LAST_CHAR(bit);\
+ mask <<= bit
- SET(a, b);
- if (action) {
- if (zzero(a)) {
- a->used = 0;
- SET_SIGNUM(a, 1);
- }
- if (a->used <= chars) {
- ENSURE_SIZE(a, chars + 1);
- zmemset(a->chars + a->used, 0, chars + 1 - a->used);
- a->used = chars + 1;
- }
- }
+void
+zbset_impl_set(z_t a, z_t b, size_t bit)
+{
+ PROLOGUE(1);
+ a->chars[chars] |= mask;
+}
- if (action > 0) {
- a->chars[chars] |= mask;
- return;
- } else if (action < 0) {
- a->chars[chars] ^= mask;
- } else if (chars < a->used) {
- a->chars[chars] &= ~mask;
- }
+void
+zbset_impl_clear(z_t a, z_t b, size_t bit)
+{
+ PROLOGUE(0);
+ a->chars[chars] &= ~mask;
+ TRIM_AND_ZERO(a);
+}
+void
+zbset_impl_flip(z_t a, z_t b, size_t bit)
+{
+ PROLOGUE(1);
+ a->chars[chars] ^= mask;
TRIM_AND_ZERO(a);
}