go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/cmpbin/float.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmpbin 16 17 import ( 18 "encoding/binary" 19 "io" 20 "math" 21 ) 22 23 // WriteFloat64 writes a memcmp-sortable float to buf. It returns the number 24 // of bytes written (8, unless an error occurs), and any write error 25 // encountered. 26 func WriteFloat64(buf io.Writer, v float64) (n int, err error) { 27 bits := math.Float64bits(v) 28 bits = bits ^ (-(bits >> 63) | (1 << 63)) 29 data := make([]byte, 8) 30 binary.BigEndian.PutUint64(data, bits) 31 return buf.Write(data) 32 } 33 34 // ReadFloat64 reads a memcmp-sortable float from buf (as written by 35 // WriteFloat64). It also returns the number of bytes read (8, unless an error 36 // occurs), and any read error encountered. 37 func ReadFloat64(buf io.Reader) (ret float64, n int, err error) { 38 // byte-ordered floats http://stereopsis.com/radix.html 39 data := make([]byte, 8) 40 if n, err = buf.Read(data); err != nil { 41 return 42 } 43 bits := binary.BigEndian.Uint64(data) 44 ret = math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63))) 45 return 46 }