0

For an STM32F7, which includes instructions for double floating points, I want to convert an uint64_t to double.

In order to test that, I used the following code:

volatile static uint64_t m_testU64 = 45uLL * 0xFFFFFFFFuLL;
volatile static double m_testD;

#ifndef DO_NOT_USE_UL2D
    m_testD = (double)m_testU64;
#else
    double t = (double)(uint32_t)(m_testU64 >> 32u);
    t *= 4294967296.0;
    t += (double)(uint32_t)(m_testU64 & 0xFFFFFFFFu);
    m_testD = t;
#endif

By default (if DO_NOT_USE_UL2D is not defined) the compiler (gcc or clang) is calling the function: __aeabi_ul2d() which is kind of complex in number of executed instruction. See the assembly code here : https://github.com/gcc-mirror/gcc/blob/master/libgcc/config/arm/ieee754-df.S#L537

For my particular example, it takes 20 instructions without entering in most of the branches

And if DO_NOT_USE_UL2D is defined, the compiler generate the following assembly code:

movw    r0, #1728       ; 0x6c0
vldr    d2, [pc, #112]  ; 0x303fa0
movt    r0, #8192       ; 0x2000
vldr    s0, [r0, #4]
ldr     r1, [r0, #0]
vcvt.f64.u32    d0, s0
vldr    s2, [r0]
vcvt.f64.u32    d1, s2
ldr     r1, [r0, #4]
vfma.f64        d1, d0, d2
vstr    d1, [r0, #8]

The code is simpler, and it is only 10 instructions.

So here the the questions (if DO_NOT_USE_UL2D is defined):

  • Is my code (in C) correct?
  • Is my code slower than the __aeabi_ul2d() function (not really important, but a bit curious)?

I have to do that, since I am not allowed to use function from libgcc (There are very good reasons for that...)

Be aware that the main purpure of this question is not about performance, I am really curious about the implementation in libgcc, and I really want to know if there is something wrong in my code.

benjarobin
  • 4,410
  • 27
  • 21
  • Is performance critical enough that you need to optimize like this? Straight assignment should do it. This dark voodoo going on will need *extensive* tests if you're going to depend on that math being correct. – tadman Feb 05 '19 at 16:40
  • _Is my code (in C) correct?_ yes it is, and if I do the two computations they calculate exactly the same value, a diff of them gives 0, there is no overflow going to double. – bruno Feb 05 '19 at 16:44
  • @tadman Question updated, this is not a question about performance. – benjarobin Feb 05 '19 at 16:45
  • @bruno I did test a little bit my code (About A million values tested), and for every tested value, the result is right. – benjarobin Feb 05 '19 at 16:46
  • 1
    even using input numbers using more bits than the mantissa of a double the result is exactly the same ! May be you will have to admit you are better than the lib ? ^^ – bruno Feb 05 '19 at 16:56

0 Answers0