Skip to content

Commit

Permalink
[8_11] optimize performance of as_hexadecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
jingkaimori authored Jun 10, 2024
1 parent 85ba510 commit 62d620e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bench/lolly/data/numeral_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ main () {
bench.complexityN (d);
bench.run ("convert hexadecimal string to int",
[&] { from_hex (hex_string); });
int hex_number= (0x1 << (d + 1)) - 1;
int hex_number= (0x1 << ((d + 1) * 4)) - 1;
bench.run ("convert signed int to hexadecimal string",
[&] { to_Hex (hex_number); });
bench.run ("convert unsigned int to hexadecimal string",
Expand Down
62 changes: 57 additions & 5 deletions lolly/data/numeral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,72 @@ from_hex (string s) {
* to the tail.
* @tparam T unsigned integral type is expected.
*/
template <typename T>
template <unsigned int cur, typename T>
std::enable_if_t<std::conjunction_v<std::is_integral<T>, std::is_unsigned<T>>,
void>
as_hexadecimal_sub (T i, unsigned int cur, string& s) {
if (cur > 0) {
as_hexadecimal_sub (i >> 4, cur - 1, s);
as_hexadecimal_sub (T i, string& s) {
if constexpr (cur > 0) {
as_hexadecimal_sub<cur - 1> (i >> 4, s);
}
s[cur]= hex_string[i & 15];
}

string
as_hexadecimal (int i, int len) {
string result (len);
as_hexadecimal_sub ((unsigned) i, len - 1, result);
switch (len) {
case 1:
as_hexadecimal_sub<0> ((unsigned int) i, result);
break;
case 2:
as_hexadecimal_sub<1> ((unsigned int) i, result);
break;
case 3:
as_hexadecimal_sub<2> ((unsigned int) i, result);
break;
case 4:
as_hexadecimal_sub<3> ((unsigned int) i, result);
break;
case 5:
as_hexadecimal_sub<4> ((unsigned int) i, result);
break;
case 6:
as_hexadecimal_sub<5> ((unsigned int) i, result);
break;
case 7:
as_hexadecimal_sub<6> ((unsigned int) i, result);
break;
case 8:
as_hexadecimal_sub<7> ((unsigned int) i, result);
break;
case 9:
as_hexadecimal_sub<8> ((unsigned int) i, result);
break;
case 10:
as_hexadecimal_sub<9> ((unsigned int) i, result);
break;
case 11:
as_hexadecimal_sub<10> ((unsigned int) i, result);
break;
case 12:
as_hexadecimal_sub<11> ((unsigned int) i, result);
break;
case 13:
as_hexadecimal_sub<12> ((unsigned int) i, result);
break;
case 14:
as_hexadecimal_sub<13> ((unsigned int) i, result);
break;
case 15:
as_hexadecimal_sub<14> ((unsigned int) i, result);
break;
case 16:
as_hexadecimal_sub<15> ((unsigned int) i, result);
break;
default:
TM_FAILED ("len is too large");
break;
}
return result;
}

Expand Down

0 comments on commit 62d620e

Please sign in to comment.