Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #81594
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Some help needed |
| Date | 2021-09-26 07:26 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <sip0ab$gpr$1@dont-email.me> (permalink) |
| References | <since9$gua$1@dont-email.me> |
So as the timings of the PAUSE-instructions are so different I
decided to write a singleton containing the fastest timing of
the PAUSE-instruction on the machine. This is the code:
cpu_pause.h:
#pragma once
#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
#include <intrin.h>
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
#include <immintrin.h>
#else
#error "need platform-specific pause-instruction"
#endif
inline
void cpu_pause()
{
#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) ||
defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
_mm_pause();
#endif
}
inline
void cpu_pause_n( unsigned iterations )
{
for( ; iterations--; cpu_pause() );
}
struct pause_singleton
{
static
double getNsPerPause();
private:
static
struct singleton_t
{
singleton_t();
double m_nsPerPause;
} singleton;
};
inline
double pause_singleton::getNsPerPause()
{
return singleton.m_nsPerPause;
}
cpu_pasue.h:
#include <chrono>
#include <limits>
#include "cpu_pause.h"
using namespace std;
using namespace chrono;
#if defined(_MSC_VER)
#pragma warning(disable: 26495) // member not initialized
#pragma warning(disable: 26498) // consider constexpr
#endif
pause_singleton::singleton_t::singleton_t()
{
int64_t leastTicks = numeric_limits<int64_t>::max();;
for( size_t i = 1000; i; --i )
{
auto start = high_resolution_clock::now();
for( size_t j = 1'000; j; --j )
cpu_pause();
int64_t ticks = (int64_t)duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count();
if( ticks < leastTicks )
leastTicks = ticks;
}
m_nsPerPause = leastTicks / 1'000.0;
}
pause_singleton::singleton_t pause_singleton::singleton;
So I can adjust the spinning-loop according
to pause_singleton::getNsPerPause().
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Some help needed Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-25 16:41 +0200
Re: Some help needed Paavo Helde <myfirstname@osa.pri.ee> - 2021-09-25 19:51 +0300
Re: Some help needed Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-25 19:01 +0200
Re: Some help needed Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-25 17:20 +0000
Re: Some help needed Bo Persson <bo@bo-persson.se> - 2021-09-25 19:05 +0200
Re: Some help needed Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-25 19:14 +0200
Re: Some help needed Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-25 17:33 +0000
Re: Some help needed Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-25 17:15 +0000
Re: Some help needed Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-25 19:16 +0200
Re: Some help needed Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-25 17:29 +0000
Re: Some help needed Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-26 07:26 +0200
Re: Some help needed Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-26 06:56 +0000
Re: Some help needed - further help Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-27 17:35 +0200
Re: Some help needed - further help Bo Persson <bo@bo-persson.se> - 2021-09-27 19:29 +0200
csiph-web