2021-03-01 04:38:33 +05:30
|
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
2020-08-18 09:53:45 +05:30
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2021-03-01 04:38:33 +05:30
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-08-18 09:53:45 +05:30
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2021-03-01 04:38:33 +05:30
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
package atomic
|
|
|
|
|
|
|
|
import "strconv"
|
2020-08-18 09:53:45 +05:30
|
|
|
|
2021-03-01 04:38:33 +05:30
|
|
|
//go:generate bin/gen-atomicwrapper -name=Float64 -type=float64 -wrapped=Uint64 -pack=math.Float64bits -unpack=math.Float64frombits -cas -json -imports math -file=float64.go
|
2020-08-18 09:53:45 +05:30
|
|
|
|
2021-03-01 04:38:33 +05:30
|
|
|
// Add atomically adds to the wrapped float64 and returns the new value.
|
|
|
|
func (f *Float64) Add(s float64) float64 {
|
|
|
|
for {
|
|
|
|
old := f.Load()
|
|
|
|
new := old + s
|
|
|
|
if f.CAS(old, new) {
|
|
|
|
return new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 09:53:45 +05:30
|
|
|
|
2021-03-01 04:38:33 +05:30
|
|
|
// Sub atomically subtracts from the wrapped float64 and returns the new value.
|
|
|
|
func (f *Float64) Sub(s float64) float64 {
|
|
|
|
return f.Add(-s)
|
|
|
|
}
|
2020-08-18 09:53:45 +05:30
|
|
|
|
2021-03-01 04:38:33 +05:30
|
|
|
// String encodes the wrapped value as a string.
|
|
|
|
func (f *Float64) String() string {
|
|
|
|
// 'g' is the behavior for floats with %v.
|
|
|
|
return strconv.FormatFloat(f.Load(), 'g', -1, 64)
|
|
|
|
}
|