Commit 2f4d35ff authored by Rob Pike's avatar Rob Pike

converting uint bits back into floats

R=rsc
DELTA=32  (32 added, 0 deleted, 0 changed)
OCL=19084
CL=19091
parent 2727abe4
......@@ -51,6 +51,8 @@ export func Inf(int) float64; // return signed Inf
export func NaN() float64; // return a NaN
export func float32bits(float32) uint32; // 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,
keyalg int, valalg int,
......
......@@ -41,6 +41,8 @@ char *sysimport =
"export func sys.NaN () (? float64)\n"
"export func sys.float32bits (? float32) (? uint32)\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.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"
......
......@@ -204,6 +204,19 @@ float64frombits(uint64 i)
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
isInf(float64 f, int32 sign)
{
......@@ -387,6 +400,21 @@ sys·float64bits(float64 din, uint64 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 uint8** argv;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment