Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Unified Diff: common/cmpbin/float.go

Issue 1219323002: Rename funnybase and add more serializations. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: fixes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/cmpbin/doc.go ('k') | common/cmpbin/float_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/cmpbin/float.go
diff --git a/common/cmpbin/float.go b/common/cmpbin/float.go
new file mode 100644
index 0000000000000000000000000000000000000000..35dcdd1f7fbd6028983cd8dc1c1acf9d5c173f60
--- /dev/null
+++ b/common/cmpbin/float.go
@@ -0,0 +1,36 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package cmpbin
+
+import (
+ "encoding/binary"
+ "io"
+ "math"
+)
+
+// WriteFloat64 writes a memcmp-sortable float to buf. It returns the number
+// of bytes written (8, unless an error occurs), and any write error
+// encountered.
+func WriteFloat64(buf io.Writer, v float64) (n int, err error) {
+ bits := math.Float64bits(v)
+ bits = bits ^ (-(bits >> 63) | (1 << 63))
+ data := make([]byte, 8)
+ binary.BigEndian.PutUint64(data, bits)
+ return buf.Write(data)
+}
+
+// ReadFloat64 reads a memcmp-sortable float from buf (as written by
+// WriteFloat64). It also returns the number of bytes read (8, unless an error
+// occurs), and any read error encountered.
+func ReadFloat64(buf io.Reader) (ret float64, n int, err error) {
+ // byte-ordered floats http://stereopsis.com/radix.html
+ data := make([]byte, 8)
+ if n, err = buf.Read(data); err != nil {
+ return
+ }
+ bits := binary.BigEndian.Uint64(data)
+ ret = math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63)))
+ return
+}
« no previous file with comments | « common/cmpbin/doc.go ('k') | common/cmpbin/float_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698