xref: /plan9/sys/src/9/port/fpi.h (revision 6f1905eb1326e6145a15c8ecfc60c3b9791adc25)
1 #ifndef nil
2 #include <u.h>
3 #endif
4 
5 typedef long Word;
6 typedef long long Vlong;
7 typedef unsigned long Single;
8 
9 /* use u.h's FPdbleword */
10 #define Double	FPdbleword
11 #define h hi
12 #define l lo
13 
14 enum {
15 	FractBits	= 28,
16 	CarryBit	= 0x10000000,
17 	HiddenBit	= 0x08000000,
18 	MsBit		= HiddenBit,
19 	NGuardBits	= 3,
20 	GuardMask	= 0x07,
21 	LsBit		= (1<<NGuardBits),
22 
23 	SingleExpBias	= 127,
24 	SingleExpMax	= 255,
25 	DoubleExpBias	= 1023,
26 	DoubleExpMax	= 2047,
27 
28 	ExpBias		= DoubleExpBias,
29 	ExpInfinity	= DoubleExpMax,
30 };
31 
32 typedef struct {
33 	/* order matters: must start with s, e, l, h in that order */
34 	unsigned char s;
35 	short e;
36 	/* double bits */
37 	long l;				/* 0000FFFFFFFFFFFFFFFFFFFFFFFFFGGG */
38 	long h;				/* 0000HFFFFFFFFFFFFFFFFFFFFFFFFFFF */
39 } Internal;
40 
41 #define IsWeird(n)	((n)->e >= ExpInfinity)
42 #define	IsInfinity(n)	(IsWeird(n) && (n)->h == HiddenBit && (n)->l == 0)
43 #define	SetInfinity(n)	((n)->e = ExpInfinity, (n)->h = HiddenBit, (n)->l = 0)
44 #define IsNaN(n)	(IsWeird(n) && (((n)->h & ~HiddenBit) || (n)->l))
45 #define	SetQNaN(n)	((n)->s = 0, (n)->e = ExpInfinity, 		\
46 			 (n)->h = HiddenBit|(LsBit<<1), (n)->l = 0)
47 #define IsZero(n)	((n)->e == 1 && (n)->h == 0 && (n)->l == 0)
48 #define SetZero(n)	((n)->e = 1, (n)->h = 0, (n)->l = 0)
49 
50 /*
51  * fpi.c
52  */
53 extern void fpiround(Internal *);
54 extern void fpiadd(Internal *, Internal *, Internal *);
55 extern void fpisub(Internal *, Internal *, Internal *);
56 extern void fpimul(Internal *, Internal *, Internal *);
57 extern void fpidiv(Internal *, Internal *, Internal *);
58 extern int fpicmp(Internal *, Internal *);
59 extern void fpinormalise(Internal*);
60 
61 /*
62  * fpimem.c
63  */
64 extern void fpis2i(Internal *, void *);
65 extern void fpid2i(Internal *, void *);
66 extern void fpiw2i(Internal *, void *);
67 extern void fpiv2i(Internal *, void *);
68 extern void fpii2s(void *, Internal *);
69 extern void fpii2d(void *, Internal *);
70 extern void fpii2w(Word *, Internal *);
71 extern void fpii2v(Vlong *, Internal *);
72