summaryrefslogtreecommitdiffhomepage
path: root/src/fenv/nt64/fenv.S
blob: 801db934def71b7e1106ed47b80f9b135eb57b41 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**************************************************************************/
/*  mmglue: midipix architecture- and target-specific bits for musl libc  */
/*  Copyright (C) 2013--2023  SysDeer Technologies, LLC                   */
/*  Released under the Standard MIT License; see COPYING.MMGLUE.          */
/**************************************************************************/

	.file "fenv.S"

/* common exception bits */
#define FE_INVALID          (1 << 0)
#define FE_DENORM           (1 << 1)
#define FE_DIVBYZERO        (1 << 2)
#define FE_OVERFLOW         (1 << 3)
#define FE_UNDERFLOW        (1 << 4)
#define FE_INEXACT          (1 << 5)

#define FE_ALL_EXCEPT       (0x3F)

/* X87 FPU rounding control bits */
#define FPU_RC_TONEAREST    (0x0 << 10)
#define FPU_RC_DOWNWARD     (0x1 << 10)
#define FPU_RC_UPWARD       (0x2 << 10)
#define FPU_RC_TOWARDZERO   (0x3 << 10)
#define FPU_RC_MASK         FPU_RC_TOWARDZERO

/* X87 FPU control word */
#define FPU_CW_MASK         (0xFFFF)
#define FPU_CW_RC_MASK      (FPU_CW_MASK ^ FPU_RC_MASK)

/* MXCSR rounding control bits */
#define MXCSR_RC_TONEAREST  (0X0 << 13)
#define MXCSR_RC_DOWNWARD   (0x1 << 13)
#define MXCSR_RC_UPWARD     (0x2 << 13)
#define MXCSR_RC_TOWARDZERO (0x3 << 13)
#define MXCSR_RC_MASK       MXCSR_RC_TOWARDZERO

/* MXCSR control word */
#define MXCSR_CW_MASK       (0xFFFF)
#define MXCSR_CW_RC_MASK    (MXCSR_CW_MASK ^ MXCSR_RC_MASK)

/* fenv_At struct definition */
#define FPUCW_MEMBER_OFFSET (0x0)
#define MXCSR_MEMBER_OFFSET (0x1C)

/* user rounding control flags */
#define FE_RC_TONEAREST     FPU_RC_TONEAREST
#define FE_RC_DOWNWARD      FPU_RC_DOWNWARD
#define FE_RC_UPWARD        FPU_RC_UPWARD
#define FE_RC_TOWARDZERO    FPU_RC_TOWARDZERO
#define FE_RC_MASK          FPU_RC_MASK

/* user default float environment */
#define FE_DFL_ENV          (-1)

	/******************************************************/
	/*                                                    */
	/* FSTCW:   check for pending floating-point          */
	/*          exceptions, then store FPU control word.  */
	/*                                                    */
	/* FNSTCW:  store FPU control word without first      */
	/*          checking for pending floating-point       */
	/*          exceptions.                               */
	/*                                                    */
	/* FSTSW:   check for pending floating-point          */
	/*          exceptions, then store FPU status word.   */
	/*                                                    */
	/* FNSTSW:  store FPU status word without first       */
	/*          checking for pending floating-point       */
	/*          exceptions.                               */
	/*                                                    */
	/* FCLEX:   check for pending floating-point          */
	/*          exceptions, then clear FPU exception      */
	/*          flags.                                    */
	/*                                                    */
	/* FNCLEX:  clear FPU exception flags without first   */
	/*          checking for pending floating-point       */
	/*          exceptions.                               */
	/*                                                    */
	/* FLDCW:   set the FPU Control Word from memory.     */
	/*                                                    */
	/* FLDENV:  set the FPU Environment from memory.      */
	/*                                                    */
	/* STMXCSR: store the state of the MXCSR register;    */
	/*          the register's reserved bits are set to   */
	/*          zero in the destination.                  */
	/*                                                    */
	/* LDMXCSR: set the MXCSR environment from memory.    */
	/*                                                    */
	/******************************************************/

	/**********************************************/
	/* int fetestexcept(int exflags);             */
	/*                                            */
	/* query which exception flags, as indicated  */
	/* by the exflags mask, are currently set.    */
	/*                                            */
	/**********************************************/

	.text
	.def fetestexcept; .scl 2; .type 32; .endef
	.global fetestexcept

	.cfi_startproc;
