Go: proposal: sync/atomic: Add StoreFloat64/LoadFloat64/AddFloat64/StoreFloat32/LoadFloat32/AddFloat32

Created on 2 Jun 2020  ·  4Comments  ·  Source: golang/go

I think it is necessary to add floating-point operation function!

  1. StoreFloat64
  2. LoadFloat64
  3. AddFloat64
  4. StoreFloat32
  5. LoadFloat32
  6. AddFloat32
Proposal WaitingForInfo

Most helpful comment

You haven't provided any justification or concrete use cases as to why these functions would be necessary to add to the standard library. Could you please clarify?

In addition, you could use https://golang.org/pkg/math/#Float64bits and etc to reinterpret floating point values as uint32/64 and use the existing atomic operations for those types.

All 4 comments

You haven't provided any justification or concrete use cases as to why these functions would be necessary to add to the standard library. Could you please clarify?

In addition, you could use https://golang.org/pkg/math/#Float64bits and etc to reinterpret floating point values as uint32/64 and use the existing atomic operations for those types.

That happened to me too.

v := atomic.LoadInt64(&a)
w := math.Float64FromBits(v);

You can implement any x ← f(x) atomic construction (provided there exist load and compare and swap operations for x-sized words) using compare and swap loop. For example, AddFloat64 would be implemented as follows:

func AddFloat64(_addr *float64, delta float64) (new float64) {
    addr := (*uint64)(unsafe.Pointer(_addr))
    for {
        x := atomic.LoadUint64(addr)
        y := math.Float64frombits(x) + delta
        if atomic.CompareAndSwapUint64(addr, x, math.Float64bits(y)) {
            return y
        }
    }
}

Note that float64 would need to be 8-aligned on 32 bit targets. Requirements for uint64 apply here as well.

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

Was this page helpful?
0 / 5 - 0 ratings