📄 misc.cpp
字号:
//---------------------------------------------------------------------------
//
// Misc.cpp: Miscellaneous Routines
//
//---------------------------------------------------------------------------
#include "StdAfx.h"
#include "Misc.h"
static int RandSeed = 0;
__int64 __declspec(naked) ReadTSC()
{
__asm
{
push ebx
push esi
push edi
xor eax, eax
cpuid
rdtsc
mov esi, eax
mov edi, edx
xor eax, eax
cpuid
mov eax, esi
mov edx, edi
pop edi
pop esi
pop ebx
ret
}
}
float __declspec(naked) ceilf(float x)
{
__asm
{
fld dword ptr[esp+4]
sub esp, 4
fnstcw word ptr[esp] // save CW
movzx eax, word ptr[esp]
or eax, 0x0b00 // round toward +INF, full precision
mov word ptr[esp+2], ax
fldcw word ptr[esp+2]
frndint
fldcw word ptr[esp]
add esp, 4
ret
}
}
float __declspec(naked) truncf(float x)
{
__asm
{
fld dword ptr[esp+4]
sub esp, 4
fnstcw word ptr[esp] // save CW
movzx eax, word ptr[esp]
or eax, 0x0f00 // round toward zero, full precision
mov word ptr[esp+2], ax
fldcw word ptr[esp+2]
frndint
fldcw word ptr[esp]
add esp, 4
ret
}
}
float __declspec(naked) roundf(float x)
{
__asm
{
fld dword ptr[esp+4]
frndint
ret
}
}
float __declspec(naked) log2f(float x)
{
__asm
{
fld1
fld dword ptr[esp+4]
fyl2x
ret
}
}
int __cdecl base2(int x)
{
float q = float(x);
return ((int &)q - 0x3f800000) >> 23;
}
void __cdecl Randomize()
{
SYSTEMTIME st;
GetSystemTime(&st);
RandSeed = ((st.wHour * 60 + st.wMinute) * 60 + st.wSecond) * 1000 + st.wMilliseconds;
}
int __cdecl RandInt(int Range)
{
int rs = RandSeed;
int ri;
__asm
{
mov eax, [Range]
imul edx, [rs], 0x08088405
add edx, 1
mov [rs], edx
mul edx
mov [ri], edx
}
RandSeed = rs;
return ri;
}
int __cdecl RandomRange(int nFrom, int nTo)
{
return nFrom + RandInt(nTo - nFrom);
}
void __cdecl FillRandom(DWORD* buf, DWORD len, DWORD minVal, DWORD maxVal)
{
DWORD l = len >> 2;
for (DWORD i = 0; i < l; ++i)
{
int r0 = RANDOM_RANGE(minVal, maxVal);
int r1 = RANDOM_RANGE(minVal, maxVal);
int r2 = RANDOM_RANGE(minVal, maxVal);
int r3 = RANDOM_RANGE(minVal, maxVal);
buf[i] = DWORD(r0 | (r1 << 8) | (r2 << 16) | (r3 << 24));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -