forked from nique_372/TsnTables
160 lines
4 KiB
MQL5
160 lines
4 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Ben.mq5 |
|
|
//| Copyright 2026, Niquel Mendoza |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2026, Niquel Mendoza"
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
#property strict
|
|
#property script_show_inputs
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
#include "CLZ.mqh"
|
|
#include <TSN\\MQLArticles\\Utils\\Random.mqh>
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
input int InpNumReps = 1000000; // Num reps
|
|
input ulong InpXhoSeed = __RANDOM__; // Seed
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int CLZ64_Bin(ulong x)
|
|
{
|
|
if(x == 0)
|
|
return 64;
|
|
int n = 0;
|
|
if((x & 0xFFFFFFFF00000000) == 0)
|
|
{
|
|
n += 32;
|
|
x <<= 32;
|
|
}
|
|
if((x & 0xFFFF000000000000) == 0)
|
|
{
|
|
n += 16;
|
|
x <<= 16;
|
|
}
|
|
if((x & 0xFF00000000000000) == 0)
|
|
{
|
|
n += 8;
|
|
x <<= 8;
|
|
}
|
|
if((x & 0xF000000000000000) == 0)
|
|
{
|
|
n += 4;
|
|
x <<= 4;
|
|
}
|
|
if((x & 0xC000000000000000) == 0)
|
|
{
|
|
n += 2;
|
|
x <<= 2;
|
|
}
|
|
if((x & 0x8000000000000000) == 0)
|
|
{
|
|
n += 1;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int CLZ64_Slow(ulong x)
|
|
{
|
|
if(x == 0)
|
|
return 64;
|
|
int count = 0;
|
|
for(int i = 63; i >= 0; i--)
|
|
{
|
|
if((x & (1ULL << i)) != 0)
|
|
break;
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int CLZ_64(ulong x)
|
|
{
|
|
if(x == 0)
|
|
return 64; // 64 completos
|
|
//---
|
|
x |= x >> 1;
|
|
x |= x >> 2;
|
|
x |= x >> 4;
|
|
x |= x >> 8;
|
|
x |= x >> 16;
|
|
x |= x >> 32;
|
|
//---
|
|
return TSN::g_tsntables_clz[((x * TSN_TABLES_CLZ_CONSTANT_MAGIC64) >> 58)];
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Script program start function |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart()
|
|
{
|
|
//---
|
|
ulong test[];
|
|
ArrayResize(test, InpNumReps);
|
|
|
|
Xoshiro256 xo;
|
|
xo.Seed(InpXhoSeed);
|
|
|
|
for(int i = 0; i < InpNumReps; i++)
|
|
{
|
|
test[i] = xo.RandomUlong();
|
|
}
|
|
|
|
//---
|
|
int c = 0;
|
|
ulong a = GetMicrosecondCount();
|
|
for(int i = 0; i < InpNumReps; i++)
|
|
{
|
|
c += CLZ64_Slow(test[i]);
|
|
}
|
|
ulong b = GetMicrosecondCount();
|
|
PrintFormat("Tiempo CLZ64 Slow base = %I64u, checksum = %d", (b - a), c);
|
|
|
|
//---
|
|
c = 0;
|
|
a = GetMicrosecondCount();
|
|
for(int i = 0; i < InpNumReps; i++)
|
|
{
|
|
c += TSN::CBitTricks::CLZ(test[i]);
|
|
}
|
|
b = GetMicrosecondCount();
|
|
PrintFormat("Tiempo TSN::CLZ = %I64u, checksum = %d", (b - a), c);
|
|
|
|
|
|
//---
|
|
c = 0;
|
|
a = GetMicrosecondCount();
|
|
for(int i = 0; i < InpNumReps; i++)
|
|
{
|
|
c += CLZ64_Bin(test[i]);
|
|
}
|
|
b = GetMicrosecondCount();
|
|
PrintFormat("Tiempo CLZ64_Bin base = %I64u, checksum = %d", (b - a), c);
|
|
|
|
//---
|
|
c = 0;
|
|
a = GetMicrosecondCount();
|
|
for(int i = 0; i < InpNumReps; i++)
|
|
{
|
|
c += CLZ_64(test[i]);
|
|
}
|
|
b = GetMicrosecondCount();
|
|
PrintFormat("Tiempo CLZ_64 = %I64u, checksum = %d", (b - a), c);
|
|
|
|
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|