Re: [go-nuts] Generic integer math limitation

2022-02-05 Thread 'Axel Wagner' via golang-nuts
On Sat, Feb 5, 2022 at 7:38 PM Marcelo Cantos 
wrote:

> Explicit casting is no better, and there is no promotion (e.g., cast i to
> int and cast the result back to T) that's suitable for all integer types.
>

FWIW `uint64` and `int64` are large enough to represent any un/signed
integer types respectively.
Depending on the case, as long as you can assume that your values fall in
an appropriate range, you can use one of them.

I would like to shrink the block size for (u)int8, but there's no way to
> specialize the logic for specific types.
>
> I figured out that I can use *i >> 9* instead of *i / 512* in this
> instance, though there will be other cases (divide by non-Po2) where there
> isn't a simple workaround Is there a general way around this limitation?
>
> --
> 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/6a844487-0c22-443f-8e6e-c38860929515n%40googlegroups.com
> 
> .
>

-- 
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/CAEkBMfGbLYnivnwO-_d_Z%2B-PuhWZYaMUEqP-JH8Xh74_E_uFoA%40mail.gmail.com.


Re: [go-nuts] Generic integer math limitation

2022-02-05 Thread Ian Lance Taylor
On Sat, Feb 5, 2022 at 10:38 AM Marcelo Cantos  wrote:
>
> I am porting an int-set type to generics. It holds bits in a block structure 
> with 512 bits per block. At some point, I need to divide by 512:
>
> func block[T constraints.Integer](i T) T {
> return i / 512
> }
>
> Go 1.18 beta 2 complains: cannot convert 512 (untyped int constant) to T
>
> The problem is that uint8 and int8 cannot represent the number 512. If I 
> build a constraint similar to constraints.Integer that excludes ~uint8 and 
> ~int8, it works fine. Explicit casting is no better, and there is no 
> promotion (e.g., cast i to int and cast the result back to T) that's suitable 
> for all integer types. I would like to shrink the block size for (u)int8, but 
> there's no way to specialize the logic for specific types.
>
> I figured out that I can use i >> 9 instead of i / 512 in this instance, 
> though there will be other cases (divide by non-Po2) where there isn't a 
> simple workaround Is there a general way around this limitation?

It's not clear to me what is supposed to happen for the int8/uint8
case.  It sounds like your block type won't work for int8/uint8.  In
that case it seems like the right approach is to use a constraint that
doesn't permit it, as you suggest.  What other kind of workaround
would be appropriate?

Ian

-- 
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/CAOyqgcXHMRgJ1A1hKseHvkydA%2BKGO_b0sNevp-V6Gf3dxv3u7A%40mail.gmail.com.


[go-nuts] Generic integer math limitation

2022-02-05 Thread Marcelo Cantos
I am porting an int-set type to generics. It holds bits in a block 
structure with 512 bits per block. At some point, I need to divide by 512:



*func block[T constraints.Integer](i T) T {return i / 512}*

Go 1.18 beta 2 complains: *cannot convert 512 (untyped int constant) to T*

The problem is that uint8 and int8 cannot represent the number 512. If I 
build a constraint similar to constraints.Integer that excludes ~uint8 and 
~int8, it works fine. Explicit casting is no better, and there is no 
promotion (e.g., cast i to int and cast the result back to T) that's 
suitable for all integer types. I would like to shrink the block size for 
(u)int8, but there's no way to specialize the logic for specific types.

I figured out that I can use *i >> 9* instead of *i / 512* in this 
instance, though there will be other cases (divide by non-Po2) where there 
isn't a simple workaround Is there a general way around this limitation?

-- 
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/6a844487-0c22-443f-8e6e-c38860929515n%40googlegroups.com.