Last active
November 30, 2015 18:09
-
-
Save bencz/b15125c884bed7635baa to your computer and use it in GitHub Desktop.
Programa para converter float para o padrão ieee754 (binario)
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
#include <stdio.h> | |
#include <malloc.h> | |
#include <string.h> | |
#if __USE_SSE__ | |
void reverse(char *begin, char *end) | |
{ | |
_asm | |
{ | |
mov edx, DWORD PTR[begin] | |
mov eax, DWORD PTR[end] | |
Loop_tamanho: | |
cmp edx, eax | |
jnb Sair | |
movzx ecx, BYTE PTR[edx] | |
movzx ebx, BYTE PTR[eax] | |
mov BYTE PTR[edx], bl | |
mov BYTE PTR[eax], cl | |
add edx, 1 | |
sub eax, 1 | |
jmp Loop_tamanho | |
Sair: | |
} | |
} | |
const unsigned char vrev[] = { 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 }; | |
void vreverse(char *begin, char *end) | |
{ | |
__asm | |
{ | |
mov eax, DWORD PTR[begin] | |
mov edx, DWORD PTR[end] | |
mov ecx, edx | |
sub ecx, eax | |
add ecx, 1 | |
Loop_inicio: | |
cmp ecx, 15 | |
jle Reverte | |
movdqu xmm1, XMMWORD PTR[eax] | |
movdqu xmm0, XMMWORD PTR[edx - 15] | |
movdqa xmm2, XMMWORD PTR[vrev] | |
pshufb xmm1, xmm2 | |
pshufb xmm0, xmm2 | |
movups XMMWORD PTR[edx - 15], xmm1 | |
movups XMMWORD PTR[eax], xmm0 | |
add eax, 16 | |
sub edx, 16 | |
sub ecx, 32 | |
jmp Loop_inicio | |
Reverte: | |
cmp ecx, 1 | |
jle Sair | |
push edx | |
push eax | |
call reverse | |
add esp, 8 | |
Sair: | |
} | |
} | |
#else | |
char *strrev_t(char *str) | |
{ | |
char *p1, *p2; | |
if (!str || !*str) | |
return str; | |
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) | |
{ | |
*p1 ^= *p2; | |
*p2 ^= *p1; | |
*p1 ^= *p2; | |
} | |
return str; | |
} | |
#endif | |
void floatToBinary(float f, char *str, int numeroDeBits) | |
{ | |
int i = 0; | |
int strIndex = 0; | |
union | |
{ | |
float f; | |
unsigned int i; | |
}u; | |
u.f = f; | |
memset(str, '0', numeroDeBits); | |
for (i = 0; i < numeroDeBits; i++) | |
{ | |
str[strIndex++] = (u.i & (1 << 0)) ? '1' : '0'; | |
u.i >>= 1; | |
} | |
str[strIndex] = '\0'; | |
#if __USE_SSE__ | |
vreverse(str, str+strIndex-1); | |
#else | |
str = strrev_t(str); | |
#endif | |
} | |
int main() | |
{ | |
float input = 0.0; | |
const int numeroDeBits = 32; | |
char *str = (char*)malloc(sizeof(char) * numeroDeBits); | |
printf("Digite um numero float: "); | |
scanf("%f", &input); | |
floatToBinary(input, str, numeroDeBits); | |
printf("%s\n", str); | |
free(str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment