Thread overview
Re: how to do iota(0,256) with ubytes ? (cf need for iotaInclusive)
Oct 09, 2015
Timothee Cour
Oct 09, 2015
Adam D. Ruppe
Oct 09, 2015
Ali Çehreli
Oct 09, 2015
Dmitry Olshansky
October 09, 2015
also, workarounds involving:
iota(0,256).map!(a=>cast(ubyte)a)
are neither pleasant nor efficient


On Thu, Oct 8, 2015 at 7:41 PM, Timothee Cour <thelastmammoth@gmail.com> wrote:

> how to do iota(0,256) with ubytes ?
> and more generally:
> iota with 'end' parameter set to max range of a type.
>
> of course this doesn't work:
> auto b=iota(ubyte(0), ubyte(256));
> //cannot implicitly convert expression (256) of type int to ubyte
>
> Could we have a function with iota_inclusive that has inclusive bounds for 'end' parameter ?
>
>


October 09, 2015
On Friday, 9 October 2015 at 02:44:19 UTC, Timothee Cour wrote:
> also, workarounds involving:
> iota(0,256).map!(a=>cast(ubyte)a)
> are neither pleasant nor efficient

There's no difference in efficiency since that's how it'd probably be done on the low level anyway (assuming the compiler inlines it at least).

I suppose it could also be written something like

xor al, al;
again:
 // stuff here
inc al;
jnc again;


But I doubt there'd be a real performance difference between that and the int compare+cast version anyway.


I think the way you did it there is one of the better options. If you don't like how it looks, just wrap it in a little function so you only have to look at it once.

If you want it in phobos, you could submit the function for there too in a pull request.

October 09, 2015
On 10/08/2015 07:43 PM, Timothee Cour via Digitalmars-d wrote:
> also, workarounds involving:
> iota(0,256).map!(a=>cast(ubyte)a)
> are neither pleasant nor efficient
>
>
> On Thu, Oct 8, 2015 at 7:41 PM, Timothee Cour <thelastmammoth@gmail.com>
> wrote:
>
>> how to do iota(0,256) with ubytes ?
>> and more generally:
>> iota with 'end' parameter set to max range of a type.
>>
>> of course this doesn't work:
>> auto b=iota(ubyte(0), ubyte(256));
>> //cannot implicitly convert expression (256) of type int to ubyte
>>
>> Could we have a function with iota_inclusive that has inclusive bounds for
>> 'end' parameter ?
>>
>>
>

From an email to a friend earlier this year, without caring much about performance implications: :)

auto inclusive_iota(T)(T beg, T end)
{
    /* Chain the elements of 'iota' (which is end-exclusive)
     * and a range containing just the end element
     * 'only'. This returns a lazy range. */
    return chain(iota(beg, end), only(end));
}

Ali

October 09, 2015
On 09-Oct-2015 05:43, Timothee Cour via Digitalmars-d wrote:
> also, workarounds involving:
> iota(0,256).map!(a=>cast(ubyte)a)
> are neither pleasant nor efficient
>

Anything less then 32/64-bit is actually a tiny bit slower due to having to mask away top 8/16 bits since most of parameters passing and arithmetic is done on full words. Anyhow cast is 0-cost.

>
> On Thu, Oct 8, 2015 at 7:41 PM, Timothee Cour <thelastmammoth@gmail.com
> <mailto:thelastmammoth@gmail.com>> wrote:
>
>     how to do iota(0,256) with ubytes ?
>     and more generally:
>     iota with 'end' parameter set to max range of a type.
>
>     of course this doesn't work:
>     auto b=iota(ubyte(0), ubyte(256));
>     //cannot implicitly convert expression (256) of type int to ubyte
>
>     Could we have a function with iota_inclusive that has inclusive
>     bounds for 'end' parameter ?
>
>


-- 
Dmitry Olshansky