2

Okay so I have to calculate this kind of calculation: 10^(some floating point value) where the floating point value (the exponent) is stored as double in xmm0 register calculated before with divsd xmm0, xmm1 with 64 bit double floating point values.

How can I do this with x86_64 assembly? Is this something very complicated to do in assembly in x86 as there are no straight exponentation functions on the processor. I really have no idea how to even approach this problem. I have a processor where the SSE and SSE2 are available.

EDIT: I was just looking around and found a way to achieve this using the fpu efficiently to solve this, now the problem seems to be how to move the result from the previous calculation (from the xmm0 reg) to the fpu stack. How do you move such floating point from the XMM0 reg to the flaoting point unit stack to be used with the fpu instructions?

The amateur programmer
  • 1,238
  • 3
  • 18
  • 38
  • https://godbolt.org/ Use this site. Write it in C and see how it would translate to give you an idea. – Eraklon Feb 20 '20 at 12:20
  • 3
    Transform to `e^x` and use a series to the necessary precision or use the FPU. You could also look in [glibc source to see how `pow` is implemented](https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/fpu/e_powl.S;h=e88570cda4aa7df0d910b8dad75fadc164b4baa4;hb=HEAD). But you should really just `call pow`. – Jester Feb 20 '20 at 12:26
  • @Jester I'll look into that source, I cannot call the pow function as I have no libraries available as I'm programming this on bare metal. – The amateur programmer Feb 20 '20 at 12:41
  • 3
    related: [Fastest Implementation of Exponential Function Using AVX](//stackoverflow.com/q/48863719) is a fast approximation using AVX / SSE instructions. (e.g. integer shift of the bit-pattern to stuff an integer into the exponent of a `float` or `double`; you really want to transform x^y into 2^y times log2(x), I think; there are other Q&As with log or log2 implementations). You can write it to use scalar FP mat if you want, still using SIMD-integer shifts. Or if you want slow high precision, just call `pow` (or copy a `pow` implementation from a good licence-compatible library.) – Peter Cordes Feb 20 '20 at 12:41
  • Are you allowed to use x87-math? – chtz Feb 20 '20 at 12:56
  • @chtz actually yes, I was just looking around and found a way to achieve this using the fpu efficiently, now the problem seems to be how to move the result from the previous calculation (from the xmm0 reg) to the fpu stack. – The amateur programmer Feb 20 '20 at 13:12
  • @Theamateurprogrammer To move a floating point number from an xmm register into the x87 FPU, you need to store it in memory and then load it into an x87 register. – fuz Feb 20 '20 at 13:31
  • 1
    As @fuz said, conversion between x87 and SSE registers is easiest done via memory (also when converting the result back): https://godbolt.org/z/DT9SFQ – chtz Feb 20 '20 at 13:34

0 Answers0