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*.
This commit is contained in:
Alexandre Boulgakov
2023-04-24 22:31:11 +01:00
parent 36d34f199b
commit 621761b289
2 changed files with 5 additions and 4 deletions

View File

@@ -50,5 +50,6 @@ swift_cc_library(
deps = [
"//swift/extractor/infra/file",
"//swift/extractor/infra/log",
"@absl//absl/numeric:bits",
],
)

View File

@@ -5,6 +5,7 @@
#include <iostream>
#include <string>
#include <vector>
#include "absl/numeric/bits.h"
#include <binlog/binlog.hpp>
#include <cmath>
#include <charconv>
@@ -53,10 +54,9 @@ class UntypedTrapLabel {
private:
size_t strSize() const {
if (id_ == undefined) return 17; // #ffffffffffffffff
if (id_ == 0) return 2; // #0
// TODO: use absl::bit_width or C+20 std::bit_width instead of this ugly formula
return /* # */ 1 + /* hex digits */ static_cast<size_t>(ceil(log2(id_ + 1) / 4));
if (id_ == 0) return 2; // #0
// Number of hex digits is ceil(bit_width(id) / 4), but C++ integer division can only do floor.
return /* # */ 1 + /* hex digits */ 1 + (absl::bit_width(id_) - 1) / 4;
}
};