Skip to content

Commit

Permalink
Merge pull request #6 from cameron314/master
Browse files Browse the repository at this point in the history
Fixed errors and warnings under MSVC2013
  • Loading branch information
Ed-von-Schleck committed Oct 6, 2014
2 parents 96589ef + 0874d63 commit 8d4a5ab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
8 changes: 8 additions & 0 deletions generate_compressor_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
{{{chrs_by_chr_and_successor_id}}}
}};
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4324) // structure was padded due to __declspec(align())
#endif
typedef struct Pack {{
const uint32_t word;
Expand All @@ -51,6 +55,10 @@
const char header;
}} Pack;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#define PACK_COUNT {pack_count}
#define MAX_SUCCESSOR_N {max_successor_len}
Expand Down
22 changes: 12 additions & 10 deletions shoco.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#if defined(_MSC_VER)
#define _ALIGNED __declspec(align(16))
#define inline __inline
#define restrict __restrict
#elif defined(__GNUC__)
#define _ALIGNED __attribute__ ((aligned(16)))
#else
Expand Down Expand Up @@ -58,14 +60,14 @@ static inline int check_indices(const int16_t * restrict indices, int pack_n) {
}
#else
static inline int check_indices(const int16_t * restrict indices, int pack_n) {
for (int i = 0; i < packs[pack_n].bytes_unpacked; ++i)
for (unsigned int i = 0; i < packs[pack_n].bytes_unpacked; ++i)
if (indices[i] > packs[pack_n].masks[i])
return 0;
return 1;
}
#endif

static inline int find_best_encoding(const int16_t * restrict indices, int n_consecutive) {
static inline int find_best_encoding(const int16_t * restrict indices, unsigned int n_consecutive) {
for (int p = PACK_COUNT - 1; p >= 0; --p)
if ((n_consecutive >= packs[p].bytes_unpacked) && (check_indices(indices, p)))
return p;
Expand All @@ -80,10 +82,10 @@ size_t shoco_compress(const char * const restrict original, size_t strlen, char
int last_chr_index;
int current_index;
int successor_index;
int n_consecutive;
unsigned int n_consecutive;
union Code code;
int pack_n;
int rest;
unsigned int rest;
const char * const in_end = original + strlen;

while ((*in != '\0')) {
Expand All @@ -109,7 +111,7 @@ size_t shoco_compress(const char * const restrict original, size_t strlen, char
if (successor_index < 0)
break;

indices[n_consecutive] = successor_index;
indices[n_consecutive] = (int16_t)successor_index;
last_chr_index = current_index;
}
if (n_consecutive < 2)
Expand All @@ -121,7 +123,7 @@ size_t shoco_compress(const char * const restrict original, size_t strlen, char
return bufsize + 1;

code.word = packs[pack_n].word;
for (int i = 0; i < packs[pack_n].bytes_unpacked; ++i)
for (unsigned int i = 0; i < packs[pack_n].bytes_unpacked; ++i)
code.word |= indices[i] << packs[pack_n].offsets[i];

// In the little-endian world, we need to swap what's
Expand All @@ -131,7 +133,7 @@ size_t shoco_compress(const char * const restrict original, size_t strlen, char

// if we'd just copy the word, we might write over the end
// of the output string
for (int i = 0; i < packs[pack_n].bytes_packed; ++i)
for (unsigned int i = 0; i < packs[pack_n].bytes_packed; ++i)
o[i] = code.bytes[i];

o += packs[pack_n].bytes_packed;
Expand Down Expand Up @@ -161,7 +163,7 @@ size_t shoco_decompress(const char * const restrict original, size_t complen, ch
char * const out_end = out + bufsize;
const char *in = original;
char last_chr;
union Code code;
union Code code = { 0 };
int offset;
int mask;
int mark;
Expand All @@ -185,7 +187,7 @@ size_t shoco_decompress(const char * const restrict original, size_t complen, ch
// This should be OK as well, but it fails with emscripten.
// Test this with new versions of emcc.
//code.word = swap(*(uint32_t *)in);
for (int i = 0; i < packs[mark].bytes_packed; ++i)
for (unsigned int i = 0; i < packs[mark].bytes_packed; ++i)
code.bytes[i] = in[i];
code.word = swap(code.word);

Expand All @@ -195,7 +197,7 @@ size_t shoco_decompress(const char * const restrict original, size_t complen, ch
last_chr = o[0] = chrs_by_chr_id[(code.word >> offset) & mask];

// unpack the successor chars
for (int i = 1; i < packs[mark].bytes_unpacked; ++i) {
for (unsigned int i = 1; i < packs[mark].bytes_unpacked; ++i) {
offset = packs[mark].offsets[i];
mask = packs[mark].masks[i];
last_chr = o[i] = chrs_by_chr_and_successor_id[(unsigned char)last_chr - MIN_CHR][(code.word >> offset) & mask];
Expand Down
20 changes: 18 additions & 2 deletions shoco.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,21 @@

#include <stddef.h>

size_t shoco_compress(const char * in, size_t len, char * out, size_t bufsize);
size_t shoco_decompress(const char * in, size_t len, char * out, size_t bufsize);
#if defined(_MSC_VER)
#define shoco_restrict __restrict
#else
#define shoco_restrict restrict
#endif

#ifdef __cplusplus
extern "C" {
#endif

size_t shoco_compress(const char * const shoco_restrict in, size_t len, char * const shoco_restrict out, size_t bufsize);
size_t shoco_decompress(const char * const shoco_restrict in, size_t len, char * const shoco_restrict out, size_t bufsize);

#ifdef __cplusplus
}
#endif

#undef shoco_restrict

0 comments on commit 8d4a5ab

Please sign in to comment.