What's the fastest way to implement
template <typename T>
unsigned highest_decimal_digit(T x);
(which returns e.g. 3 for 356431, 7 for 71 and 9 for 9)?
The best I can think of is:
- constexpr-calculate the "middle-size" power of 10 which fits into T.
- perform a binary search (over the powers of 10, possibly using a constexpr-constructed lookup table) to find p, the highest power-of-10 lower than x.
- return x divided by p
... but maybe there's another approach.
Notes:
- I expressed the question and my approach in C++14ish terms, and a solution in code would be nice, but an abstract solution (or even a solution in x86_64 assembly) would be fine. I do want something that will work for all (unsigned) integer types, though.
- You may ignore signed integral types.
- I didn't specify what "fast" is, but be hardware-conscious please.