3 ms·
Case in point: try to do the fast inverse square root hack with a (truly) strongly typed language.
by TheLoneWolfling 12y ago
Case in point: try to do the fast inverse square root hack with a (truly) strongly typed language.
- phaedrus 12y agoIf the language supports union, then simply give your working variable the type of "union { float, long }". This directly expresses the idea that you're working with a piece of memory that is used for two purposes.
- TheLoneWolfling 12y agoIf the language supports untagged unions, it isn't strongly typed. With very few exceptions.
- tomp 12y agoUntagged unions between two non-pointer types with the same representation (same number of bits) are perfectly safe, they are just ordinary bit-casts.
- gsg 12y agoWhat's the difficulty, exactly? You need some mundane float->bits and bits->float operations, which are commonly although not universally provided in strongly typed environments. The rest is integer ops.
- TheLoneWolfling 12y agoSure, if you can convert float -> bits -> float you can make code that does the same thing, but efficient code? Good luck.
- jwise0 12y agoThat's not a terribly difficult proposition in a strongly-typed language -- in fact, it is even more performant than if you were to do it with tagged unions! The function "floatFromBits : float -> bits" has no execution overhead, and simply adds a type annotation to the semantic meaning of the identity. At execution time, there are no instructions emitted for it... So, thanks for the well wishes, but in this case I don't think they're needed: any reasonable compiler should be able to do this without great difficulty.
- gsg 12y agoIt could just be a movd if the compiler backend people put the work in (and if they can't be arsed and implement it as a function call, that's nothing to do with the type system).