float32_ext.go raw

   1  // Copyright (c) 2020-2022 Uber Technologies, Inc.
   2  //
   3  // Permission is hereby granted, free of charge, to any person obtaining a copy
   4  // of this software and associated documentation files (the "Software"), to deal
   5  // in the Software without restriction, including without limitation the rights
   6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   7  // copies of the Software, and to permit persons to whom the Software is
   8  // furnished to do so, subject to the following conditions:
   9  //
  10  // The above copyright notice and this permission notice shall be included in
  11  // all copies or substantial portions of the Software.
  12  //
  13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19  // THE SOFTWARE.
  20  
  21  package atomic
  22  
  23  import (
  24  	"math"
  25  	"strconv"
  26  )
  27  
  28  //go:generate bin/gen-atomicwrapper -name=Float32 -type=float32 -wrapped=Uint32 -pack=math.Float32bits -unpack=math.Float32frombits -swap -json -imports math -file=float32.go
  29  
  30  // Add atomically adds to the wrapped float32 and returns the new value.
  31  func (f *Float32) Add(delta float32) float32 {
  32  	for {
  33  		old := f.Load()
  34  		new := old + delta
  35  		if f.CAS(old, new) {
  36  			return new
  37  		}
  38  	}
  39  }
  40  
  41  // Sub atomically subtracts from the wrapped float32 and returns the new value.
  42  func (f *Float32) Sub(delta float32) float32 {
  43  	return f.Add(-delta)
  44  }
  45  
  46  // CAS is an atomic compare-and-swap for float32 values.
  47  //
  48  // Deprecated: Use CompareAndSwap
  49  func (f *Float32) CAS(old, new float32) (swapped bool) {
  50  	return f.CompareAndSwap(old, new)
  51  }
  52  
  53  // CompareAndSwap is an atomic compare-and-swap for float32 values.
  54  //
  55  // Note: CompareAndSwap handles NaN incorrectly. NaN != NaN using Go's inbuilt operators
  56  // but CompareAndSwap allows a stored NaN to compare equal to a passed in NaN.
  57  // This avoids typical CompareAndSwap loops from blocking forever, e.g.,
  58  //
  59  //	for {
  60  //	  old := atom.Load()
  61  //	  new = f(old)
  62  //	  if atom.CompareAndSwap(old, new) {
  63  //	    break
  64  //	  }
  65  //	}
  66  //
  67  // If CompareAndSwap did not match NaN to match, then the above would loop forever.
  68  func (f *Float32) CompareAndSwap(old, new float32) (swapped bool) {
  69  	return f.v.CompareAndSwap(math.Float32bits(old), math.Float32bits(new))
  70  }
  71  
  72  // String encodes the wrapped value as a string.
  73  func (f *Float32) String() string {
  74  	// 'g' is the behavior for floats with %v.
  75  	return strconv.FormatFloat(float64(f.Load()), 'g', -1, 32)
  76  }
  77