Skip to content

Commit 6a45919

Browse files
thibaultchaisagesiddhesh
committed
feature: x86: Runtime detection of SSE4.2 support when compiled with '-msse4.2'.
Co-authored-by: iSage <isage.dna@gmail.com> Co-authored-by: Siddhesh Poyarekar <siddhesh@gotplt.org>
1 parent 7363d43 commit 6a45919

11 files changed

Lines changed: 186 additions & 161 deletions

File tree

src/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,9 @@ LJLIB_O= lib_base.o lib_math.o lib_bit.o lib_string.o lib_table.o \
490490
LJLIB_C= $(LJLIB_O:.o=.c)
491491

492492
LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o lj_buf.o \
493-
lj_str.o lj_tab.o lj_func.o lj_udata.o lj_meta.o lj_debug.o \
494-
lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o lj_strscan.o \
495-
lj_strfmt.o lj_strfmt_num.o lj_api.o lj_profile.o \
493+
lj_str.o lj_str_hash.o lj_tab.o lj_func.o lj_udata.o lj_meta.o \
494+
lj_debug.o lj_state.o lj_dispatch.o lj_vmevent.o lj_vmmath.o \
495+
lj_strscan.o lj_strfmt.o lj_strfmt_num.o lj_api.o lj_profile.o \
496496
lj_lex.o lj_parse.o lj_bcread.o lj_bcwrite.o lj_load.o \
497497
lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \
498498
lj_opt_dce.o lj_opt_loop.o lj_opt_split.o lj_opt_sink.o \

src/lj_arch.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,13 @@ extern void *LJ_WIN_LOADLIBA(const char *path);
615615
#define LJ_52 0
616616
#endif
617617

618+
/* Optimized string hashing, added by OpenResty. */
619+
#if LUAJIT_TARGET == LUAJIT_ARCH_X64 && defined(__GNUC__) && defined(__SSE4_2__)
620+
#ifndef LJ_OR_DISABLE_STRHASHCRC32
621+
#define LJ_OR_STRHASHCRC32 1
622+
#endif
623+
#else
624+
#define LJ_OR_STRHASHCRC32 0
625+
#endif
626+
618627
#endif

src/lj_obj.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ typedef struct SBuf {
160160
MRef L; /* lua_State, used for buffer resizing. */
161161
} SBuf;
162162

163+
#if LJ_OR_STRHASHCRC32
164+
/* String hashing functions, added by OpenResty. */
165+
typedef MSize (*StrHashFunction)(const char *, size_t);
166+
#endif
167+
163168
/* -- Tags and values ----------------------------------------------------- */
164169

165170
/* Frame link. */
@@ -622,6 +627,9 @@ typedef struct global_State {
622627
MRef saved_jit_base; /* saved jit_base for lj_err_throw */
623628
MRef ctype_state; /* Pointer to C type state. */
624629
GCRef gcroot[GCROOT_MAX]; /* GC roots. */
630+
#if LJ_OR_STRHASHCRC32
631+
StrHashFunction strhashfn; /* String hashing function, added by OpenResty */
632+
#endif
625633
} global_State;
626634

627635
#define mainthread(g) (&gcref(g->mainthref)->th)

src/lj_state.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
204204
setgcref(g->uvhead.prev, obj2gco(&g->uvhead));
205205
setgcref(g->uvhead.next, obj2gco(&g->uvhead));
206206
g->strmask = ~(MSize)0;
207+
#if LJ_OR_STRHASHCRC32
208+
lj_init_strhashfn(g);
209+
#endif
207210
setnilV(registry(L));
208211
setnilV(&g->nilnode.val);
209212
setnilV(&g->nilnode.key);

src/lj_str.c

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -130,49 +130,6 @@ void lj_str_resize(lua_State *L, MSize newmask)
130130
g->strhash = newhash;
131131
}
132132

133-
static MSize
134-
lj_str_original_hash(const char *str, size_t lenx)
135-
{
136-
MSize len = (MSize)lenx;
137-
MSize a, b, h = len;
138-
139-
/* Compute string hash. Constants taken from lookup3 hash by Bob Jenkins. */
140-
if (len >= 4) { /* Caveat: unaligned access! */
141-
a = lj_getu32(str);
142-
h ^= lj_getu32(str+len-4);
143-
b = lj_getu32(str+(len>>1)-2);
144-
h ^= b; h -= lj_rol(b, 14);
145-
b += lj_getu32(str+(len>>2)-1);
146-
} else if (len > 0) {
147-
a = *(const uint8_t *)str;
148-
h ^= *(const uint8_t *)(str+len-1);
149-
b = *(const uint8_t *)(str+(len>>1));
150-
h ^= b; h -= lj_rol(b, 14);
151-
} else {
152-
return 0;
153-
}
154-
155-
a ^= h; a -= lj_rol(h, 11);
156-
b ^= a; b -= lj_rol(a, 25);
157-
h ^= b; h -= lj_rol(b, 16);
158-
159-
return h;
160-
}
161-
162-
MSize
163-
lj_str_indep_hash(GCstr *str)
164-
{
165-
return lj_str_original_hash(strdata(str), str->len);
166-
}
167-
168-
#include "x64/src/lj_str_hash_x64.h"
169-
170-
#if defined(LJ_ARCH_STR_HASH)
171-
#define LJ_STR_HASH LJ_ARCH_STR_HASH
172-
#else
173-
#define LJ_STR_HASH lj_str_original_hash
174-
#endif
175-
176133
/* Intern a string and return string object. */
177134
GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
178135
{
@@ -189,7 +146,12 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx)
189146
return &g->strempty;
190147
}
191148

192-
h = LJ_STR_HASH(str, lenx);
149+
#if LJ_OR_STRHASHCRC32
150+
lua_assert(g->strhashfn != NULL);
151+
h = g->strhashfn(str, lenx);
152+
#else
153+
h = lj_str_hash_orig(str, lenx);
154+
#endif
193155

194156
/* Check if the string has already been interned. */
195157
o = gcref(g->strhash[h & g->strmask]);

src/lj_str.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdarg.h>
1010

1111
#include "lj_obj.h"
12+
#include "lj_str_hash.h"
1213

1314
/* String helpers. */
1415
LJ_FUNC int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b);
@@ -23,7 +24,6 @@ LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s);
2324

2425
#define lj_str_newz(L, s) (lj_str_new(L, s, strlen(s)))
2526
#define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1))
26-
27-
MSize lj_str_indep_hash(GCstr *str);
27+
#define lj_str_indep_hash(s) (lj_str_hash_orig(strdata(s), s->len))
2828

2929
#endif

0 commit comments

Comments
 (0)