1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! FXSR 浮点上下文快速保存和恢复。

#[cfg(test)]
use stdarch_test::assert_instr;

#[allow(improper_ctypes)]
extern "C" {
    #[link_name = "llvm.x86.fxsave64"]
    fn fxsave64(p: *mut u8);
    #[link_name = "llvm.x86.fxrstor64"]
    fn fxrstor64(p: *const u8);
}

/// 将 `x87` FPU,`MMX` 技术,`XMM` 和 `MXCSR` 寄存器保存到
/// 512 字节长,16 字节对齐的内存区域 `mem_addr`。
///
/// 目标操作数未对齐将引发通用保护 (#GP) 或对齐检查异常 (#AC)。
///
/// 请参见 [`FXSAVE`][fxsave] 和 [`FXRSTOR`][fxrstor]。
///
/// [fxsave]: http://www.felixcloutier.com/x86/FXSAVE.html
/// [fxrstor]: http://www.felixcloutier.com/x86/FXRSTOR.html
///
/// [Intel's documentation](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_fxsave64)
///
#[inline]
#[target_feature(enable = "fxsr")]
#[cfg_attr(test, assert_instr(fxsave64))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _fxsave64(mem_addr: *mut u8) {
    fxsave64(mem_addr)
}

/// 从以下位置恢复 `XMM`,`MMX`,`MXCSR` 和 `x87` FPU 寄存器
/// 512 字节长,16 字节对齐的内存区域 `mem_addr`。
///
/// 该存储区的内容应该已经由以前的 `_fxsave` 或 `_fxsave64` 内部函数写入。
///
/// 目标操作数未对齐将引发通用保护 (#GP) 或对齐检查异常 (#AC)。
///
/// 请参见 [`FXSAVE`][fxsave] 和 [`FXRSTOR`][fxrstor]。
///
/// [fxsave]: http://www.felixcloutier.com/x86/FXSAVE.html
/// [fxrstor]: http://www.felixcloutier.com/x86/FXRSTOR.html
///
/// [Intel's documentation](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_fxrstor64)
///
///
///
#[inline]
#[target_feature(enable = "fxsr")]
#[cfg_attr(test, assert_instr(fxrstor64))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn _fxrstor64(mem_addr: *const u8) {
    fxrstor64(mem_addr)
}

#[cfg(test)]
mod tests {
    use crate::core_arch::x86_64::*;
    use std::{cmp::PartialEq, fmt};
    use stdarch_test::simd_test;

    #[repr(align(16))]
    struct FxsaveArea {
        data: [u8; 512], // 512 字节
    }

    impl FxsaveArea {
        fn new() -> FxsaveArea {
            FxsaveArea { data: [0; 512] }
        }
        fn ptr(&mut self) -> *mut u8 {
            &mut self.data[0] as *mut _ as *mut u8
        }
    }

    impl PartialEq<FxsaveArea> for FxsaveArea {
        fn eq(&self, other: &FxsaveArea) -> bool {
            for i in 0..self.data.len() {
                if self.data[i] != other.data[i] {
                    return false;
                }
            }
            true
        }
    }

    impl fmt::Debug for FxsaveArea {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "[")?;
            for i in 0..self.data.len() {
                write!(f, "{}", self.data[i])?;
                if i != self.data.len() - 1 {
                    write!(f, ", ")?;
                }
            }
            write!(f, "]")
        }
    }

    #[simd_test(enable = "fxsr")]
    unsafe fn fxsave64() {
        let mut a = FxsaveArea::new();
        let mut b = FxsaveArea::new();

        fxsr::_fxsave64(a.ptr());
        fxsr::_fxrstor64(a.ptr());
        fxsr::_fxsave64(b.ptr());
        assert_eq!(a, b);
    }
}