File: float64.go

package info (click to toggle)
golang-github-segmentio-encoding 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,468 kB
  • sloc: makefile: 286
file content (37 lines) | stat: -rw-r--r-- 739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package proto

import (
	"math"
	"unsafe"
)

var float64Codec = codec{
	wire:   fixed64,
	size:   sizeOfFloat64,
	encode: encodeFloat64,
	decode: decodeFloat64,
}

func sizeOfFloat64(p unsafe.Pointer, flags flags) int {
	if p != nil {
		if v := *(*float64)(p); v != 0 || flags.has(wantzero) || math.Signbit(v) {
			return 8
		}
	}
	return 0
}

func encodeFloat64(b []byte, p unsafe.Pointer, flags flags) (int, error) {
	if p != nil {
		if v := *(*float64)(p); v != 0 || flags.has(wantzero) || math.Signbit(v) {
			return encodeLE64(b, math.Float64bits(v))
		}
	}
	return 0, nil
}

func decodeFloat64(b []byte, p unsafe.Pointer, _ flags) (int, error) {
	v, n, err := decodeLE64(b)
	*(*float64)(p) = math.Float64frombits(v)
	return n, err
}