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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2011, Richard Lowe
*/
#ifndef _FENV_INLINES_H
#define _FENV_INLINES_H
#ifdef __GNUC__
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#if defined(__x86)
/*
* Floating point Control Word and Status Word
* Definition should actually be shared with x86
* (much of this 'amd64' code can be, in fact.)
*/
union fp_cwsw {
uint32_t cwsw;
struct {
uint16_t cw;
uint16_t sw;
} words;
};
extern __GNU_INLINE void
__fenv_getcwsw(unsigned int *value)
{
union fp_cwsw *u = (union fp_cwsw *)value;
__asm__ __volatile__(
"fstsw %0\n\t"
"fstcw %1\n\t"
: "=m" (u->words.cw), "=m" (u->words.sw));
}
extern __GNU_INLINE void
__fenv_setcwsw(const unsigned int *value)
{
union fp_cwsw cwsw;
short fenv[16];
cwsw.cwsw = *value;
__asm__ __volatile__(
"fstenv %0\n\t"
"movw %4,%1\n\t"
"movw %3,%2\n\t"
"fldenv %0\n\t"
"fwait\n\t"
: "=m" (fenv), "=m" (fenv[0]), "=m" (fenv[2])
: "r" (cwsw.words.cw), "r" (cwsw.words.sw)
/* For practical purposes, we clobber the whole FPU */
: "cc", "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)",
"st(6)", "st(7)");
}
extern __GNU_INLINE void
__fenv_getmxcsr(unsigned int *value)
{
__asm__ __volatile__("stmxcsr %0" : "=m" (*value));
}
extern __GNU_INLINE void
__fenv_setmxcsr(const unsigned int *value)
{
__asm__ __volatile__("ldmxcsr %0" : : "m" (*value));
}
extern __GNU_INLINE long double
f2xm1(long double x)
{
long double ret;
__asm__ __volatile__("f2xm1" : "=t" (ret) : "0" (x) : "cc");
return (ret);
}
extern __GNU_INLINE long double
fyl2x(long double y, long double x)
{
long double ret;
__asm__ __volatile__("fyl2x"
: "=t" (ret)
: "0" (x), "u" (y)
: "st(1)", "cc");
return (ret);
}
extern __GNU_INLINE long double
fptan(long double x)
{
/*
* fptan pushes 1.0 then the result on completion, so we want to pop
* the FP stack twice, so we need a dummy value into which to pop it.
*/
long double ret;
long double dummy;
__asm__ __volatile__("fptan"
: "=t" (dummy), "=u" (ret)
: "0" (x)
: "cc");
return (ret);
}
extern __GNU_INLINE long double
fpatan(long double x, long double y)
{
long double ret;
__asm__ __volatile__("fpatan"
: "=t" (ret)
: "0" (y), "u" (x)
: "st(1)", "cc");
return (ret);
}
extern __GNU_INLINE long double
fxtract(long double x)
{
__asm__ __volatile__("fxtract" : "+t" (x) : : "cc");
return (x);
}
extern __GNU_INLINE long double
fprem1(long double idend, long double div)
{
__asm__ __volatile__("fprem1" : "+t" (div) : "u" (idend) : "cc");
return (div);
}
extern __GNU_INLINE long double
fprem(long double idend, long double div)
{
__asm__ __volatile__("fprem" : "+t" (div) : "u" (idend) : "cc");
return (div);
}
extern __GNU_INLINE long double
fyl2xp1(long double y, long double x)
{
long double ret;
__asm__ __volatile__("fyl2xp1"
: "=t" (ret)
: "0" (x), "u" (y)
: "st(1)", "cc");
return (ret);
}
extern __GNU_INLINE long double
fsqrt(long double x)
{
__asm__ __volatile__("fsqrt" : "+t" (x) : : "cc");
return (x);
}
extern __GNU_INLINE long double
fsincos(long double x)
{
long double dummy;
__asm__ __volatile__("fsincos" : "+t" (x), "=u" (dummy) : : "cc");
return (x);
}
extern __GNU_INLINE long double
frndint(long double x)
{
__asm__ __volatile__("frndint" : "+t" (x) : : "cc");
return (x);
}
extern __GNU_INLINE long double
fscale(long double x, long double y)
{
long double ret;
__asm__ __volatile__("fscale" : "=t" (ret) : "0" (y), "u" (x) : "cc");
return (ret);
}
extern __GNU_INLINE long double
fsin(long double x)
{
__asm__ __volatile__("fsin" : "+t" (x) : : "cc");
return (x);
}
extern __GNU_INLINE long double
fcos(long double x)
{
__asm__ __volatile__("fcos" : "+t" (x) : : "cc");
return (x);
}
extern __GNU_INLINE void
sse_cmpeqss(float *f1, float *f2, int *i1)
{
__asm__ __volatile__(
"cmpeqss %2, %1\n\t"
"movss %1, %0"
: "=m" (*i1), "+x" (*f1)
: "x" (*f2)
: "cc");
}
extern __GNU_INLINE void
sse_cmpltss(float *f1, float *f2, int *i1)
{
__asm__ __volatile__(
"cmpltss %2, %1\n\t"
"movss %1, %0"
: "=m" (*i1), "+x" (*f1)
: "x" (*f2)
: "cc");
}
extern __GNU_INLINE void
sse_cmpless(float *f1, float *f2, int *i1)
{
__asm__ __volatile__(
"cmpless %2, %1\n\t"
"movss %1, %0"
: "=m" (*i1), "+x" (*f1)
: "x" (*f2)
: "cc");
}
extern __GNU_INLINE void
sse_cmpunordss(float *f1, float *f2, int *i1)
{
__asm__ __volatile__(
"cmpunordss %2, %1\n\t"
"movss %1, %0"
: "=m" (*i1), "+x" (*f1)
: "x" (*f2)
: "cc");
}
extern __GNU_INLINE void
sse_minss(float *f1, float *f2, float *f3)
{
__asm__ __volatile__(
"minss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f3), "+x" (*f1)
: "x" (*f2));
}
extern __GNU_INLINE void
sse_maxss(float *f1, float *f2, float *f3)
{
__asm__ __volatile__(
"maxss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f3), "+x" (*f1)
: "x" (*f2));
}
extern __GNU_INLINE void
sse_addss(float *f1, float *f2, float *f3)
{
__asm__ __volatile__(
"addss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f3), "+x" (*f1)
: "x" (*f2));
}
extern __GNU_INLINE void
sse_subss(float *f1, float *f2, float *f3)
{
__asm__ __volatile__(
"subss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f3), "+x" (*f1)
: "x" (*f2));
}
extern __GNU_INLINE void
sse_mulss(float *f1, float *f2, float *f3)
{
__asm__ __volatile__(
"mulss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f3), "+x" (*f1)
: "x" (*f2));
}
extern __GNU_INLINE void
sse_divss(float *f1, float *f2, float *f3)
{
__asm__ __volatile__(
"divss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f3), "+x" (*f1)
: "x" (*f2));
}
extern __GNU_INLINE void
sse_sqrtss(float *f1, float *f2)
{
double tmp;
__asm__ __volatile__(
"sqrtss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f2), "=x" (tmp)
: "m" (*f1));
}
extern __GNU_INLINE void
sse_ucomiss(float *f1, float *f2)
{
__asm__ __volatile__("ucomiss %1, %0" : : "x" (*f1), "x" (*f2));
}
extern __GNU_INLINE void
sse_comiss(float *f1, float *f2)
{
__asm__ __volatile__("comiss %1, %0" : : "x" (*f1), "x" (*f2));
}
extern __GNU_INLINE void
sse_cvtss2sd(float *f1, double *d1)
{
double tmp;
__asm__ __volatile__(
"cvtss2sd %2, %1\n\t"
"movsd %1, %0"
: "=m" (*d1), "=x" (tmp)
: "m" (*f1));
}
extern __GNU_INLINE void
sse_cvtsi2ss(int *i1, float *f1)
{
double tmp;
__asm__ __volatile__(
"cvtsi2ss %2, %1\n\t"
"movss %1, %0"
: "=m" (*f1), "=x" (tmp)
: "m" (*i1));
}
extern __GNU_INLINE void
sse_cvttss2si(float *f1, int *i1)
{
int tmp;
__asm__ __volatile__(
"cvttss2si %2, %1\n\t"
"movl %1, %0"
: "=m" (*i1), "=r" (tmp)
: "m" (*f1));
}
extern __GNU_INLINE void
sse_cvtss2si(float *f1, int *i1)
{
int tmp;
__asm__ __volatile__(
"cvtss2si %2, %1\n\t"
"movl %1, %0"
: "=m" (*i1), "=r" (tmp)
: "m" (*f1));
}
#if defined(__amd64)
extern __GNU_INLINE void
sse_cvtsi2ssq(long long *ll1, float *f1)
{
double tmp;
__asm__ __volatile__(
"cvtsi2ssq %2, %1\n\t"
"movss %1, %0"
: "=m" (*f1), "=x" (tmp)
: "m" (*ll1));
}
extern __GNU_INLINE void
sse_cvttss2siq(float *f1, long long *ll1)
{
uint64_t tmp;
__asm__ __volatile__(
"cvttss2siq %2, %1\n\t"
"movq %1, %0"
: "=m" (*ll1), "=r" (tmp)
: "m" (*f1));
}
extern __GNU_INLINE void
sse_cvtss2siq(float *f1, long long *ll1)
{
uint64_t tmp;
__asm__ __volatile__(
"cvtss2siq %2, %1\n\t"
"movq %1, %0"
: "=m" (*ll1), "=r" (tmp)
: "m" (*f1));
}
#endif
extern __GNU_INLINE void
sse_cmpeqsd(double *d1, double *d2, long long *ll1)
{
__asm__ __volatile__(
"cmpeqsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*ll1), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_cmpltsd(double *d1, double *d2, long long *ll1)
{
__asm__ __volatile__(
"cmpltsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*ll1), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_cmplesd(double *d1, double *d2, long long *ll1)
{
__asm__ __volatile__(
"cmplesd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*ll1), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_cmpunordsd(double *d1, double *d2, long long *ll1)
{
__asm__ __volatile__(
"cmpunordsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*ll1), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_minsd(double *d1, double *d2, double *d3)
{
__asm__ __volatile__(
"minsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d3), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_maxsd(double *d1, double *d2, double *d3)
{
__asm__ __volatile__(
"maxsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d3), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_addsd(double *d1, double *d2, double *d3)
{
__asm__ __volatile__(
"addsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d3), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_subsd(double *d1, double *d2, double *d3)
{
__asm__ __volatile__(
"subsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d3), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_mulsd(double *d1, double *d2, double *d3)
{
__asm__ __volatile__(
"mulsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d3), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_divsd(double *d1, double *d2, double *d3)
{
__asm__ __volatile__(
"divsd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d3), "+x" (*d1)
: "x" (*d2));
}
extern __GNU_INLINE void
sse_sqrtsd(double *d1, double *d2)
{
double tmp;
__asm__ __volatile__(
"sqrtsd %2, %1\n\t"
"movsd %1, %0"
: "=m" (*d2), "=x" (tmp)
: "m" (*d1));
}
extern __GNU_INLINE void
sse_ucomisd(double *d1, double *d2)
{
__asm__ __volatile__("ucomisd %1, %0" : : "x" (*d1), "x" (*d2));
}
extern __GNU_INLINE void
sse_comisd(double *d1, double *d2)
{
__asm__ __volatile__("comisd %1, %0" : : "x" (*d1), "x" (*d2));
}
extern __GNU_INLINE void
sse_cvtsd2ss(double *d1, float *f1)
{
double tmp;
__asm__ __volatile__(
"cvtsd2ss %2,%1\n\t"
"movss %1,%0"
: "=m" (*f1), "=x" (tmp)
: "m" (*d1));
}
extern __GNU_INLINE void
sse_cvtsi2sd(int *i1, double *d1)
{
double tmp;
__asm__ __volatile__(
"cvtsi2sd %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d1), "=x" (tmp)
: "m" (*i1));
}
extern __GNU_INLINE void
sse_cvttsd2si(double *d1, int *i1)
{
int tmp;
__asm__ __volatile__(
"cvttsd2si %2,%1\n\t"
"movl %1,%0"
: "=m" (*i1), "=r" (tmp)
: "m" (*d1));
}
extern __GNU_INLINE void
sse_cvtsd2si(double *d1, int *i1)
{
int tmp;
__asm__ __volatile__(
"cvtsd2si %2,%1\n\t"
"movl %1,%0"
: "=m" (*i1), "=r" (tmp)
: "m" (*d1));
}
#if defined(__amd64)
extern __GNU_INLINE void
sse_cvtsi2sdq(long long *ll1, double *d1)
{
double tmp;
__asm__ __volatile__(
"cvtsi2sdq %2,%1\n\t"
"movsd %1,%0"
: "=m" (*d1), "=x" (tmp)
: "m" (*ll1));
}
extern __GNU_INLINE void
sse_cvttsd2siq(double *d1, long long *ll1)
{
uint64_t tmp;
__asm__ __volatile__(
"cvttsd2siq %2,%1\n\t"
"movq %1,%0"
: "=m" (*ll1), "=r" (tmp)
: "m" (*d1));
}
extern __GNU_INLINE void
sse_cvtsd2siq(double *d1, long long *ll1)
{
uint64_t tmp;
__asm__ __volatile__(
"cvtsd2siq %2,%1\n\t"
"movq %1,%0"
: "=m" (*ll1), "=r" (tmp)
: "m" (*d1));
}
#endif
#elif defined(__sparc)
extern __GNU_INLINE void
__fenv_getfsr(unsigned long *l)
{
__asm__ __volatile__(
#if defined(__sparcv9)
"stx %%fsr,%0\n\t"
#else
"st %%fsr,%0\n\t"
#endif
: "=m" (*l));
}
extern __GNU_INLINE void
__fenv_setfsr(const unsigned long *l)
{
__asm__ __volatile__(
#if defined(__sparcv9)
"ldx %0,%%fsr\n\t"
#else
"ld %0,%%fsr\n\t"
#endif
: : "m" (*l) : "cc");
}
extern __GNU_INLINE void
__fenv_getfsr32(unsigned int *l)
{
__asm__ __volatile__("st %%fsr,%0\n\t" : "=m" (*l));
}
extern __GNU_INLINE void
__fenv_setfsr32(const unsigned int *l)
{
__asm__ __volatile__("ld %0,%%fsr\n\t" : : "m" (*l));
}
#else
#error "GCC FENV inlines not implemented for this platform"
#endif
#ifdef __cplusplus
}
#endif
#endif /* __GNUC__ */
#endif /* _FENV_INLINES_H */
|