From 2ce7d829b00d6c1e98ede25d567263d902efa937 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Mon, 28 Oct 2013 12:31:10 +0100 Subject: [PATCH] various issues related to encoding larger gaps than anticipated, mostly solved by moving to 64 bits calculations where required, clarifying signedness and adding asert to catch problems --- cpp/Makefile | 14 ++++++++++++++ cpp/gcs.cpp | 12 ++++++++---- cpp/gcs.h | 7 +++---- cpp/make.sh | 2 -- cpp/md5.c | 1 - 5 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 cpp/Makefile delete mode 100755 cpp/make.sh diff --git a/cpp/Makefile b/cpp/Makefile new file mode 100644 index 0000000..ac3eb88 --- /dev/null +++ b/cpp/Makefile @@ -0,0 +1,14 @@ +CXXFLAGS=-Wall -MMD -O3 +CFLAGS=-Wall -MMD -O3 +all: gcs + +-include *.d + +GCS_OBJECTS = main.o gcs.o md5.o + +gcs: $(GCS_OBJECTS) + g++ $(LDFLAGS) $(GCS_OBJECTS) -o $@ +clean: + rm -f *~ *.o *.d gcs + + diff --git a/cpp/gcs.cpp b/cpp/gcs.cpp index d02b9e2..eb8a8b3 100644 --- a/cpp/gcs.cpp +++ b/cpp/gcs.cpp @@ -31,10 +31,14 @@ #include #include -#define BITMASK(n) ((1 << (n)) - 1) +inline uint64_t BITMASK(unsigned int n) +{ + assert(n < 63); + return ((1ULL << (n)) - 1ULL); +} -static uint32_t gcs_hash(const void *data, int size, int N, int P) +static uint32_t gcs_hash(const void *data, int size, unsigned int N, unsigned int P) { unsigned char digest[16]; MD5Context ctx; @@ -72,7 +76,7 @@ class BitWriter BitWriter(std::ostream &_f) : f(_f), accum(0), n(0) {} - void write(int nbits, unsigned value) + void write(int nbits, uint64_t value) { assert(nbits >= 0); while (nbits) @@ -136,7 +140,7 @@ class GolombEncoder }; -GCSBuilder::GCSBuilder(int _n, int _p) +GCSBuilder::GCSBuilder(unsigned int _n, unsigned int _p) : N(_n), P(_p) { assert(N <= ~(hash_t)0 / P); diff --git a/cpp/gcs.h b/cpp/gcs.h index 710765f..492f440 100644 --- a/cpp/gcs.h +++ b/cpp/gcs.h @@ -34,18 +34,18 @@ typedef uint32_t hash_t; class GCSBuilder { - int N, P; + unsigned int N, P; std::vector values; public: - GCSBuilder(int N, int P); + GCSBuilder(unsigned int N, unsigned int P); void add(const void *data, int size); void finalize(std::ostream &f); }; class GCSQuery { - int N, P; + unsigned int N, P; std::istream &f; uint8_t *gcs; int gcs_len; @@ -56,6 +56,5 @@ class GCSQuery bool query(const void *data, int size); }; - #endif /* GCS_H */ diff --git a/cpp/make.sh b/cpp/make.sh deleted file mode 100755 index 1997657..0000000 --- a/cpp/make.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -g++ -O3 -o gcs main.cpp gcs.cpp md5.c diff --git a/cpp/md5.c b/cpp/md5.c index 3b2614d..60d253f 100644 --- a/cpp/md5.c +++ b/cpp/md5.c @@ -24,7 +24,6 @@ static void byteReverse(unsigned char *buf, unsigned longs) { - uint32 t; do { *(uint32*)buf = O32_HOST_TO_LE(*(uint32*)buf); buf += 4;