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: unsafe: add a func to convert bool type to int #65274

Closed
ahfuzhang opened this issue Jan 25, 2024 · 1 comment
Closed

proposal: unsafe: add a func to convert bool type to int #65274

ahfuzhang opened this issue Jan 25, 2024 · 1 comment
Labels
Milestone

Comments

@ahfuzhang
Copy link

Proposal Details

sometimes I don't want to use if when search a table.
So if we can convert a bool value to int, then we can reduce if clause .

For example:

var aTableToSearch = []int64{xxxx}
var a int = 1234

func getValueFromTable(index int) int64{
     if a>1000{
          return aTableToSearch[0]
     }
     return aTableToSearch[index]
}

We can change another way if we can convert bool to int:

var aTableToSearch = []int64{xxxx}
var a int = 1234

func getValueFromTable(index int) int64{
     return aTableToSearch[index*BoolToInt(!(a>1000))]
}

func BoolToInt(b bool) int{
    return int(*(*uint8)(unsafe.Pointer(&b)))  // this way is slow, use assemly
}
@gopherbot gopherbot added this to the Proposal milestone Jan 25, 2024
@ALTree ALTree closed this as completed Jan 25, 2024
@ALTree ALTree reopened this Jan 25, 2024
@ALTree
Copy link
Member

ALTree commented Jan 25, 2024

This is a dup of #64825, closing here in favour of the older thread.

@ALTree ALTree closed this as completed Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants