Code: Select all
1 << (int)(ceil(log2(given)))
How can this be converted to PB? I see Log and Log10 but not Log2.
Code: Select all
1 << (int)(ceil(log2(given)))
Code: Select all
Procedure GetNearestTwo(Value, Mode)
ProcedureReturn 1 << Int(Round(Log(Value) / Log(2), Mode))
EndProcedure
Debug "next two up"
For i = 1 To 25
Debug Str(i) + " " + Str(GetNearestTwo(i, #PB_Round_Up))
Next
Debug ""
Debug "nearest two"
For i = 1 To 25
Debug Str(i) + " " + Str(GetNearestTwo(i, #PB_Round_Nearest))
Next
Code: Select all
Procedure GetNearestTwo(Value, Mode)
ProcedureReturn 1 << Int(Round(Log(Value) / Log(2), Mode))
EndProcedure
Procedure Nearest2pow(Value)
!mov ecx, [p.v_Value]
!dec ecx
!bsr ecx, ecx
!mov eax, 1
!add ecx, 1
!shl eax, cl
ProcedureReturn
EndProcedure
#Tries = 10000000
Dim results(#Tries, 1)
time = ElapsedMilliseconds()
For U = 0 To #Tries
results(u, 0) = GetNearestTwo(u, #PB_Round_Up)
Next
MessageRequester("", Str(ElapsedMilliseconds()-time))
time = ElapsedMilliseconds()
For U = 0 To #Tries
results(u, 1) = Nearest2pow(u)
Next
MessageRequester("", Str(ElapsedMilliseconds()-time))
Now sure what you mean by that Trond? 2^0 = 1 and thus 1 is indeed a power of 2.By the way, 1 is not really a power of two, as the other procedure seems to think.
Code: Select all
Procedure GetNextPowerOfTwo( x )
x & $ffffffff -1
x | x >> 1
x | x >> 2
x | x >> 4
x | x >> 8
x | x >> 16
ProcedureReturn x & $ffffffff + 1
EndProcedure
Code: Select all
Procedure NEXTPOWEROF2(number.l)
EnableASM
Protected RetVal.l = 1;
XOR ecx , ecx
BSR ecx , Number
INC ecx
SHL RetVal , cl
DisableASM
ProcedureReturn RetVal.l
EndProcedure