Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit 69ed400

Browse files
committed
Check for signedness of char when performing key conversion in CUDA's radix sort.
1 parent ef58122 commit 69ed400

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

thrust/system/cuda/detail/detail/b40c/radixsort_key_conversion.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,33 @@ struct PostprocessKeyFunctor<double> {
164164
//
165165

166166
template <> struct KeyConversion<char> {
167-
typedef unsigned char UnsignedBits;
167+
typedef unsigned char UnsignedBits;
168168
};
169169

170170
template <>
171171
struct PreprocessKeyFunctor<char> {
172-
__device__ __host__ __forceinline__ void operator()(unsigned char &converted_key) {
173-
const unsigned int SIGN_MASK = 1u << ((sizeof(char) * 8) - 1);
174-
converted_key ^= SIGN_MASK;
175-
}
176-
__device__ __host__ __forceinline__ static bool MustApply(){ return true;}
172+
__device__ __host__ __forceinline__ void operator()(unsigned char &converted_key) {
173+
// char is unsigned on some platforms, so we have to check
174+
if(std::numeric_limits<char>::is_signed)
175+
{
176+
const unsigned int SIGN_MASK = 1u << ((sizeof(char) * 8) - 1);
177+
converted_key ^= SIGN_MASK;
178+
}
179+
}
180+
__device__ __host__ __forceinline__ static bool MustApply(){ return std::numeric_limits<char>::is_signed;}
177181
};
178182

179183
template <>
180184
struct PostprocessKeyFunctor<char> {
181-
__device__ __host__ __forceinline__ void operator()(unsigned char &converted_key) {
182-
const unsigned int SIGN_MASK = 1u << ((sizeof(char) * 8) - 1);
183-
converted_key ^= SIGN_MASK;
185+
__device__ __host__ __forceinline__ void operator()(unsigned char &converted_key) {
186+
// char is unsigned on some platforms, so we have to check
187+
if(std::numeric_limits<char>::is_signed)
188+
{
189+
const unsigned int SIGN_MASK = 1u << ((sizeof(char) * 8) - 1);
190+
converted_key ^= SIGN_MASK;
184191
}
185-
__device__ __host__ __forceinline__ static bool MustApply(){ return true;}
192+
}
193+
__device__ __host__ __forceinline__ static bool MustApply(){ return std::numeric_limits<char>::is_signed;}
186194
};
187195

188196

0 commit comments

Comments
 (0)