Skip to content

Commit

Permalink
Use std::countl_zero and std::countr_zero
Browse files Browse the repository at this point in the history
  • Loading branch information
baldersheim committed Aug 20, 2024
1 parent 9613905 commit 5c1c8e5
Showing 1 changed file with 8 additions and 51 deletions.
59 changes: 8 additions & 51 deletions vespalib/src/vespa/vespalib/util/optimized.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@

#pragma once

#include <cstdint>
#include <bit>

namespace vespalib {

/**
* @brief Optimized instructions class
*
* Here are fast handcoded assembly functions for doing some special
* low level functions that can be carried out very fast by special instructions.
* Currently only implemented for GCC on i386 and x86_64 platforms.
**/
class Optimized
{
public:
static int msbIdx(unsigned int v) noexcept;
static int msbIdx(unsigned long v) noexcept;
static int msbIdx(unsigned long long v) noexcept;
static int lsbIdx(unsigned int v) noexcept;
static int lsbIdx(unsigned long v) noexcept;
static int lsbIdx(unsigned long long v) noexcept;

static constexpr int msbIdx(unsigned int v) noexcept { return v ? sizeof(unsigned int) * 8 - 1 - std::countl_zero(v) : 0; }
static constexpr int msbIdx(unsigned long v) noexcept { return v ? sizeof(unsigned long) * 8 - 1 - std::countl_zero(v) : 0; }
static constexpr int msbIdx(unsigned long long v) noexcept { return v ? sizeof(unsigned long long) * 8 - 1 - std::countl_zero(v) : 0; }
static constexpr int lsbIdx(unsigned int v) noexcept { return v ? std::countr_zero(v) : 0; }
static constexpr int lsbIdx(unsigned long v) noexcept { return v ? std::countr_zero(v) : 0; }
static constexpr int lsbIdx(unsigned long long v) noexcept { return v ? std::countr_zero(v) : 0; }
};

/**
Expand Down Expand Up @@ -60,46 +57,6 @@ class Optimized
* @return index [0-63] of lsb, 0 if none.
**/

#ifdef __x86_64__
inline int Optimized::msbIdx(unsigned int v) noexcept {
unsigned int result;
__asm __volatile("bsrl %0,%0" : "=r" (result) : "0" (v));
return result;
}
inline int Optimized::lsbIdx(unsigned int v) noexcept {
unsigned int result;
__asm __volatile("bsfl %0,%0" : "=r" (result) : "0" (v));
return result;
}
inline int Optimized::msbIdx(unsigned long v) noexcept {
unsigned long result;
__asm __volatile("bsrq %0,%0" : "=r" (result) : "0" (v));
return result;
}
inline int Optimized::lsbIdx(unsigned long v) noexcept {
unsigned long result;
__asm __volatile("bsfq %0,%0" : "=r" (result) : "0" (v));
return result;
}
inline int Optimized::msbIdx(unsigned long long v) noexcept {
unsigned long long result;
__asm __volatile("bsrq %0,%0" : "=r" (result) : "0" (v));
return result;
}
inline int Optimized::lsbIdx(unsigned long long v) noexcept {
unsigned long long result;
__asm __volatile("bsfq %0,%0" : "=r" (result) : "0" (v));
return result;
}
#else
inline int Optimized::msbIdx(unsigned int v) noexcept { return v ? sizeof(unsigned int) * 8 - 1 - __builtin_clz(v) : 0; }
inline int Optimized::msbIdx(unsigned long v) noexcept { return v ? sizeof(unsigned long) * 8 - 1 - __builtin_clzl(v) : 0; }
inline int Optimized::msbIdx(unsigned long long v) noexcept { return v ? sizeof(unsigned long long) * 8 - 1 - __builtin_clzll(v) : 0; }
inline int Optimized::lsbIdx(unsigned int v) noexcept { return v ? __builtin_ctz(v) : 0; }
inline int Optimized::lsbIdx(unsigned long v) noexcept { return v ? __builtin_ctzl(v) : 0; }
inline int Optimized::lsbIdx(unsigned long long v) noexcept { return v ? __builtin_ctzll(v) : 0; }
#endif

#define VESPA_DLL_LOCAL __attribute__ ((visibility("hidden")))

}
Expand Down

0 comments on commit 5c1c8e5

Please sign in to comment.