From 1ad1e0ebf58da30ee7f65a3a1f2a8f62900c47d5 Mon Sep 17 00:00:00 2001 From: KunoiSayami Date: Wed, 17 May 2023 14:08:08 +0800 Subject: fix: Fix fast_log Signed-off-by: KunoiSayami --- sortlib.cuh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'sortlib.cuh') diff --git a/sortlib.cuh b/sortlib.cuh index fa306b0..5d32e4b 100644 --- a/sortlib.cuh +++ b/sortlib.cuh @@ -17,10 +17,12 @@ public: const int MOVE_OFFSET; const unsigned int STEP_LIMIT; - // IEEE 754 __device__ __host__ static size_t fast_log(size_t a) { - float t = a; - return (((*(int *)&t) >> 23) + 1) & 127; +#ifdef __CUDA_ARCH__ + return log2((double)a); +#else + return std::log2(a); +#endif } __device__ __host__ size_t calculate_index(size_t rank) const { @@ -38,7 +40,11 @@ public: for (size_t i = 0; i < LENGTH; i++) { auto left = calculate_index(i); auto right = calculate_rank(left - 1); - + if (i + 1 == LENGTH || i + 1 != right) { + printf("%lu %lu %lu\n", i, left, right); + printf("%lu %lu %lu\n", LENGTH, fast_log(i + 1), + (LENGTH + 1) >> fast_log(i + 1) >> 1); + } assert(i + 1 == right); } } @@ -176,6 +182,11 @@ __global__ void testCustomCalculation(size_t length) { CustomSort(length, sizeof(long) * 8).testCalculation(); } +__global__ void testCustomCalculationWithPrint(size_t length) { + CustomSort(length, sizeof(long) * 8).testCalculation(); + printf("%lu pass\n", length); +} + class FactorySort { public: -- cgit v1.3.1