I write my own long arithmetic library in C ++ for fun, and it is already pretty finished, I even implemented several cryptographic algorithms with this library, but one important thing is still missing: I want to convert doubles (and floats / long doubles) to my number and vice versa. My numbers are represented as an array with a variable size of an unsigned long int plus a signed bit.
I tried to find the answer using Google, but the problem is that people rarely ever implement such things, so I only find things about how to use Java BigInteger, etc.
Conceptually, this is quite easy: I take the mantissa, shift it by the number of bits dictated by the exponent, and set the sign. In the other direction, I truncate it so that it fits into the mantissa and sets the exponent depending on my function log2.
But it’s hard for me to figure out the details, I could either play with some bit patterns or throw it on a double, but I couldn’t find an elegant way to achieve this, or I could “calculate” it, exponentially, multiply it, etc. .d., but it does not seem very effective.
I would appreciate a solution that does not use library calls, because I try to avoid libraries for my project, otherwise I could just use gmp, in addition, I often have two solutions in several other cases, one of which is used inline assembler, which is efficient and more platform independent, so any answer is useful to me.
edit: I use uint64_t for my parts, but I would like to be able to change it depending on the machine, but I want to make several different implementations with some #ifdefs to achieve this.