3 ms·
Fun fact GNAT (GCC Ada front-end) will use a biased representation for range type when packing tight: with Ada.Text_IO; use Ada.Text_IO; procedure T is
by guerby 6y ago
Fun fact GNAT (GCC Ada front-end) will use a biased representation for range type when packing tight:
with Ada.Text_IO; use Ada.Text_IO;
procedure T is
type T1 is range 16..19;
type T2 is range -7..0;
type R is record
A : T1;
B,C : T2;
end record;
for R use
record
A at 0 range 0 .. 1;
B at 0 range 2 .. 4;
C at 0 range 5 .. 7;
end record;
X : R := (17,-2,-3);
begin
Put_Line(X'Size'Image); -- 8 bits
end T;
- henrikeh 6y agoFor those not familiar with Ada and bit representations, the compiler is exploiting the fact that types in Ada can have constraints that limit their size. T1 is a type of integer ranging from 16 to 19 (inclusive). Thus, it can only be four distinct values (16, 17, 18, 19). Two bits is enough to represent these four distinct values and thus the binary representation of the type (in record R) can be reduced to 2 bits.
- guerby 6y agoI heard that the only reason Ada had an "Integer" type as opposed to just have range types with user provided bounds was because of String index needing it - String is just a standard array of character in Ada.
- renox 6y agoAda is older than Unicode..
- noneeeed 6y agoThe type system in Ada was always so nice to use, and it was incredibly useful when using SPARK to really constrain the bounds of your types and make the runtime exception freedom proofs easier. I miss working in Ada, but I don't really miss working on those kinds of projects.