Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8_11] optimize performance of as_hexadecimal #334

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading