Skip to content

Instantly share code, notes, and snippets.

@jacbar
Created April 26, 2011 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacbar/942623 to your computer and use it in GitHub Desktop.
Save jacbar/942623 to your computer and use it in GitHub Desktop.
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