Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Reverse lower 16 bits, instead of swapping lower 2 bytes
  • Loading branch information
zhengyu123 committed Jun 12, 2025
commit a3d2477d439f2c865e29fe46a956a7ad4a283d43
23 changes: 23 additions & 0 deletions ddprof-lib/src/main/cpp/reverse_bits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Borrow the implementation from openjdk
// https://github.com/openjdk/jdk/blob/master/src/hotspot/share/utilities/reverse_bits.hpp
//

#ifndef REVERSE_BITS_H
#define REVERSE_BITS_H
#include "arch_dd.h"
#include <stdint.h>

static constexpr u32 rep_5555 = static_cast<u32>(UINT64_C(0x5555555555555555));
static constexpr u32 rep_3333 = static_cast<u32>(UINT64_C(0x3333333333333333));
static constexpr u32 rep_0F0F = static_cast<u32>(UINT64_C(0x0F0F0F0F0F0F0F0F));

inline u16 reverse16(u16 v) {
u32 x = static_cast<u32>(v);
x = ((x & rep_5555) << 1) | ((x >> 1) & rep_5555);
x = ((x & rep_3333) << 2) | ((x >> 2) & rep_3333);
x = ((x & rep_0F0F) << 4) | ((x >> 4) & rep_0F0F);
return __builtin_bswap16(static_cast<u16>(x));
}

#endif //REVERSE_BITS_H
3 changes: 2 additions & 1 deletion ddprof-lib/src/main/cpp/threadFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "threadFilter.h"
#include "counters.h"
#include "os.h"
#include "reverse_bits.h"
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -89,7 +90,7 @@ int ThreadFilter::mapThreadId(int thread_id) {
// We want to map the thread_id inside the same bitmap
static_assert(BITMAP_SIZE >= (u16)0xffff, "Potential verflow");
u16 lower16 = (u16)(thread_id & 0xffff);
lower16 = ((lower16 & 0x00ff) << 8) | ((lower16 & 0xff00) >> 8);
lower16 = reverse16(lower16);
int tid = (thread_id & ~0xffff) | lower16;
return tid;
}
Expand Down
Loading