Page 1 of 1

float parameter, alignment and stacks

Posted: Sat Jan 19, 2013 5:53 am
by russian
My search for printf implementation with float support has pointed me to http://home.comcast.net/~derelict/files/printf2.c
Soon enough I have learned about the whole nightmare with passing float/double parameters to functions and alignment, I have resolved it by

Code: Select all

         case 'f':
            {
            // http://wiki.debian.org/ArmEabiPort#Structpackingandalignment
            // Stack alignment
            //
            // The ARM EABI requires 8-byte stack alignment at public function entry points,
            // compared to the previous 4-byte alignment.
            char *cptr = (char *) varg ;  //lint !e740 !e826  convert to double pointer
            // just a bit of hell related to parameters alignment
            uint caddr = (uint) cptr ;
            if ((caddr & 0x7) != 0) {
               cptr += 4 ;
            }


I was happy while I was only working with all this in the main thread. As soon as I have started printing doubles from user threads, same issues with weird output came back, and the alignment trick does not work any more. I have even tried pragma to align the stack array but this made zero difference

Code: Select all

#pragma pack(16)
static WORKING_AREA(consoleThread, 512);
#pragma pack()


Is there maybe a know way to avoid all this pain with floats and doubles and functions invocations?,,,
I am using stm32f4 and Chibi 2.4.2

Re: float parameter, alignment and stacks

Posted: Sat Jan 19, 2013 8:13 am
by Giovanni
The thread stacks are aligned to 8 bytes already, it must be another issue. Are using chprintf() or a library printf()?

Giovanni

Re: float parameter, alignment and stacks

Posted: Thu Mar 20, 2014 7:28 pm
by lilvinz
Hi,

i am observing a similar (or the same) issue when passing double using variable arguments "...".
The odd thing is that enabling CH_DBG_SYSTEM_STATE_CHECK fixes it.
The working area is aligned at an 8 bytes boundary.
Any idea how to further track this down?

Thanks and cheers,

Vinz

Re: float parameter, alignment and stacks

Posted: Thu Mar 20, 2014 9:30 pm
by lilvinz
I will try to provide a minimal test case.