github.com/segmentio/encoding@v0.4.0/proto/float64.go (about)

     1  package proto
     2  
     3  import (
     4  	"math"
     5  	"unsafe"
     6  )
     7  
     8  var float64Codec = codec{
     9  	wire:   fixed64,
    10  	size:   sizeOfFloat64,
    11  	encode: encodeFloat64,
    12  	decode: decodeFloat64,
    13  }
    14  
    15  func sizeOfFloat64(p unsafe.Pointer, flags flags) int {
    16  	if p != nil {
    17  		if v := *(*float64)(p); v != 0 || flags.has(wantzero) || math.Signbit(v) {
    18  			return 8
    19  		}
    20  	}
    21  	return 0
    22  }
    23  
    24  func encodeFloat64(b []byte, p unsafe.Pointer, flags flags) (int, error) {
    25  	if p != nil {
    26  		if v := *(*float64)(p); v != 0 || flags.has(wantzero) || math.Signbit(v) {
    27  			return encodeLE64(b, math.Float64bits(v))
    28  		}
    29  	}
    30  	return 0, nil
    31  }
    32  
    33  func decodeFloat64(b []byte, p unsafe.Pointer, _ flags) (int, error) {
    34  	v, n, err := decodeLE64(b)
    35  	*(*float64)(p) = math.Float64frombits(v)
    36  	return n, err
    37  }