float parameter, alignment and stacks

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
russian
Posts: 368
Joined: Mon Oct 29, 2012 3:17 am
Has thanked: 16 times
Been thanked: 14 times

float parameter, alignment and stacks

Post 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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: float parameter, alignment and stacks

Post by Giovanni »

The thread stacks are aligned to 8 bytes already, it must be another issue. Are using chprintf() or a library printf()?

Giovanni
User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

Re: float parameter, alignment and stacks

Post 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
User avatar
lilvinz
Posts: 130
Joined: Sat Dec 21, 2013 2:56 pm
Has thanked: 1 time
Been thanked: 6 times

Re: float parameter, alignment and stacks

Post by lilvinz »

I will try to provide a minimal test case.
Post Reply