Skip to content

Commit a3d2477

Browse files
committed
Reverse lower 16 bits, instead of swapping lower 2 bytes
1 parent bb2a116 commit a3d2477

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Borrow the implementation from openjdk
3+
// https://github.com/openjdk/jdk/blob/master/src/hotspot/share/utilities/reverse_bits.hpp
4+
//
5+
6+
#ifndef REVERSE_BITS_H
7+
#define REVERSE_BITS_H
8+
#include "arch_dd.h"
9+
#include <stdint.h>
10+
11+
static constexpr u32 rep_5555 = static_cast<u32>(UINT64_C(0x5555555555555555));
12+
static constexpr u32 rep_3333 = static_cast<u32>(UINT64_C(0x3333333333333333));
13+
static constexpr u32 rep_0F0F = static_cast<u32>(UINT64_C(0x0F0F0F0F0F0F0F0F));
14+
15+
inline u16 reverse16(u16 v) {
16+
u32 x = static_cast<u32>(v);
17+
x = ((x & rep_5555) << 1) | ((x >> 1) & rep_5555);
18+
x = ((x & rep_3333) << 2) | ((x >> 2) & rep_3333);
19+
x = ((x & rep_0F0F) << 4) | ((x >> 4) & rep_0F0F);
20+
return __builtin_bswap16(static_cast<u16>(x));
21+
}
22+
23+
#endif //REVERSE_BITS_H

ddprof-lib/src/main/cpp/threadFilter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "threadFilter.h"
1818
#include "counters.h"
1919
#include "os.h"
20+
#include "reverse_bits.h"
2021
#include <stdlib.h>
2122
#include <string.h>
2223

@@ -89,7 +90,7 @@ int ThreadFilter::mapThreadId(int thread_id) {
8990
// We want to map the thread_id inside the same bitmap
9091
static_assert(BITMAP_SIZE >= (u16)0xffff, "Potential verflow");
9192
u16 lower16 = (u16)(thread_id & 0xffff);
92-
lower16 = ((lower16 & 0x00ff) << 8) | ((lower16 & 0xff00) >> 8);
93+
lower16 = reverse16(lower16);
9394
int tid = (thread_id & ~0xffff) | lower16;
9495
return tid;
9596
}

0 commit comments

Comments
 (0)