Created
November 16, 2012 19:32
-
-
Save Ruzzz/4090174 to your computer and use it in GitHub Desktop.
Быстрые мат. функции на си (нашел где-то)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int floor(float a) | |
{ | |
int b = (int)a; | |
if (a >= 0 || (a - b) == 0) | |
return b; | |
else | |
return b - 1; | |
} | |
int trunc(float r) | |
{ | |
__asm { | |
FLD DWORD [ESP] ; Load r into ST(0) | |
SUB ESP, 0CH | |
FSTCW [ESP] | |
FSTCW [ESP+2] | |
OR WORD[ESP+2], 0F00H | |
FLDCW [ESP+2] | |
FISTP QWORD[ESP+4] | |
FLDCW [ESP] | |
POP ECX | |
POP EAX | |
POP EDX | |
} | |
} | |
int round(float r) | |
{ | |
__asm { | |
FLD DWORD [ESP] ; Load r into ST(0) | |
FISTP DWORD [ESP] ; Store ST(0) in [ESP] and pop ST(0) | |
POP EAX ; EAX = [ESP] | |
} | |
} | |
int truncate_int(double x) | |
{ | |
assert(x > static_cast(INT_MIN / 2) – 1.0); | |
assert(x < static_cast(INT_MAX / 2) + 1.0); | |
const float round_towards_m_i = -0.5f; | |
int i; | |
__asm { | |
fld x | |
fadd st, st(0) | |
fabs | |
fadd round_towards_m_i | |
fistp i | |
sar i, 1 | |
} | |
if (x < 0) | |
i = -i; | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment