Skip to content

Commit

Permalink
Merge pull request madler#7 from kitaisreal/functable-use-attribute-c…
Browse files Browse the repository at this point in the history
…onstructor

Use constructors for functable
  • Loading branch information
alexey-milovidov authored Apr 18, 2021
2 parents 4039bb4 + d03dce9 commit 7f25452
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 62 deletions.
72 changes: 15 additions & 57 deletions functable.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ extern uint32_t longest_match_unaligned_avx2(deflate_state *const s, Pos cur_mat
#endif
#endif

Z_INTERNAL Z_TLS struct functable_s functable;
/* functable init */
Z_INTERNAL struct functable_s functable;

/* stub functions */
Z_INTERNAL void insert_string_stub(deflate_state *const s, const uint32_t str, uint32_t count) {
static void __attribute__((constructor)) insert_string_stub() {
// Initialize default

functable.insert_string = &insert_string_c;
Expand All @@ -149,11 +150,9 @@ Z_INTERNAL void insert_string_stub(deflate_state *const s, const uint32_t str, u
if (arm_cpu_has_crc32)
functable.insert_string = &insert_string_acle;
#endif

functable.insert_string(s, str, count);
}

Z_INTERNAL Pos quick_insert_string_stub(deflate_state *const s, const uint32_t str) {
static void __attribute__((constructor)) quick_insert_string_stub_init() {
functable.quick_insert_string = &quick_insert_string_c;

#ifdef X86_SSE42_CRC_HASH
Expand All @@ -163,11 +162,9 @@ Z_INTERNAL Pos quick_insert_string_stub(deflate_state *const s, const uint32_t s
if (arm_cpu_has_crc32)
functable.quick_insert_string = &quick_insert_string_acle;
#endif

return functable.quick_insert_string(s, str);
}

Z_INTERNAL void slide_hash_stub(deflate_state *s) {
static void __attribute__((constructor)) slide_hash_stub_init() {

functable.slide_hash = &slide_hash_c;

Expand All @@ -190,11 +187,9 @@ Z_INTERNAL void slide_hash_stub(deflate_state *s) {
if (power_cpu_has_arch_2_07)
functable.slide_hash = &slide_hash_power8;
#endif

functable.slide_hash(s);
}

Z_INTERNAL uint32_t adler32_stub(uint32_t adler, const unsigned char *buf, size_t len) {
static void __attribute__((constructor)) adler32_stub_init() {
// Initialize default
functable.adler32 = &adler32_c;

Expand All @@ -216,11 +211,9 @@ Z_INTERNAL uint32_t adler32_stub(uint32_t adler, const unsigned char *buf, size_
if (power_cpu_has_arch_2_07)
functable.adler32 = &adler32_power8;
#endif

return functable.adler32(adler, buf, len);
}

Z_INTERNAL uint32_t chunksize_stub(void) {
static void __attribute__((constructor)) chunksize_stub_init(void) {
// Initialize default
functable.chunksize = &chunksize_c;

Expand All @@ -238,11 +231,9 @@ Z_INTERNAL uint32_t chunksize_stub(void) {
if (arm_cpu_has_neon)
functable.chunksize = &chunksize_neon;
#endif

return functable.chunksize();
}

Z_INTERNAL uint8_t* chunkcopy_stub(uint8_t *out, uint8_t const *from, unsigned len) {
static void __attribute__((constructor)) chunkcopy_stub_init() {
// Initialize default
functable.chunkcopy = &chunkcopy_c;

Expand All @@ -260,11 +251,9 @@ Z_INTERNAL uint8_t* chunkcopy_stub(uint8_t *out, uint8_t const *from, unsigned l
if (arm_cpu_has_neon)
functable.chunkcopy = &chunkcopy_neon;
#endif

return functable.chunkcopy(out, from, len);
}

Z_INTERNAL uint8_t* chunkcopy_safe_stub(uint8_t *out, uint8_t const *from, unsigned len, uint8_t *safe) {
static void __attribute__((constructor)) chunkcopy_safe_stub_init() {
// Initialize default
functable.chunkcopy_safe = &chunkcopy_safe_c;

Expand All @@ -282,11 +271,9 @@ Z_INTERNAL uint8_t* chunkcopy_safe_stub(uint8_t *out, uint8_t const *from, unsig
if (arm_cpu_has_neon)
functable.chunkcopy_safe = &chunkcopy_safe_neon;
#endif

return functable.chunkcopy_safe(out, from, len, safe);
}

Z_INTERNAL uint8_t* chunkunroll_stub(uint8_t *out, unsigned *dist, unsigned *len) {
static void __attribute__((constructor)) chunkunroll_stub_init() {
// Initialize default
functable.chunkunroll = &chunkunroll_c;

Expand All @@ -305,11 +292,9 @@ Z_INTERNAL uint8_t* chunkunroll_stub(uint8_t *out, unsigned *dist, unsigned *len
if (arm_cpu_has_neon)
functable.chunkunroll = &chunkunroll_neon;
#endif

return functable.chunkunroll(out, dist, len);
}

Z_INTERNAL uint8_t* chunkmemset_stub(uint8_t *out, unsigned dist, unsigned len) {
static void __attribute__((constructor)) chunkmemset_stub_init() {
// Initialize default
functable.chunkmemset = &chunkmemset_c;

Expand All @@ -327,11 +312,9 @@ Z_INTERNAL uint8_t* chunkmemset_stub(uint8_t *out, unsigned dist, unsigned len)
if (arm_cpu_has_neon)
functable.chunkmemset = &chunkmemset_neon;
#endif

return functable.chunkmemset(out, dist, len);
}

Z_INTERNAL uint8_t* chunkmemset_safe_stub(uint8_t *out, unsigned dist, unsigned len, unsigned left) {
static void __attribute__((constructor)) chunkmemset_safe_stub_init() {
// Initialize default
functable.chunkmemset_safe = &chunkmemset_safe_c;

Expand All @@ -349,11 +332,9 @@ Z_INTERNAL uint8_t* chunkmemset_safe_stub(uint8_t *out, unsigned dist, unsigned
if (arm_cpu_has_neon)
functable.chunkmemset_safe = &chunkmemset_safe_neon;
#endif

return functable.chunkmemset_safe(out, dist, len, left);
}

Z_INTERNAL uint32_t crc32_stub(uint32_t crc, const unsigned char *buf, uint64_t len) {
static void __attribute__((constructor)) crc32_stub_init() {
int32_t use_byfour = sizeof(void *) == sizeof(ptrdiff_t);

Assert(sizeof(uint64_t) >= sizeof(size_t),
Expand All @@ -375,11 +356,9 @@ Z_INTERNAL uint32_t crc32_stub(uint32_t crc, const unsigned char *buf, uint64_t
} else {
functable.crc32 = crc32_generic;
}

return functable.crc32(crc, buf, len);
}

Z_INTERNAL uint32_t compare258_stub(const unsigned char *src0, const unsigned char *src1) {
static void __attribute__((constructor)) compare258_stub_init() {

functable.compare258 = &compare258_c;

Expand All @@ -400,11 +379,9 @@ Z_INTERNAL uint32_t compare258_stub(const unsigned char *src0, const unsigned ch
functable.compare258 = &compare258_unaligned_avx2;
# endif
#endif

return functable.compare258(src0, src1);
}

Z_INTERNAL uint32_t longest_match_stub(deflate_state *const s, Pos cur_match) {
static void __attribute__((constructor)) longest_match_stub_init() {

functable.longest_match = &longest_match_c;

Expand All @@ -425,23 +402,4 @@ Z_INTERNAL uint32_t longest_match_stub(deflate_state *const s, Pos cur_match) {
functable.longest_match = &longest_match_unaligned_avx2;
# endif
#endif

return functable.longest_match(s, cur_match);
}

/* functable init */
Z_INTERNAL Z_TLS struct functable_s functable = {
insert_string_stub,
quick_insert_string_stub,
adler32_stub,
crc32_stub,
slide_hash_stub,
compare258_stub,
longest_match_stub,
chunksize_stub,
chunkcopy_stub,
chunkcopy_safe_stub,
chunkunroll_stub,
chunkmemset_stub,
chunkmemset_safe_stub
};
2 changes: 1 addition & 1 deletion functable.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ struct functable_s {
uint8_t* (* chunkmemset_safe) (uint8_t *out, unsigned dist, unsigned len, unsigned left);
};

Z_INTERNAL extern Z_TLS struct functable_s functable;
Z_INTERNAL extern struct functable_s functable;

#endif
4 changes: 0 additions & 4 deletions zutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
# define Z_REGISTER
#endif

#ifndef Z_TLS
# define Z_TLS
#endif

#include <stddef.h>
#include <string.h>
#include <stdlib.h>
Expand Down

0 comments on commit 7f25452

Please sign in to comment.