Skip to content

proposal: sync/atomic: add logical operations #31748

Closed
@bartoszmodelski2

Description

@bartoszmodelski2

x86, x64 support atomic instructions for NOT, AND, OR, XOR. I believe, exposing them has some advantages:

  • higher performance than Load&CompAndSwap
  • cleaner code
  • it also reduces the risk of contention in some applications, hence increased stability

As for other architectures, implementing these operations in assembly should yield some considerable speed-up and said cleaner code.

Example application: bloom filters.

Example AND/OR implementations for int64, amd64:

//declarations.go
//go:noescape
func And(ptr *int64, delta int64)

//go:noescape
func Or(ptr *int64, delta int64)

//operations.s
#include "textflag.h"

TEXT ·Or(SB), NOSPLIT, $0-16
	MOVQ	ptr+0(FP), BX       
	MOVQ	delta+8(FP), AX    
	LOCK	
   	ORQ	AX, 0(BX)               

	RET

TEXT ·And(SB), NOSPLIT, $0-16
	MOVQ	ptr+0(FP), BX     
	MOVQ	delta+8(FP), AX    

    	LOCK	
    	ANDQ	AX, 0(BX)           

	RET

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions