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

sync/atomic: some words are redundant in package doc. #22974

Closed
dotaheor opened this issue Dec 2, 2017 · 9 comments
Closed

sync/atomic: some words are redundant in package doc. #22974

dotaheor opened this issue Dec 2, 2017 · 9 comments

Comments

@dotaheor
Copy link

dotaheor commented Dec 2, 2017

The doc for the function AddUint32:

AddUint32 atomically adds delta to *addr and returns the new value. To subtract a signed positive constant value c from x, do AddUint32(&x, ^uint32(c-1)). In particular, to decrement x, do AddUint32(&x, ^uint32(0)). 

I think here, signed positive constant value should be replaced with positive value.
constant and signed are redundant.

Same problems for other AddUxxxxx functions.

@dotaheor dotaheor changed the title sync/atomic: some words are redundant and improper in package doc. sync/atomic: some words are redundant in package doc. Dec 2, 2017
@ianlancetaylor
Copy link
Contributor

It's not redundant, as the Go language rules make it harder than you think to subtract a signed positive constant value. To subtract a variable value, you can simply write AddUint32(&x, -v). That doesn't work for constant values because the resulting untyped constant can not be assigned to uint32.

Closing because I don't think we should change this.

@dotaheor
Copy link
Author

dotaheor commented Dec 2, 2017

How is it possible to call AddUint32(&x, -v) for an uint32 variable v?
Compiler will report error.

[edit] sorry, no errors are reported. Surprise!

@dotaheor
Copy link
Author

dotaheor commented Dec 2, 2017

btw, there are many a uintxxx uses in sync/atomic doc.
Should they be an uintxxx?

@dotaheor
Copy link
Author

dotaheor commented Dec 2, 2017

@ianlancetaylor I still think signed is redundant, why are unsigned constants excluded?
I can't find ways to subtract unsigned constant values, except the ^uint32(c-1) way.

Example

package main

import (
	"fmt"
	"sync/atomic"
)

func main() {
	var n uint32 = 100
	const d uint32 = 100
	// atomic.AddUint32(&n, -d) // constant -100 overflows uint32.
	atomic.AddUint32(&n, ^uint32(d - 1)) // this one is ok.
	fmt.Println(n)
}

@dotaheor
Copy link
Author

dotaheor commented Dec 3, 2017

Oh yeah, I found another way to subtract unsigned constant, if the unsigned constant is typed and its type is same as the second parameter type.

atomic.AddUint32(&n, ^(d - 1))

But if the type of the typed unsigned constant doesn't match he second parameter type, then the ^uint32(c-1) way still needs to be used.

@dotaheor
Copy link
Author

dotaheor commented Dec 4, 2017

@ianlancetaylor

That doesn't work for constant values because the resulting untyped constant can not be assigned to uint32.

but the doc says ... subtract a signed positive constant value c ..., "a signed constant" implies c is a typed constant, an operation with a typed constant c involved will always result a constant with the same type as c. So I think the reason of the trick ^uint32(c-1) is not for untyped constant can not be assigned to uint32.

@ianlancetaylor
Copy link
Contributor

Thanks for the comments. Let's agree to disagree.

@go101
Copy link

go101 commented Dec 6, 2017

but the docs really doesn't mention how to subtract an unsigned constant.

@ianlancetaylor
Copy link
Contributor

Go doesn't really have the concept of an unsigned constant. Go constants are untyped. Numeric constants are positive or negative. They are always signed. See https://golang.org/blog/constants.

@golang golang locked and limited conversation to collaborators Dec 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants