Skip to content

Commit

Permalink
platform.h: fully define bitcast template (#1635)
Browse files Browse the repository at this point in the history
Ported from OIIO. This lets us not have to depend on the specific
definition in OIIO's fmath.h, and in particular not worry about
whether it's a new enough version of OIIO that the old bit_cast is now
marked as deprecated.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
  • Loading branch information
lgritz committed Jan 13, 2023
1 parent 8036e17 commit 66c9b61
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
7 changes: 1 addition & 6 deletions src/include/OSL/oslnoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,7 @@ inline OSL_HOSTDEVICE float bits_to_01 (unsigned int bits) {
OSL_FORCEINLINE OSL_HOSTDEVICE int
bitcast_to_uint (float x)
{
#if OPENIMAGEIO_VERSION >= 20500
return OIIO::bitcast<unsigned int, float>(x);
#else
// obsolete call
return OIIO::bit_cast<float, unsigned int>(x);
#endif
return OSL::bitcast<unsigned int, float>(x);
}


Expand Down
39 changes: 39 additions & 0 deletions src/include/OSL/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include <OSL/oslversion.h>

#if defined(__x86_64__) && !defined(__CUDA_ARCH__)
# include <x86intrin.h>
#endif

/////////////////////////////////////////////////////////////////////////
// Detect which compiler and version we're using
Expand Down Expand Up @@ -516,6 +519,42 @@ OSL_FORCEINLINE OSL_HOSTDEVICE To bitcast(const From& src) noexcept {
return dst;
}

#if defined(__x86_64__) && !defined(__CUDA_ARCH__) && \
(defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER) \
|| OSL_CLANG_VERSION >= 100000 || OSL_APPLE_CLANG_VERSION >= 130000)
// On x86/x86_64 for certain compilers we can use Intel CPU intrinsics for
// some common bitcast cases that might be even more understandable to the
// compiler and generate better code without its getting confused about the
// memcpy in the general case. We're a bit conservative with the compiler
// version checks here, it may be that some earlier versions support these
// intrinsics.

template<> OSL_FORCEINLINE uint32_t bitcast<uint32_t, float>(const float& val) noexcept {
return static_cast<uint32_t>(_castf32_u32(val));
}
template<> OSL_FORCEINLINE int32_t bitcast<int32_t, float>(const float& val) noexcept {
return static_cast<int32_t>(_castf32_u32(val));
}
template<> OSL_FORCEINLINE float bitcast<float, uint32_t>(const uint32_t& val) noexcept {
return _castu32_f32(val);
}
template<> OSL_FORCEINLINE float bitcast<float, int32_t>(const int32_t& val) noexcept {
return _castu32_f32(val);
}
template<> OSL_FORCEINLINE uint64_t bitcast<uint64_t, double>(const double& val) noexcept {
return static_cast<uint64_t>(_castf64_u64(val));
}
template<> OSL_FORCEINLINE int64_t bitcast<int64_t, double>(const double& val) noexcept {
return static_cast<int64_t>(_castf64_u64(val));
}
template<> OSL_FORCEINLINE double bitcast<double, uint64_t>(const uint64_t& val) noexcept {
return _castu64_f64(val);
}
template<> OSL_FORCEINLINE double bitcast<double, int64_t>(const int64_t& val) noexcept {
return _castu64_f64(val);
}
#endif



#if OSL_CPLUSPLUS_VERSION >= 20
Expand Down
7 changes: 1 addition & 6 deletions src/include/OSL/wide.h
Original file line number Diff line number Diff line change
Expand Up @@ -980,12 +980,7 @@ template<int WidthT> struct alignas(VecReg<WidthT>) Block<ustringhash, WidthT> {
#ifdef OIIO_USTRING_HAS_CTR_FROM_USTRINGHASH
return ustringhash::from_hash(str[lane]);
#else
// Dumb workaround if we are on old OIIO
# if OPENIMAGEIO_VERSION >= 20500
return OIIO::bitcast<ustringhash>(str[lane]);
# else
return OIIO::bit_cast<size_t, ustringhash>(str[lane]);
# endif
return OSL::bitcast<ustringhash>(str[lane]);
#endif
}

Expand Down
7 changes: 1 addition & 6 deletions src/liboslexec/wide/wide_ophash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ namespace {
OSL_FORCEINLINE OSL_HOSTDEVICE int
bitcast_to_uint(float x)
{
#if OPENIMAGEIO_VERSION >= 20500
return OIIO::bitcast<unsigned int, float>(x);
#else
// obsolete call
return OIIO::bit_cast<float, unsigned int>(x);
#endif
return OSL::bitcast<unsigned int>(x);
}


Expand Down

0 comments on commit 66c9b61

Please sign in to comment.