#ifndef _KERNEL_AMD64_FX_H
#define _KERNEL_AMD64_FX_H

#define fx_save(buf)                                                                               \
  do {                                                                                             \
    __asm__ volatile ("fxsave64 %0" : "=m"(*(buf))::"memory");                                     \
  } while (0)

#define fx_restore(buf)                                                                            \
  do {                                                                                             \
    __asm__ volatile ("fxrstor64 %0" ::"m"(*(buf)) : "memory");                                    \
  } while (0)

#define fx_init(buf)                                                                               \
  do {                                                                                             \
    memset ((buf), 0, sizeof ((buf)));                                                             \
    __asm__ volatile ("fninit; fxsave64 %0" : "=m"(*(buf))::"memory");                             \
    uint32_t* __mxcsr = (uint32_t*)&(buf)[24];                                                     \
    *__mxcsr = 0x1F80;                                                                             \
    uint16_t* __fcw = (uint16_t*)&(buf)[0];                                                        \
    *__fcw = 0x037F;                                                                               \
  } while (0)

#endif // _KERNEL_AMD64_FX_H
