Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rocket049 opened this issue Jun 2, 2020 · 4 comments
Labels
FrozenDueToAge Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@rocket049
Copy link

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

  1. StoreFloat64
  2. LoadFloat64
  3. AddFloat64
  4. StoreFloat32
  5. LoadFloat32
  6. AddFloat32
@gopherbot gopherbot added this to the Proposal milestone Jun 2, 2020
@mdlayher
Copy link
Member

mdlayher commented Jun 2, 2020

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.

@mdlayher mdlayher added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 2, 2020
@jech
Copy link

jech commented Jun 2, 2020

That happened to me too.

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

@nanokatze
Copy link

nanokatze commented Jun 4, 2020

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.

@gopherbot
Copy link

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.)

@golang golang locked and limited conversation to collaborators Jul 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants