Skip to content

Commit 147c080

Browse files
committed
Added negate and copy sign functions
1 parent 4d0d477 commit 147c080

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

include/nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ struct bitReverse_helper<Vector NBL_PARTIAL_REQ_BOT(hlsl::is_integral_v<Vector>
327327
Vector output;
328328
using traits = hlsl::vector_traits<Vector>;
329329
for (uint32_t i = 0; i < traits::Dimension; ++i)
330-
output[i] = bitReverse_helper<scalar_type_t<Vector> >::__call(vec[i]);
330+
output[i] = bitReverse_helper<traits::scalar_type >::__call(vec[i]);
331331
return output;
332332
#endif
333333
}
@@ -360,6 +360,41 @@ struct mul_helper
360360
}
361361
};
362362

363+
template<typename T NBL_STRUCT_CONSTRAINABLE>
364+
struct negate_helper;
365+
366+
template<typename FloatingPoint>
367+
NBL_PARTIAL_REQ_TOP(hlsl::is_floating_point_v<FloatingPoint> && hlsl::is_scalar_v<FloatingPoint>)
368+
struct negate_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(hlsl::is_floating_point_v<FloatingPoint> && hlsl::is_scalar_v<FloatingPoint>) >
369+
{
370+
static inline FloatingPoint __call(NBL_CONST_REF_ARG(FloatingPoint) val)
371+
{
372+
#ifdef __HLSL_VERSION
373+
return spirv::fNegate(val);
374+
#else
375+
return -val;
376+
#endif
377+
}
378+
};
379+
380+
template<typename Vector>
381+
NBL_PARTIAL_REQ_TOP(hlsl::is_vector_v<Vector>)
382+
struct negate_helper<Vector NBL_PARTIAL_REQ_BOT(hlsl::is_floating_point_v<Vector> && hlsl::is_vector_v<Vector>) >
383+
{
384+
static Vector __call(NBL_CONST_REF_ARG(Vector) vec)
385+
{
386+
#ifdef __HLSL_VERSION
387+
return spirv::fNegate(vec);
388+
#else
389+
Vector output;
390+
using traits = hlsl::vector_traits<Vector>;
391+
for (uint32_t i = 0; i < traits::Dimension; ++i)
392+
output[i] = negate_helper<traits::scalar_type >::__call(vec[i]);
393+
return output;
394+
#endif
395+
}
396+
};
397+
363398
}
364399
}
365400
}

include/nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ inline Integer bitReverse(Integer val)
152152
return cpp_compat_intrinsics_impl::bitReverse_helper<Integer>::__call(val);
153153
}
154154

155+
template<typename FloatingPoint>
156+
inline FloatingPoint negate(FloatingPoint val)
157+
{
158+
return cpp_compat_intrinsics_impl::negate_helper<FloatingPoint>::__call(val);
159+
}
155160

156161
}
157162
}

include/nbl/builtin/hlsl/ieee754.hlsl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ NBL_CONSTEXPR_INLINE_FUNC typename unsigned_integer_of_size<sizeof(T)>::type ext
130130
return ieee754::impl::bitCastToUintType(x) & traits<AsFloat>::signMask;
131131
}
132132

133+
template <typename FloatingPoint NBL_FUNC_REQUIRES(hlsl::is_floating_point_v<FloatingPoint> && hlsl::is_scalar_v<FloatingPoint>)
134+
NBL_CONSTEXPR_INLINE_FUNC FloatingPoint copySign(FloatingPoint to, FloatingPoint from)
135+
{
136+
using AsUint = typename unsigned_integer_of_size<sizeof(FloatingPoint)>::type;
137+
138+
const AsUint toAsUint = ieee754::impl::bitCastToUintType(to);
139+
const AsUint fromAsUint = ieee754::impl::bitCastToUintType(from);
140+
141+
return bit_cast<FloatingPoint>(toAsUint | extractSignPreserveBitPattern(from));
142+
}
143+
133144
}
134145
}
135146
}

include/nbl/builtin/hlsl/spirv_intrinsics/core.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ template<typename Matrix>
246246
[[vk::ext_instruction( spv::OpTranspose )]]
247247
Matrix transpose(Matrix mat);
248248

249+
template<typename FloatingPoint>
250+
[[vk::ext_instruction(spv::OpFNegate)]]
251+
enable_if_t<is_floating_point_v<FloatingPoint>, FloatingPoint> fNegate(FloatingPoint mat);
252+
249253
}
250254

251255
#endif

include/nbl/builtin/hlsl/tgmath.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(
9898
}
9999

100100
template<typename FloatingPoint NBL_FUNC_REQUIRES(hlsl::is_floating_point_v<FloatingPoint>)
101-
inline bool isnan(NBL_CONST_REF_ARG(FloatingPoint) val)
101+
inline bool isnan(NBL_CONST_REF_ARG(FloatingPoint) val)
102102
{
103103
#ifdef __HLSL_VERSION
104104
return spirv::isNan<FloatingPoint>(val);

0 commit comments

Comments
 (0)