Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
cortex-m4/stm32f4 longjmp/setjmp implementation with FPU store/restore
* tested with gcc on an stm32f4
* Michel Pollet <>
#if defined(__VFP_FP__)
typedef long __jmp_buf[10 + 8 + 1]; // d8-d15 fpu + fpscr
typedef long __jmp_buf[10];
static int __setjmp(__jmp_buf buf) __attribute__ ((noinline));
static int __setjmp(__jmp_buf buf)
register void * r0 __asm__("r0") = buf;
"mov %%ip, %%sp\n"
"stmia %[store]!, {%%r4-%%r9, %%sl, %%fp, %%ip, %%lr}\n"
#if defined(__VFP_FP__)
"vstmia %[store]!, {%%d8-%%d15}\n"
"vmrs %%r1, fpscr\n"
"str %%r1, [%[store]], #4\n"
"mov.w %r0, #0\n"
: : [store] "r" (r0) :);
static void __longjmp(__jmp_buf buf, long value) __attribute__((noreturn));
static void __longjmp(__jmp_buf buf, long value)
"ldmia %[load]!, {%%r4-%%r9, %%sl, %%fp, %%ip, %%lr}\n"
#if defined(__VFP_FP__)
"vldmia %[load]!, {%%d8-%%d15}\n"
"ldr %%r0, [%[load]], #4\n"
"vmsr fpscr, %%r0\n"
"mov %%sp, %%ip\n"
"movs %%r0, %%r1\n"
"it eq\n"
"moveq %%r0, #1\n"
"bx lr\n"
: : [load] "r" (buf), [value] "r" (value):);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment