atomicbitops: Add atomic float64.

This adds a new atomic type, `atomicbitops.Float64`, which has similar
operations as `atomicbitops.Uint64`. It actually uses `atomicbitops.Uint64`
for storing its bits.

`atomicbitops.Float64` supports `Swap`, `CompareAndSwap`, and `Add`
operations.

This is useful in gVisor's metric library for keeping track of the
sum-of-squared-deviation statistic of distribution metrics.

PiperOrigin-RevId: 537127647
This commit is contained in:
Etienne Perot
2023-06-01 14:24:59 -07:00
committed by gVisor bot
parent c006f01e0e
commit cb0481301f
3 changed files with 298 additions and 0 deletions
+1
View File
@@ -16,6 +16,7 @@ go_library(
"atomicbitops_amd64.s",
"atomicbitops_arm64.go",
"atomicbitops_arm64.s",
"atomicbitops_float64.go",
"atomicbitops_noasm.go",
"bool.go",
],
+105
View File
@@ -0,0 +1,105 @@
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package atomicbitops
import (
"math"
"gvisor.dev/gvisor/pkg/sync"
)
// Float64 is an atomic 64-bit floating-point number.
//
// +stateify savable
type Float64 struct {
_ sync.NoCopy
// bits stores the bit of a 64-bit floating point number.
// It is not (and should not be interpreted as) a real uint64.
bits Uint64
}
// FromFloat64 returns a Float64 initialized to value v.
//
//go:nosplit
func FromFloat64(v float64) Float64 {
return Float64{bits: FromUint64(math.Float64bits(v))}
}
// Load loads the floating-point value.
//
//go:nosplit
func (f *Float64) Load() float64 {
return math.Float64frombits(f.bits.Load())
}
// RacyLoad is analogous to reading an atomic value without using
// synchronization.
//
// It may be helpful to document why a racy operation is permitted.
//
//go:nosplit
func (f *Float64) RacyLoad() float64 {
return math.Float64frombits(f.bits.RacyLoad())
}
// Store stores the given floating-point value in the Float64.
//
//go:nosplit
func (f *Float64) Store(v float64) {
f.bits.Store(math.Float64bits(v))
}
// RacyStore is analogous to setting an atomic value without using
// synchronization.
//
// It may be helpful to document why a racy operation is permitted.
//
//go:nosplit
func (f *Float64) RacyStore(v float64) {
f.bits.RacyStore(math.Float64bits(v))
}
// Swap stores the given value and returns the previously-stored one.
//
//go:nosplit
func (f *Float64) Swap(v float64) float64 {
return math.Float64frombits(f.bits.Swap(math.Float64bits(v)))
}
// CompareAndSwap does a compare-and-swap operation on the float64 value.
// Note that unlike typical IEEE 754 semantics, this function will treat NaN
// as equal to itself if all of its bits exactly match.
//
//go:nosplit
func (f *Float64) CompareAndSwap(oldVal, newVal float64) bool {
return f.bits.CompareAndSwap(math.Float64bits(oldVal), math.Float64bits(newVal))
}
// Add increments the float by the given value.
// Note that unlike an atomic integer, this requires spin-looping until we win
// the compare-and-swap race, so this may take an indeterminate amount of time.
//
//go:nosplit
func (f *Float64) Add(v float64) {
// We do a racy load here because we optimistically think it may pass the
// compare-and-swap operation. If it doesn't, we'll load it safely, so this
// is OK and not a race for the overall intent of the user to add a number.
sync.RaceDisable()
oldVal := f.RacyLoad()
for !f.CompareAndSwap(oldVal, oldVal+v) {
oldVal = f.Load()
}
sync.RaceEnable()
}
+192
View File
@@ -16,6 +16,8 @@
package atomicbitops
import (
"fmt"
"math"
"runtime"
"testing"
@@ -197,3 +199,193 @@ func TestCompareAndSwapUint64(t *testing.T) {
}
}
}
var interestingFloats = []float64{
0.0,
1.0,
0.1,
2.1,
-1.0,
-0.1,
-2.1,
math.MaxFloat64,
-math.MaxFloat64,
math.SmallestNonzeroFloat64,
-math.SmallestNonzeroFloat64,
math.Inf(1),
math.Inf(-1),
math.NaN(),
}
// equalOrBothNaN returns true if a == b or if a and b are both NaN.
func equalOrBothNaN(a, b float64) bool {
return a == b || (math.IsNaN(a) && math.IsNaN(b))
}
// getInterestingFloatPermutations returns a list of `num`-sized permutations
// of the floating-point values in `interestingFloats`.
func getInterestingFloatPermutations(num int) [][]float64 {
permutations := make([][]float64, 0, len(interestingFloats))
for _, f := range interestingFloats {
permutations = append(permutations, []float64{f})
}
for i := 1; i < num; i++ {
oldPermutations := permutations
permutations = make([][]float64, 0, len(permutations)*len(interestingFloats))
for _, oldPermutation := range oldPermutations {
for _, f := range interestingFloats {
alreadyInPermutation := false
for _, f2 := range oldPermutation {
if equalOrBothNaN(f, f2) {
alreadyInPermutation = true
break
}
}
if alreadyInPermutation {
continue
}
permutations = append(permutations, append(oldPermutation, f))
}
}
}
return permutations
}
func TestCompareAndSwapFloat64(t *testing.T) {
for _, floats := range getInterestingFloatPermutations(3) {
a, b, c := floats[0], floats[1], floats[2]
t.Run(fmt.Sprintf("a=%v b=%v c=%v", a, b, c), func(t *testing.T) {
tests := []struct {
name string
prev float64
old float64
new float64
next float64
}{
{
name: "Successful compare-and-swap with prev == new",
prev: a,
old: a,
new: a,
next: a,
},
{
name: "Successful compare-and-swap with prev != new",
prev: a,
old: a,
new: b,
next: b,
},
{
name: "Failed compare-and-swap with prev == new",
prev: a,
old: b,
new: a,
next: a,
},
{
name: "Failed compare-and-swap with prev != new",
prev: a,
old: b,
new: c,
next: a,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
val := FromFloat64(test.prev)
success := val.CompareAndSwap(test.old, test.new)
wantSuccess := equalOrBothNaN(test.prev, test.old) && equalOrBothNaN(test.new, test.next)
if success != wantSuccess {
t.Errorf("incorrect success value: got %v, expected %v", success, wantSuccess)
}
if got, want := val.Load(), test.next; !equalOrBothNaN(got, want) {
t.Errorf("incorrect value stored in val: got %v, expected %v", got, want)
}
})
}
})
}
}
func TestAddFloat64(t *testing.T) {
runtime.GOMAXPROCS(100)
for _, floats := range getInterestingFloatPermutations(3) {
a, b, c := floats[0], floats[1], floats[2]
// This test computes the outcome of adding `b` and `c` to `a`.
// Because floating point numbers lose precision with each operation,
// it is not always the case that a + b + c = a + c + b.
// Therefore, it computes both a + b + c and a + c + b, and verifies that
// adding Float64s in that order works exactly, while Float64s to which
// `b` and `c` are added in separate goroutines may end up at either
// `a + b + c` or `a + c + b`.
testName := fmt.Sprintf("a=%v b=%v c=%v", a, b, c)
for i := 0; i < iterations; i++ {
fCanonical := a
fCanonicalReverse := a
fLinear := FromFloat64(a)
fLinearReverse := FromFloat64(a)
fParallel1 := FromFloat64(a)
fParallel2 := FromFloat64(a)
var wg sync.WaitGroup
spawn := func(f func()) {
wg.Add(1)
go func() {
defer wg.Done()
f()
}()
}
spawn(func() {
fCanonical += b
fCanonical += c
})
spawn(func() {
fCanonicalReverse += c
fCanonicalReverse += b
})
spawn(func() {
fLinear.Add(b)
fLinear.Add(c)
})
spawn(func() {
fLinearReverse.Add(c)
fLinearReverse.Add(b)
})
spawn(func() {
fParallel1.Add(b)
})
spawn(func() {
fParallel2.Add(c)
})
spawn(func() {
fParallel1.Add(c)
})
spawn(func() {
fParallel2.Add(b)
})
wg.Wait()
for _, f := range []struct {
name string
val float64
want []float64
}{
{"linear", fLinear.Load(), []float64{fCanonical}},
{"linear reverse", fLinearReverse.Load(), []float64{fCanonicalReverse}},
{"parallel 1", fParallel1.Load(), []float64{fCanonical, fCanonicalReverse}},
{"parallel 2", fParallel2.Load(), []float64{fCanonical, fCanonicalReverse}},
} {
found := false
for _, want := range f.want {
if equalOrBothNaN(f.val, want) {
found = true
break
}
}
if !found {
t.Errorf("%s: %s was not equal to expected result: %v not in %v", testName, f.name, f.val, f.want)
}
}
}
}
}