-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Custom hardware numeric formats #7157
Comments
Perhaps this goes hand-in-hand with my idea to use a Computable Number implementation for Thoughts? |
Pretty much everything out there either uses IEEE-754 or fixed point math since the past three or so decades. The introduction of posits, while promising, is mostly just an academic exercise at this point. It would seem to me that it's more pragmatic to hardcode any new formats as they pop up, than to build a framework around something that something that rarely (if ever) changes. I suppose a custom float type with a user-defined number of exponent and mantissa bits could be useful, for future revisions of the IEEE-754 standard? |
The proper way to support custom numeric types is operator overloading. There are very good use cases for that in the here-and-now (vector math, complex numbers, rationals). On the other hand, if you consider operator overloading harmful, then you have to live with method chaining for your posits, just as you do for vectors and rationals. And in the extremely unlikely event that posits or LNS or whatever else goes mainstream, there should be no problem in retrofitting support for it into the language, since Zig has no plans of being feature-frozen after 1.0 (#350, #6466). In the mean time, let's not complicate the language and compiler with speculative features. |
Ewww. |
There are a wide variety of possible numeric formats supported by different hardware, and new ones may come into use in the future (I personally have my eye on posits). Our current strategy is to hardcode every format that we intend to support, which means that if we don't support a format, a new one rises to prominence, or someone just wants to tinker with an FPGA and tries to compile Zig for their work, either we need to make breaking changes to the compiler or someone has to fork it and fragment efforts. There are two solutions: either we try to anticipate every hardware format that numbers can possibly have (impossible, inelegant), or we provide a way of implementing formats in userspace. I propose the latter.
To be clear: I am NOT proposing operator overloading. In fact this proposal takes great care to make that impossible. The key insight is that we don't need functions -- we merely need to expose the capabilities of the existing hardware. This means that when defining a format, we need only provide three things:
comptime_int
orcomptime_float
or both to a bit patternFor convenience in representation, it might also be helpful to define
b{n}
, an n-width bit vector. Such a type would support slicing by comptime-known bit indices with assignment, concatenation, repetition, equality checking, coercion from signed or unsigned integers (but not vice versa), and bitwise logical operations, but no arithmetic. Also, optionally, a more convenient coercion syntax:(T)val
, in which the parentheses are required. Also optionally, typesanyint
andanyfloat
to describe generic values that may only be specialised to integers or reals, and for which@typeOf
returns the specialisation type if the value is defined. None of this is necessary for this proposal -- if they would create more problems than they solve, then to hell with them.Here is one possible syntax, probably not the best:
By requiring a register map and a bit width, and more or less forcing inline assembly for operations, we ensure that the defined type will be supported in hardware. No operator overloading here.
For smooth interop with SIMD,
numtype
s may not be subdivided -- they represent scalars only. Strictly speaking, there is nothing to stop the author wrapping a matrix or other tensor type this way, however this will not interoperate with Zig SIMD. Defining.reg
as a tagged union as mentioned above may ease integration -- further thought is needed on this subject.With this proposal, we future-proof the language against new numeric formats, without breaking it wide open. Zig remains tight and transparent, while becoming more flexible and adaptable.
The text was updated successfully, but these errors were encountered: