Skip to content

Commit

Permalink
fix cast of vectypes with sizeof < vec4f (16)
Browse files Browse the repository at this point in the history
Do use memcpy-to-temp-var on clang cast as it's portable, correct and fast
and use various type of loads for other compilers (msvc/gcc) to match
generated code on clang

This fixes clang18 binding of inline functions that return vec3/Point3
and removes special code path for TSAN.
  • Loading branch information
Vladimir 'virtul' Ivannikov committed Dec 28, 2024
1 parent 42b5ccb commit e413593
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions include/daScript/simulate/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ namespace das
}
};

template <typename PT>
struct prune<vec4f, PT> {
static __forceinline vec4f from(const PT & v) {
static_assert(sizeof(PT) <= sizeof(vec4f), "type too big to be pruned");
#ifdef __clang__ // Clang does the right thing without need to micro-optimize stuff
vec4f r = v_zero();
memcpy(&r, &v, sizeof(PT));
return r;
#else
if constexpr (sizeof(PT) == sizeof(vec4f))
return v_ldu((const float*)&v);
else if constexpr (sizeof(PT) == sizeof(float) * 3)
return v_ldu_p3_safe((const float*)&v);
else if constexpr (sizeof(PT) == sizeof(float) * 2)
return v_ldu_half((const float*)&v);
else
{
static_assert(sizeof(PT) == sizeof(float));
return v_set_x(*(const float*)&v);
}
#endif
}
};

template <typename PT, typename VT>
struct das_alias_ref {
static __forceinline VT & from ( PT & value ) {
Expand Down Expand Up @@ -341,15 +365,7 @@ namespace das
return prune<TT,vec4f>::from(x);
}
static __forceinline vec4f from ( const TT & x ) {
#if __SANITIZE_THREAD__
if constexpr ( sizeof(TT) != sizeof(vec4f) )
{
vec4f v = v_zero(); // Init to prevent uninitialized warnings
memcpy(&v, &x, sizeof(x));
return v;
}
#endif
return v_ldu((const float*)&x);
return prune<vec4f,TT>::from(x);
}
};

Expand All @@ -359,7 +375,7 @@ namespace das
return prune<TT,vec4f>::from(x);
}
static __forceinline vec4f from ( const TT & x ) {
return v_ldu_half((const float*)&x);
return prune<vec4f,TT>::from(x);
}
};

Expand All @@ -373,15 +389,7 @@ namespace das
return prune<TT,vec4f>::from(x);
}
static __forceinline vec4f from ( const TT & x ) {
#if __SANITIZE_THREAD__
if constexpr ( sizeof(TT) != sizeof(vec4f) )
{
vec4f v = v_zero(); // Init to prevent uninitialized warnings
memcpy(&v, &x, sizeof(x));
return v;
}
#endif
return v_cast_vec4f(v_ldui((const int*)&x));
return prune<vec4f,TT>::from(x);
}
};

Expand All @@ -391,7 +399,7 @@ namespace das
return prune<TT,vec4f>::from(x);
}
static __forceinline vec4f from ( const TT & x ) {
return v_cast_vec4f(v_ldui_half((const int*)&x));
return prune<vec4f,TT>::from(x);
}
};

Expand Down

0 comments on commit e413593

Please sign in to comment.