Avoid cast in PtrDiff

ptrdiff_t is the return type
pull/687/head
Jeffrey Walton 2018-07-10 11:55:50 -04:00
parent 9748c3cf3e
commit 722d3e38c1
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
1 changed files with 16 additions and 16 deletions

32
misc.h
View File

@ -386,21 +386,6 @@ inline PTR PtrSub(PTR pointer, OFF offset)
return pointer-static_cast<ptrdiff_t>(offset); return pointer-static_cast<ptrdiff_t>(offset);
} }
/// \brief Determine pointer difference
/// \tparam PTR a pointer type
/// \param pointer1 the first pointer
/// \param pointer2 the second pointer
/// \details PtrByteDiff can be used to squash Clang and GCC
/// UBsan findings for pointer addition and subtraction.
/// pointer1 and pointer2 must point to the same object or
/// array (or one past the end), and yields the number of
/// bytes (not elements) difference.
template <typename PTR>
inline uintptr_t PtrByteDiff(const PTR pointer1, const PTR pointer2)
{
return static_cast<uintptr_t>(pointer1) - static_cast<uintptr_t>(pointer2);
}
/// \brief Determine pointer difference /// \brief Determine pointer difference
/// \tparam PTR a pointer type /// \tparam PTR a pointer type
/// \param pointer1 the first pointer /// \param pointer1 the first pointer
@ -413,7 +398,22 @@ inline uintptr_t PtrByteDiff(const PTR pointer1, const PTR pointer2)
template <typename PTR> template <typename PTR>
inline ptrdiff_t PtrDiff(const PTR pointer1, const PTR pointer2) inline ptrdiff_t PtrDiff(const PTR pointer1, const PTR pointer2)
{ {
return static_cast<ptrdiff_t>(pointer1 - pointer2); return pointer1 - pointer2;
}
/// \brief Determine pointer difference
/// \tparam PTR a pointer type
/// \param pointer1 the first pointer
/// \param pointer2 the second pointer
/// \details PtrByteDiff can be used to squash Clang and GCC
/// UBsan findings for pointer addition and subtraction.
/// pointer1 and pointer2 must point to the same object or
/// array (or one past the end), and yields the number of
/// bytes (not elements) difference.
template <typename PTR>
inline size_t PtrByteDiff(const PTR pointer1, const PTR pointer2)
{
return (size_t)(static_cast<uintptr_t>(pointer1) - static_cast<uintptr_t>(pointer2));
} }
#if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB) #if (!__STDC_WANT_SECURE_LIB__ && !defined(_MEMORY_S_DEFINED)) || defined(CRYPTOPP_WANT_SECURE_LIB)