GCC - How to realign stack?

前端 未结 5 779
广开言路
广开言路 2021-02-09 05:45

I try to build an application which uses pthreads and __m128 SSE type. According to GCC manual, default stack alignment is 16 bytes. In order to use __m128, the requirement is t

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-09 06:34

    Another solution would be, to use a padding function, which first aligns the stack and then calls f. So instead of calling f directly, you call pad, which pads the stack first and then calls foowith an aligned stack.

    The code would look like this:

    #include 
    #include 
    
    #define ALIGNMENT 16
    
    void *f(void *x) {
        __m128 y;
        // other stuff
    }
    
    void * pad(void *val) {
        unsigned int x; // to get the current address from the stack
        unsigned char pad[ALIGNMENT - ((unsigned int) &x) % ALIGNMENT];
        return f(val);
    }
    
    int main(void){
        pthread_t p;
        pthread_create(&p, NULL, pad, NULL);
    }
    

提交回复
热议问题