some fixes for ubsan

This commit is contained in:
dzaima 2022-07-10 01:42:32 +03:00
parent 9a00da6369
commit 0684fe5fcb
3 changed files with 16 additions and 12 deletions

View File

@ -326,6 +326,8 @@ B pick_c2(B t, B w, B x) {
#ifdef __BMI2__
#include <immintrin.h>
void storeu_u64(u64* p, u64 v) { memcpy(p, &v, 8); }
u64 loadu_u64(u64* p) { u64 v; memcpy(&v, p, 8); return v; }
#if SINGELI
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"

View File

@ -1,11 +1,13 @@
include './base'
include './bmi2'
def storeu{p:T, i, v:eltype{T} & *u64==T} = emit{void, 'storeu_u64', p+i, v}
def loadu{p:T & *u64==T} = emit{eltype{T}, 'loadu_u64', p}
def comp8{w:*u64, X, r:*i8, l:u64} = {
@for(w in reinterpret{*u8,w} over i to cdiv{l,8}) {
@for(w in *u8~~w over i to cdiv{l,8}) {
pc:= popc{w}
store{reinterpret{*u64,r}, 0, pext{promote{u64,X{}}, pdep{promote{u64, w}, cast{u64,0x0101010101010101}}*255}}
storeu{*u64~~r, 0, pext{promote{u64,X{}}, pdep{promote{u64, w}, cast{u64,0x0101010101010101}}*255}}
r+= pc
}
}
@ -19,10 +21,10 @@ def tab{n==0,l} = tup{0}
c16lut:*u64 = tab{4,16}
def comp16{w:*u64, X, r:*i16, l:u64} = {
@for(w in reinterpret{*u8,w} over i to cdiv{l,8}) {
@for(w in *u8~~w over i to cdiv{l,8}) {
def step{w} = {
pc:= popc{w}
store{reinterpret{*u64,r}, 0, pext{promote{u64,X{}}, load{c16lut, w}}} # TODO don't use unaligned store to make valgrind/ubsan happy
storeu{*u64~~r, 0, pext{promote{u64,X{}}, load{c16lut, w}}}
r+= pc
}
step{w&15}
@ -31,8 +33,8 @@ def comp16{w:*u64, X, r:*i16, l:u64} = {
}
slash2{F, T}(w:*u64, x:*T, r:*T, l:u64) : void = {
xv:= reinterpret{*u64, x}
F{w, {} => {c:= *xv; xv+= 1; c}, r, l}
xv:= *u64~~x
F{w, {} => {c:= loadu{xv}; xv+= 1; c}, r, l}
}
slash1{F, T, iota, add}(w:*u64, r:*T, l:u64) : void = {

View File

@ -127,11 +127,11 @@ static void bit_cpy(u64* r, usz rs, u64* x, usz xs, usz l) {
u64 ti = rs>>6;
u64 ei = re>>6;
i64 dp = d>>6;
u64 dp = (u64)(d>>6);
u64 df = ((u64)d)&63u;
#define RDF0 x[ti+dp]
#define RDFp ((x[ti+dp] >> df) | (x[ti+dp+1] << (64-df)))
#define READ (df==0? RDF0 : RDFp)
#define RDFo(N) *(x + (i64)(ti+dp+N))
#define RDFp ((RDFo(0) >> df) | (RDFo(1) << (64-df)))
#define READ (df==0? RDFo(0) : RDFp)
if (ti!=ei) {
if (rs&63) {
u64 m = (1ULL << (rs&63))-1;
@ -139,7 +139,7 @@ static void bit_cpy(u64* r, usz rs, u64* x, usz xs, usz l) {
ti++;
}
if (df==0) for (; ti<ei; ti++) r[ti] = RDF0;
if (df==0) for (; ti<ei; ti++) r[ti] = RDFo(0);
else for (; ti<ei; ti++) r[ti] = RDFp;
if (re&63) {
@ -153,7 +153,7 @@ static void bit_cpy(u64* r, usz rs, u64* x, usz xs, usz l) {
}
#undef READ
#undef RDFp
#undef RDF0
#undef RDFo
}
B vec_join(B w, B x); // consumes both