On 18 November 2012 20:57, Artur Skawina <span dir="ltr"><<a href="mailto:art.08.09@gmail.com" target="_blank">art.08.09@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
On 11/18/12 16:32, Manu wrote:<br>
<div class="im">> On 18 November 2012 16:33, Artur Skawina <<a href="mailto:art.08.09@gmail.com">art.08.09@gmail.com</a> <mailto:<a href="mailto:art.08.09@gmail.com">art.08.09@gmail.com</a>>> wrote:<br>
><br>
> On 11/18/12 13:58, Manu wrote:<br>
</div><div><div class="h5">> > On 18 November 2012 14:51, Artur Skawina <<a href="mailto:art.08.09@gmail.com">art.08.09@gmail.com</a> <mailto:<a href="mailto:art.08.09@gmail.com">art.08.09@gmail.com</a>> <mailto:<a href="mailto:art.08.09@gmail.com">art.08.09@gmail.com</a> <mailto:<a href="mailto:art.08.09@gmail.com">art.08.09@gmail.com</a>>>> wrote:<br>
> ><br>
> > On 11/18/12 12:21, Manu wrote:<br>
> > > I've often wondered about having an official 'half' type.<br>
> > > It's very common in rendering/image processing, supported by most video cards (so compression routines interacting with this type are common), and it's also supported in hardware by some cpu's.<br>
> > ><br>
> > > ARM for instance supports 'half's in hardware, and GCC has an __fp16 type which would map nicely if D supported the type in the front end.<br>
> > ><br>
> > > 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)...<br>
> > > 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.<br>
> > ><br>
> > > It could be done in a library, but then GCC couldn't map it properly to the hardware type, and since D has no way to describe implicit casts (that I know of?) it becomes awkward to use.<br>
> > > someFloat = someHalf <- doesn't work, because a cast operator expects an explicit cast, even though this is a lossless conversion and should be exactly the same as someDouble = someFloat.<br>
> > ><br>
> > > Thoughts?<br>
> ><br>
> > version (GNU) alias half = @gcc[mode(HF)] float;<br>
> ><br>
> > But of course this won't work right now, cause that kind of type attributes<br>
> > aren't supported yet. 'pragma' can't be used on types either (otherwise<br>
> > something like "alias half = pragma(attribute, mode("HF")) float;" could be<br>
> > made to work).<br>
> ><br>
> > And - yes - the /proper/ way would be eg<br>
> ><br>
> > alias half = @core[size=2] float;<br>
> > // or '@core.size(2)' or any other syntax.<br>
> ><br>
> > For now, a struct + alias-this-getter might be enough, for a s/w implementation.<br>
> ><br>
> ><br>
> > Yes, that's what I and everyone else does, but that's not the point of the topic...<br>
> > I think it's a fundamental datatype, and it deserves the same casting/type conversion/type safety rules as ints and floats, and it would also be useful to map it to hardware (which GDC can easily do).<br>
><br>
> All of the 'half' definitions above would give you a 'fundamental' datatype --<br>
> -- there is no reason to make it a /language/-defined type. The work required<br>
> to implement things like implicit conversions can be done more generically;<br>
> right now the problem is the lack of a way to define such types.<br>
><br>
> alias fp24 = @core[exp=7, mant=16] float; // It's only a matter of time until<br>
> // someone asks for it&co...<br>
><br>
> Also, *every* user- (or in this case more likely lib-) defined data type deserves<br>
> the same control over casting and conversions as built-in types. No, D doesn't<br>
> have that and yes - it is a problem.<br>
><br>
> I actually think these types /should/ be done as structs, but<br>
> a) it isn't currently possible (also for reasons other than the above mentioned ones)<br>
> and<br>
> b) such aliases would /still/ be useful for mapping to "magic" compiler-supported<br>
> h/w types.<br>
><br>
><br>
> I don't necessarily disagree, but you can take that logic and run with it as far as you like.<br>
> long can easily be implemented as a struct with 2 ints, why bother with a builtin type for that? Any answer or counter argument you can possibly give me applies equally to half.<br>
<br>
</div></div> alias long = @core[size(int.sizeof*2)] int;<br>
<br>
I'm saying that there's no point in defining '__fp16' when '@core[size(2)] float'<br>
will also do the job. Just as "string" is not a built-in type in D.<br>
Now, this can't be done today, so until it /is/ possible, the struct solution<br>
is an option. No, it isn't perfect. But is it unusable? (honest question)<br></blockquote><div><br></div><div>I've already agreed that existing solutions (via structs) are usable, but that's not the point.</div>
<div>They feel artificial. They're just not orthogonal with existing primitive types, this causes template problems.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Would exposing the raw __fp16 type in GDC really be a better long-term solution?<br></blockquote><div><br></div><div>No, exposing a GDC type is not acceptable. I'll only use it if it's defined by the front end, for portability.</div>
<div>Mapping GDC types to a type defined by the front end is another question though, since the front end will define the rules in that case.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
[Of course that 'long' D definition is not a good one, but that's another topic]<br>
<div class="im"><br>
> D needs quite a few tweaks to be able to create a user defined type that is capable of performing like a builtin type. The main limiters are custom implicit conversion, and literal syntax definition,<br>
<br>
</div>I know, and I'm not sure I'd use the words 'a few tweaks". :)<br>
<div class="im"><br>
> but it also seems really nasty to hook a library, suppress it, and alias it to a hardware type in the case of GDC for instance. Who's to say the GDC type behaves in exactly the same way as those defined by the D front end?<br>
<br>
</div>The types MUST be compatible - this is also true for any "built-in" type.<br>
Any operation that isn't portable, compiler- or h/w-wise, must be either<br>
disallowed, emulated or specced as undefined behavior. Which is actually<br>
part of the reason why I'd like it wrapped in a struct - that way the compiler<br>
doesn't have to handle all the special cases and the *user* can choose between<br>
"raw" (hw-supported) versions and more generic ones that may fallback to s/w<br>
on platforms that require that.<br></blockquote><div><br></div><div>How would the user 'choose' a hw-supported version under your vision? What would such code look like?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="im">> Point is, this type should behave in EXACTLY the same way as expected when dealing with builtin float types, it's not just some user type, it's an established, industry-recognised precision of float. In reality it's probably significantly easier to define it wherever the others are defined rather than in a new library all on its own.<br>
> I'd also argue that 'half' is far more valuable to modern computing than 'real'.<br>
><br>
> Anyway, the point is, we all already use libraries for half, offering library solutions misses the point of the topic.<br>
> The question I raised is: SHOULD half be a builtin type?<br>
<br>
</div>Well, no - it shouldn't. For a simple reason - it's not universally available.<br></blockquote><div><br></div><div>Neither is real, or long. Should they be removed? Reverted to libraries? (serious question. though obviously the language isn't capable of producing a seamless library implementation of a primitive type atm)</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
And the people using it won't usually be ok with pure s/w emulation.</blockquote><div><br></div><div>Actually, they probably will. People using it will need to use it regardless of how it's implemented. Hardware support it great when available, but realistically, it's used when processing data for execution on another processor that does support it in hardware (typically a GPU). Use of fp16 is probably not optional; it doubles your bandwidth wherever the loss of precision is acceptable.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">But, yes - it should be accessible on platforms that do support it.</blockquote>
<div><br></div></div></div>