Skip to content

Commit 621761b

Browse files
committed
Swift: Use absl::bit_width to calculate TRAP label size.
It's not much cleaner due to arithmetic to convert truncating division to a ceiling, but has two advantages: 1. It doesn't suffer from rounding issues with large TRAP labels. This is largely theoretical, but does let us handle `undefined` uniformly. 2. It should be much faster (using LZCNT/BSR instead of floating point arithmetic). This is probably not a performance bottleneck, so *shrug*.
1 parent 36d34f1 commit 621761b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

swift/extractor/trap/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ swift_cc_library(
5050
deps = [
5151
"//swift/extractor/infra/file",
5252
"//swift/extractor/infra/log",
53+
"@absl//absl/numeric:bits",
5354
],
5455
)

swift/extractor/trap/TrapLabel.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <iostream>
66
#include <string>
77
#include <vector>
8+
#include "absl/numeric/bits.h"
89
#include <binlog/binlog.hpp>
910
#include <cmath>
1011
#include <charconv>
@@ -53,10 +54,9 @@ class UntypedTrapLabel {
5354

5455
private:
5556
size_t strSize() const {
56-
if (id_ == undefined) return 17; // #ffffffffffffffff
57-
if (id_ == 0) return 2; // #0
58-
// TODO: use absl::bit_width or C+20 std::bit_width instead of this ugly formula
59-
return /* # */ 1 + /* hex digits */ static_cast<size_t>(ceil(log2(id_ + 1) / 4));
57+
if (id_ == 0) return 2; // #0
58+
// Number of hex digits is ceil(bit_width(id) / 4), but C++ integer division can only do floor.
59+
return /* # */ 1 + /* hex digits */ 1 + (absl::bit_width(id_) - 1) / 4;
6060
}
6161
};
6262

0 commit comments

Comments
 (0)