core: Get doublings in trickle-timer the elegant way

This commit is contained in:
André Apitzsch
2024-02-05 11:53:29 +01:00
committed by Tommaso Pecorella
parent 3e5ac445dc
commit 7b717ff485
2 changed files with 5 additions and 14 deletions

View File

@@ -21,6 +21,7 @@
#include "log.h"
#include <bit>
#include <limits>
namespace ns3
@@ -116,20 +117,7 @@ TrickleTimer::GetDoublings() const
return 0;
}
// Here we assume that m_ticks is a power of 2.
// This could have been way more elegant by using
// std::countl_zero() defined in the <bit> header
// which is c++20 - so not yet widely available.
uint64_t ticks = m_ticks;
uint8_t doublings = 0;
while (ticks != 1)
{
ticks >>= 1;
doublings++;
}
return doublings;
return std::countr_zero(m_ticks);
}
uint16_t

View File

@@ -85,6 +85,9 @@ Network Animator nam. It is not possible to run a simulation
./src/core/examples/hash-example.cc
6ed21de2 brand-newness peripherial
./src/core/model/trickle-timer.cc:120
return std::countr_zero(m_ticks);
./src/dsr/model/dsr-routing.cc:243, 246, 3360, 3363
.AddAttribute("GratReplyHoldoff",
MakeTimeAccessor(&DsrRouting::m_gratReplyHoldoff),