Thank you for the responses. Unfortunately, they all comment on the given 
use case. 
The real question is if we should add a function in math/bits that performs 
the operation. 
The function would be 

func notZero(x int) int {
    if x == 0 {
        return 0
    }
    return 1
}

Regarding code optimization, do they also optimize range testing ?

if x >= a && x <= b ...

can be translated into the following when b > a. 

if uint(x - a) <= uint(b-a) ...

This replaces two conditional branches with one. I checked go assembly and 
the later is indeed simpler. 

I benchmarked decode rune with both and didn't see much difference. This is 
probably because in a tight loop of benchmark tests, the branching 
prediction does well its job. 


Le samedi 21 novembre 2020 à 10:30:13 UTC+1, b.ca...@pobox.com a écrit :

> That's fantastic.  Paste in
>
> int Test0(int v, int v1, int v2, int v3)
> {
>      return v == v1 || v == v2 || v == v3;
> }
>
> and then the the compiler options to -O3.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e0be58e6-d836-4200-8d4d-a422f003ced2n%40googlegroups.com.

Reply via email to