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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// This file is part of x86_64-xsave. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/x86_64-xsave/master/COPYRIGHT. No part of x86_64-xsave, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2019 The developers of x86_64-xsave. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/x86_64-xsave/master/COPYRIGHT.


/// See Figure 8-6 in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1, for the layout of the x87 FPU Control Word.
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct X87FloatingPointUnitStatusWord(u16);

impl X87FloatingPointUnitStatusWord
{
	/// Reads the control word after raising any pending unmasked floating point exceptions.
	///
	/// Uses the non-`AX` register form of the `FSTSW` instruction (ie always writes to memory); see <https://github.com/HJLebbink/asm-dude/wiki/FSTSW_FNSTSW>.
	#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
	#[inline(always)]
	pub fn save_after_raising_any_pending_unmasked_floating_point_exceptions() -> Self
	{
		let mut control_word: u16 = unsafe { uninitialized() };
		let control_word_pointer = &mut control_word;

		unsafe
		{
			asm!
			(
				"fstsw $0"
				:
					// Output constraints.
					"=*m"(control_word_pointer)
				:
					// Input constraints.
				:
					// Clobbers.
				:
					// Options.
					"volatile"
			);
		}

		Self(control_word)
	}

	/// Reads the control word.
	///
	/// Uses the non-`AX` register form of the `FNSTSW` instruction (ie always writes to memory); see <https://github.com/HJLebbink/asm-dude/wiki/FSTSW_FNSTSW>.
	#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
	#[inline(always)]
	pub fn save() -> Self
	{
		let mut control_word: u16 = unsafe { uninitialized() };
		let control_word_pointer = &mut control_word;

		unsafe
		{
			asm!
			(
				"fnstsw $0"
				:
					// Output constraints.
					"=*m"(control_word_pointer)
				:
					// Input constraints.
				:
					// Clobbers.
				:
					// Options.
					"volatile"
			);
		}

		Self(control_word)
	}

	/// Invalid Operation exception flag, `IE`.
	///
	/// One of the 6 exception flags that can be controlled by the status word.
	///
	/// When set (`true`), the corresponding x87 FPU floating-point exception occurred.
	#[inline(always)]
	pub fn exception_flag_invalid_operation(self) -> bool
	{
		self.0 & 0b0000_0000_0000_0001 != 0
	}

	/// Denormalized Operand exception flag, `DE`.
	///
	/// One of the 6 exception flags that can be controlled by the status word.
	///
	/// When set (`true`), the corresponding x87 FPU floating-point exception occurred.
	#[inline(always)]
	pub fn exception_flag_denormalized_operand(self) -> bool
	{
		self.0 & 0b0000_0000_0000_0010 != 0
	}

	/// Zero Divide exception flag, `ZE`.
	///
	/// One of the 6 exception flags that can be controlled by the status word.
	///
	/// When set (`true`), the corresponding x87 FPU floating-point exception occurred.
	#[inline(always)]
	pub fn exception_flag_zero_divide(self) -> bool
	{
		self.0 & 0b0000_0000_0000_0100 != 0
	}

	/// Overflow exception flag, `OE`.
	///
	/// One of the 6 exception flags that can be controlled by the status word.
	///
	/// When set (`true`), the corresponding x87 FPU floating-point exception occurred.
	#[inline(always)]
	pub fn exception_flag_overflow(self) -> bool
	{
		self.0 & 0b0000_0000_0000_1000 != 0
	}

	/// Underflow exception flag, `UE`.
	///
	/// One of the 6 exception flags that can be controlled by the status word.
	///
	/// When set (`true`), the corresponding x87 FPU floating-point exception occurred.
	#[inline(always)]
	pub fn exception_flag_underflow(self) -> bool
	{
		self.0 & 0b0000_0000_0001_0000 != 0
	}

	/// Precision exception flag, `EE`.
	///
	/// One of the 6 exception flags that can be controlled by the status word.
	///
	/// When set (`true`), the corresponding x87 FPU floating-point exception occurred.
	#[inline(always)]
	pub fn exception_flag_precision(self) -> bool
	{
		self.0 & 0b0000_0000_0010_0000 != 0
	}

	/// Stack Fault exception flag, `SF`.
	///
	/// The stack fault flag indicates that stack overflow or stack underflow has occurred with data in the x87 FPU data register stack.
	/// The x87 FPU explicitly sets the `SF` flag when it detects a stack overflow or underflow condition, but it does not explicitly clear the flag when it detects an invalid-arithmetic-operand condition.
	/// When this flag is set, the condition code flag `C1` indicates the nature of the fault: overflow (`C1 = 1`) and underflow (`C1 = 0`).
	///
	/// The `SF` flag is a “sticky” flag, meaning that after it is set, the processor does not clear it until it is explicitly instructed to do so (for example, by an `FINIT`/`FNINIT`, `FCLEX`/`FNCLEX`, or `FSAVE`/`FNSAVE` instruction).
	#[inline(always)]
	pub fn stack_fault(self) -> bool
	{
		self.0 & 0b0000_0000_0100_0000 != 0
	}

	/// Exception Summary Status, `ES`.
	///
	/// When set (`true`), a x87 FPU floating-point exception occurred.
	#[inline(always)]
	pub fn exception_summary_status(self) -> bool
	{
		self.0 & 0b0000_0000_1000_0000 != 0
	}

	/// Condition Code `C0`.
	///
	/// See Table 8-1 (Condition Code Interpretation) in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1.
	#[inline(always)]
	pub fn condition_code_c0(self) -> bool
	{
		self.0 & 0b0000_0001_0000_0000 != 0
	}

	/// Condition Code `C1`.
	///
	/// See Table 8-1 (Condition Code Interpretation) in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1.
	#[inline(always)]
	pub fn condition_code_c1(self) -> bool
	{
		self.0 & 0b0000_0010_0000_0000 != 0
	}

	/// Condition Code `C2`.
	///
	/// See Table 8-1 (Condition Code Interpretation) in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1.
	#[inline(always)]
	pub fn condition_code_c2(self) -> bool
	{
		self.0 & 0b0000_0100_0000_0000 != 0
	}

	/// Condition Code `C3`.
	///
	/// See Table 8-1 (Condition Code Interpretation) in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1.
	#[inline(always)]
	pub fn condition_code_c3(self) -> bool
	{
		self.0 & 0b0100_0000_0000_0000 != 0
	}

	/// Top of Stack Pointer, `TOP`.
	///
	/// A pointer to the x87 FPU data register that is currently at the top of the x87 FPU register stack.
	///
	/// This pointer is a binary value from 0 to 7 (A 3-bit (`u3`) value).
	#[inline(always)]
	pub fn top(self) -> u8
	{
		(self.0 & 0b0011_1000_0000_0000 >> 11) as u8
	}

	/// FPU Busy, `B`, or `B-bit`.
	///
	/// This is for 8087 compatibility only.
	/// It reflects the contents of the `ES` (`self.exception_summary_status()`) flag.
	#[inline(always)]
	pub fn fpu_busy(self) -> bool
	{
		self.0 & 0b1000_0000_0000_0000 != 0
	}
}