]> Cypherpunks repositories - gostls13.git/commitdiff
sync/atomic: explain how to subtract an unsigned constant
authorRob Pike <r@golang.org>
Thu, 3 Oct 2013 17:40:42 +0000 (10:40 -0700)
committerRob Pike <r@golang.org>
Thu, 3 Oct 2013 17:40:42 +0000 (10:40 -0700)
Explain for those unfamiliar with twos-complement arithmetic how to
implement negation of signed positive constant.
Fixes #6408.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/14267044

src/pkg/sync/atomic/doc.go

index 34a2bc1390b2f258f35770576b8fb7ee40ee8cc4..17ba72fa17187aa4fa31d4ffaba3c6eab9f4b14a 100644 (file)
@@ -94,12 +94,16 @@ func CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) (swapp
 func AddInt32(addr *int32, delta int32) (new int32)
 
 // 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)).
 func AddUint32(addr *uint32, delta uint32) (new uint32)
 
 // AddInt64 atomically adds delta to *addr and returns the new value.
 func AddInt64(addr *int64, delta int64) (new int64)
 
 // AddUint64 atomically adds delta to *addr and returns the new value.
+// To subtract a signed positive constant value c from x, do AddUint64(&x, ^uint64(c-1)).
+// In particular, to decrement x, do AddUint64(&x, ^uint64(0)).
 func AddUint64(addr *uint64, delta uint64) (new uint64)
 
 // AddUintptr atomically adds delta to *addr and returns the new value.