Created
April 26, 2011 16:38
-
-
Save jacbar/942623 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
uint64_t float_to_rational(float a){ | |
uint64_t result, r_denominator = 1, r_numerator, r_sign; | |
float fraction = a - floor(a); | |
while(fraction > 0){ | |
fraction *= 10; | |
fraction = fraction - floor(fraction); | |
r_denominator *= 10; | |
} | |
r_numerator = (int)(a * r_denominator); | |
(a >= 0) ? r_sign = 0 : r_sign = 0x8000000000000000; | |
asm volatile("movq %3, %%rdx\n\t" | |
"orq %%rdx, %0\n\t" | |
"movq %2, %%rdx\n\t" | |
"orq %%rdx, %0\n\t" | |
"movq %1, %%rdx\n\t" | |
"shl $0x20, %%rdx\n\t" | |
"orq %%rdx, %0\n\t" | |
:"=m"(result) | |
:"m"(r_denominator), "m"(r_numerator), "m"(r_sign) | |
:"rdx" | |
); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment