1
0
mirror of https://github.com/golang/go synced 2024-11-12 09:50:21 -07:00

converting uint bits back into floats

R=rsc
DELTA=32  (32 added, 0 deleted, 0 changed)
OCL=19084
CL=19091
This commit is contained in:
Rob Pike 2008-11-12 11:51:34 -08:00
parent 2727abe4fc
commit 2f4d35ffb9
3 changed files with 32 additions and 0 deletions

View File

@ -51,6 +51,8 @@ export func Inf(int) float64; // return signed Inf
export func NaN() float64; // return a NaN export func NaN() float64; // return a NaN
export func float32bits(float32) uint32; // raw bits export func float32bits(float32) uint32; // raw bits
export func float64bits(float64) uint64; // raw bits export func float64bits(float64) uint64; // raw bits
export func float32frombits(uint32) float32; // raw bits
export func float64frombits(uint64) float64; // raw bits
export func newmap(keysize int, valsize int, export func newmap(keysize int, valsize int,
keyalg int, valalg int, keyalg int, valalg int,

View File

@ -41,6 +41,8 @@ char *sysimport =
"export func sys.NaN () (? float64)\n" "export func sys.NaN () (? float64)\n"
"export func sys.float32bits (? float32) (? uint32)\n" "export func sys.float32bits (? float32) (? uint32)\n"
"export func sys.float64bits (? float64) (? uint64)\n" "export func sys.float64bits (? float64) (? uint64)\n"
"export func sys.float32frombits (? uint32) (? float32)\n"
"export func sys.float64frombits (? uint64) (? float64)\n"
"export func sys.newmap (keysize int, valsize int, keyalg int, valalg int, hint int) (hmap *map[any] any)\n" "export func sys.newmap (keysize int, valsize int, keyalg int, valalg int, hint int) (hmap *map[any] any)\n"
"export func sys.mapaccess1 (hmap *map[any] any, key any) (val any)\n" "export func sys.mapaccess1 (hmap *map[any] any, key any) (val any)\n"
"export func sys.mapaccess2 (hmap *map[any] any, key any) (val any, pres bool)\n" "export func sys.mapaccess2 (hmap *map[any] any, key any) (val any, pres bool)\n"

View File

@ -204,6 +204,19 @@ float64frombits(uint64 i)
return u.f; return u.f;
} }
static float32
float32frombits(uint32 i)
{
// The obvious cast-and-pointer code is technically
// not valid, and gcc miscompiles it. Use a union instead.
union {
float32 f;
uint32 i;
} u;
u.i = i;
return u.f;
}
bool bool
isInf(float64 f, int32 sign) isInf(float64 f, int32 sign)
{ {
@ -387,6 +400,21 @@ sys·float64bits(float64 din, uint64 iou)
FLUSH(&iou); FLUSH(&iou);
} }
// func float32frombits(uint32) float32; // raw bits to float32
void
sys·float32frombits(uint32 uin, float32 dou)
{
dou = float32frombits(uin);
FLUSH(&dou);
}
// func float64frombits(uint64) float64; // raw bits to float64
void
sys·float64frombits(uint64 uin, float64 dou)
{
dou = float64frombits(uin);
FLUSH(&dou);
}
static int32 argc; static int32 argc;
static uint8** argv; static uint8** argv;