fetestexcept:
	and $FE_ALL_EXCEPT,%ecx     /* normalize exflags    */
	push %r10                   /* temporary state var  */
	stmxcsr (%rsp)              /* store mxcsr state    */
	pop %rdx                    /* copy state to rdx    */
	fnstsw %ax                  /* store fpu state      */
	or %edx,%eax                /* fpu+mxcsr state      */
	and %ecx,%eax               /* desired flags only   */
	ret
	.cfi_endproc


	/**********************************************/
	/* int feclearexcept(int exflags);            */
	/*                                            */
	/* clear specific fpu exception flags, as     */
	/* indicated by the exflags mask.             */
	/*                                            */
	/**********************************************/

	.text
	.def feclearexcept; .scl 2; .type 32; .endef
	.global feclearexcept

	.cfi_startproc;
feclearexcept:
	fnstsw %ax                  /* store fpu state      */
	and $FE_ALL_EXCEPT,%ecx     /* normalize exflags    */
	and $FE_ALL_EXCEPT,%eax     /* normalize fpu state  */
	test %ecx,%eax              /* test exflags,state   */
	jz 1f                       /* state already clear? */
	fnclex                      /* clear fpu state      */
1:	push %rdx                   /* temporary state var  */
	stmxcsr (%rsp)              /* store mxcsr state    */
	pop %rdx                    /* copy state to rdx    */
	or %eax,%edx                /* fpu+mxcsr state      */
	test %ecx,%edx              /* test exflags,state   */
	jz 1f                       /* state already clear? */
	not %ecx                    /* clear desired flags- */
	and %ecx,%edx               /* -from mxcsr state    */
	push %rdx                   /* modified mxcsr var   */
	ldmxcsr (%rsp)              /* update mxcsr         */
	pop %rdx                    /* dealloc mxcsr var    */
1:	xor %eax,%eax               /* ret zero either way  */
	ret
	.cfi_endproc


	/**********************************************/
	/* int feraiseexcept(int exflags);            */
	/*                                            */
	/* set specific fpu exception flags, as       */
	/* indicated by the exflags mask.             */
	/*                                            */
	/* it is enough to set the flags only in      */
	/* either mxcsr or the [legacy] x87 fpu,      */
	/* specifically since fetestexcept() does     */
	/* take both into account.                    */
	/*                                            */
	/**********************************************/

	.text
	.def feraiseexcept; .scl 2; .type 32; .endef
	.global feraiseexcept

	.cfi_startproc;
feraiseexcept:
	and $FE_ALL_EXCEPT,%ecx     /* normalize exflags    */
	stmxcsr -0x8(%rsp)          /* store mxcsr state    */
	or %ecx,-0x8(%rsp)          /* set desired flags    */
	ldmxcsr -0x8(%rsp)          /* update mxcsr         */
	xor %eax,%eax
	ret
	.cfi_endproc


	/**********************************************/
	/* int fegetround(void);                      */
	/*                                            */
	/* obtain the current rounding mode, which    */
	/* should be one of: round-to-neareset,       */
	/* round-downwards, round-upwards, or         */
	/* round-toward-zero.                         */
	/*                                            */
	/* when we set the rounding mode we set both  */
	/* mxcsr and the [legacy] x87 fpu, and it     */
	/* thus suffices to obtain the mode from      */
	/* either, which is mxcsr in our case.        */
	/**********************************************/

	.text
	.def fegetround; .scl 2; .type 32; .endef
	.global fegetround

	.cfi_startproc;
fegetround:
	push %rdx                   /* temporary state var  */
	stmxcsr (%rsp)              /* store mxcsr state    */
	pop %rax                    /* copy state to rax    */
	and $MXCSR_RC_MASK,%rax     /* MXCSR RC bits only   */
	shr $0x3,%rax               /* MXCSR --> FE bits    */
	ret
	.cfi_endproc


	/**********************************************/
	/* int fesetround(int rcmode);                */
	/*                                            */
	/* set the current rounding mode, which       */
	/* should be one of: round-to-neareset,       */
	/* round-downwards, round-upwards, or         */
	/* round-toward-zero.                         */
	/**********************************************/

	.text
	.def __fesetround; .scl 2; .type 32; .endef
	.global __fesetround

	.cfi_startproc;
