11/18/2012 4:41 PM, Manu пишет:
        The alternative us to use ushort everywhere, which is awkward,
        because
        it is neither unsigned, nor is it an integer, and it's not typesafe
        (allows direct assignment to ints and stuff)...
        It would be nice if: cast(half)someFloat would yield the proper
        value,
        even if it is performed in software in most architectures, it
        could be
        mapped to hardware for those that do it.


    I guess half(someFloat) will do for conversion.


How do you define 'will do'? It still behaves differently than proper types.
someFloat = someDouble without a cast is a compile error, likewise
should be true in this case, but I don't know how to do that.
someHalf = someFloat should require an explicit cast too, or use a 1.0h
literal ;)

The point was it does now. Wrapper struct doesn't accept direct assignments from float (unless you want it to and defined opAssign to do that).

alias this trick makes it magical r-value of type float where it makes sense. Obviously on assignment it doesn't ;)

In code:
half myHalf = half(1.23);
myHalf = 35; //doesn't compile
myHalf = half(35); //works

The example though demonstrates one painful limitation of a wrapper - no value range propagation. Basically since 35 fits into a half, no explicit cast should be needed.

And that's a big deal sometimes:
ubyte flags = 0x80 | 0x40; //no casts, thank God

    Everything but hardware support is doable as is. I'm not sure if
    it's possible and/or feasible to make _efficient_ wrapper type that
    uses hardware support.

    The easiest path seems to be:
    - convince GDC to add __fp16 type as an extension that maps to GCC's one
    - use it on GDC, and fallback to emulation on DMD

    That being said I personally have no objections to add half type to
    built-ins in DMD.


I'm sure it's already accessible in GDC,

Good to know.

The point would be to name the type, add the conversion functions to
druntime for fallback/portability, and a literal 1.0h would be handy to
identify the type to templates.

Mmm the literal. Dunno but '1h' looks like 1 hour to me :)

Another trick to pull is to use UFCS:

@property auto hf(T)(T value)
if(isFloatingPoint!T || isIntegral!T)
{
        return half(value);
}

Then 1.0.hf && 32.hf both should work. Not as nice as true suffix but damn close.


--
Dmitry Olshansky

Reply via email to