Extend decoding of int NaN values to include MaxInt (#4885)

This commit is contained in:
andig 2022-10-21 16:48:33 +02:00 • committed by GitHub
parent 269e120cdc
commit 058284145d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 14 deletions

View file

@ -53,28 +53,37 @@ func decodeBool16(mask uint64) func(b []byte) float64 {
}
}
func decodeNaN16(nan uint16, f func(b []byte) float64) func(b []byte) float64 {
func decodeNaN16(f func(b []byte) float64, nan ...uint16) func(b []byte) float64 {
return func(b []byte) float64 {
if binary.BigEndian.Uint16(b) == nan {
return 0
u := binary.BigEndian.Uint16(b)
for _, nan := range nan {
if u == nan {
return 0
}
}
return f(b)
}
}
func decodeNaN32(nan uint32, f func(b []byte) float64) func(b []byte) float64 {
func decodeNaN32(f func(b []byte) float64, nan ...uint32) func(b []byte) float64 {
return func(b []byte) float64 {
if binary.BigEndian.Uint32(b) == nan {
return 0
u := binary.BigEndian.Uint32(b)
for _, nan := range nan {
if u == nan {
return 0
}
}
return f(b)
}
}
func decodeNaN64(nan uint64, f func(b []byte) float64) func(b []byte) float64 {
func decodeNaN64(f func(b []byte) float64, nan ...uint64) func(b []byte) float64 {
return func(b []byte) float64 {
if binary.BigEndian.Uint64(b) == nan {
return 0
u := binary.BigEndian.Uint64(b)
for _, nan := range nan {
if u == nan {
return 0
}
}
return f(b)
}

View file

@ -309,13 +309,13 @@ func RegisterOperation(r Register) (rs485.Operation, error) {
op.Transform = asFloat64(encoding.Int16)
op.ReadLen = 1
case "int16nan":
op.Transform = decodeNaN16(1<<15, asFloat64(encoding.Int16))
op.Transform = decodeNaN16(asFloat64(encoding.Int16), 1<<15, 1<<15-1)
op.ReadLen = 1
case "uint16":
op.Transform = asFloat64(encoding.Uint16)
op.ReadLen = 1
case "uint16nan":
op.Transform = decodeNaN16(0xFFFF, asFloat64(encoding.Uint16))
op.Transform = decodeNaN16(asFloat64(encoding.Uint16), 1<<16-1)
op.ReadLen = 1
case "bool16":
mask, err := decodeMask(r.BitMask)
@ -329,7 +329,7 @@ func RegisterOperation(r Register) (rs485.Operation, error) {
case "int32":
op.Transform = asFloat64(encoding.Int32)
case "int32nan":
op.Transform = decodeNaN32(1<<31, asFloat64(encoding.Int32))
op.Transform = decodeNaN32(asFloat64(encoding.Int32), 1<<31, 1<<31-1)
case "int32s":
op.Transform = asFloat64(encoding.Int32LswFirst)
case "uint32":
@ -337,7 +337,7 @@ func RegisterOperation(r Register) (rs485.Operation, error) {
case "uint32s":
op.Transform = asFloat64(encoding.Uint32LswFirst)
case "uint32nan":
op.Transform = decodeNaN32(0xFFFFFFFF, asFloat64(encoding.Uint32))
op.Transform = decodeNaN32(asFloat64(encoding.Uint32), 1<<32-1)
case "float32", "ieee754":
op.Transform = asFloat64(encoding.Float32)
case "float32s", "ieee754s":
@ -348,7 +348,7 @@ func RegisterOperation(r Register) (rs485.Operation, error) {
op.Transform = asFloat64(encoding.Uint64)
op.ReadLen = 4
case "uint64nan":
op.Transform = decodeNaN64(0xFFFFFFFFFFFFFFFF, asFloat64(encoding.Uint64))
op.Transform = decodeNaN64(asFloat64(encoding.Uint64), 1<<64-1)
op.ReadLen = 4
case "float64":
op.Transform = encoding.Float64