__fesetround:
	push %rdx                       /* alloc control var    */
	and $FE_RC_MASK,%ecx            /* normalize rcarg      */
	fnstcw (%rsp)                   /* current control word */
	andw $FPU_CW_RC_MASK,(%rsp)     /* control word only    */
	or %ecx,(%rsp)                  /* set rc bits to rcarg */
	fldcw (%rsp)                    /* set fpu control word */
	shl $0x3,%ecx                   /* rcarg: FPU --> MXCSR */
	stmxcsr (%rsp)                  /* current mxcsr reg    */
	andw $MXCSR_CW_RC_MASK,(%rsp)   /* control word only    */
	or %ecx,(%rsp)                  /* set rc bits to rcarg */
	ldmxcsr (%rsp)                  /* set mxcsr ctrl word  */
	pop %rdx                        /* dealloc control var  */
	xor %eax,%eax
	ret
	.cfi_endproc


	/**********************************************/
	/* int fegetenv(fenv_t *);                    */
	/*                                            */
	/* popualte a structure of type fenv_t with   */
	/* the current X87 FPU environmtnet (fnstenv) */
	/* as well as MXCSR environment (stmxcsr).    */
	/**********************************************/

	.text
	.def fegetenv; .scl 2; .type 32; .endef
	.global fegetenv

	.cfi_startproc;
fegetenv:
	fnstenv FPUCW_MEMBER_OFFSET(%rcx)
	stmxcsr MXCSR_MEMBER_OFFSET(%rcx)
	xor %eax,%eax
	ret
	.cfi_endproc


	/**********************************************/
	/* int fesetenv(cont fenv_t *);               */
	/*                                            */
	/* (re)initialize the X87 FPU and MXCSR       */
	/* environments from a user structure of      */
	/* type fenv_t.                               */
	/*                                            */
	/* if the pointer to the fenv_t structure is  */
	/* FE_DFL_ENV, reset the X87 FPU and MXCSR    */
	/* environments to theit default values as    */
	/* defined by the architecture.               */
	/*                                            */
	/*   .__control_word  = 0x037f,               */
	/*   .__unused1       = N/A,                  */
	/*   .__status_word   = 0x0,                  */
	/*   .__unused2       = N/A,                  */
	/*   .__tags          = 0xffff,               */
	/*   .__unused3       = N/A,                  */
	/*   .__eip           = 0x0,                  */
	/*   .__cs_selector   = 0x0,                  */
	/*   .__opcode        = 0x0,                  */
	/*   .__unused4       = N/A,                  */
	/*   .__data_offset   = 0x0,                  */
	/*   .__data_selector = 0x0,                  */
	/*   .__unused5       = N/A,                  */
	/*   .__mxcsr         = 0x1f80                */
	/*                                            */
	/**********************************************/

	.text
	.def fesetenv; .scl 2; .type 32; .endef
	.global fesetenv

	.cfi_startproc;
fesetenv:
	/* prolog */
	xor %eax,%eax

	/* use fenv_t or reset to default values? */
	cmpq $FE_DFL_ENV,%rcx
	jz 1f

	/* use the user-provided fenv_t */
	fldenv  FPUCW_MEMBER_OFFSET(%rcx)
	ldmxcsr MXCSR_MEMBER_OFFSET(%rcx)
	ret

	/* reset environments to the architecture-defined defaults */
1:	subq $0x20,%rsp

	/* X87 FPU */
	movq $0x037f,(0x0)(%rsp)
	movq $0xffff,(0x8)(%rsp)
	movq $0x0,(0x10)(%rsp)
	movq $0x0,(0x18)(%rsp)
	fldenv (%rsp)

	/* MXCSR */
	movq $0x1f80,(%rsp)
	ldmxcsr (%rsp)

	/* all done */
	addq $0x20,%rsp
	ret
	.cfi_endproc


	.section .got$fetestexcept
	.global __imp_fetestexcept
__imp_fetestexcept:
	.quad fetestexcept
	.linkonce discard


	.section .got$feclearexcept
	.global __imp_feclearexcept
__imp_feclearexcept:
	.quad feclearexcept
	.linkonce discard


	.section .got$feraiseexcept
	.global __imp_feraiseexcept
__imp_feraiseexcept:
	.quad feraiseexcept
	.linkonce discard


	.section .got$fegetround
	.global __imp_fegetround
__imp_fegetround:
	.quad fegetround
	.linkonce discard


	.section .got$__fesetround
	.global __imp___fesetround
__imp___fesetround:
	.quad __fesetround
	.linkonce discard


	.section .got$fegetenv
	.global __imp_fegetenv
__imp_fegetenv:
	.quad fegetenv
	.linkonce discard


	.section .got$fesetenv
	.global __imp_fesetenv
__imp_fesetenv:
	.quad fesetenv
	.linkonce discard