-- --
------------------------------------------------------------------------------
--- Fixed point I/O
--- ---------------
+-- -------------------
+-- - Fixed point I/O -
+-- -------------------
-- The following text documents implementation details of the fixed point
-- input/output routines in the GNAT runtime. The first part describes the
-- Subsequently these are reduced to implementation constraints and the impact
-- of these constraints on a few possible approaches to input/output is given.
-- Based on this analysis, a specific implementation is selected for use in
--- the GNAT runtime. Finally, the chosen algorithm is analyzed numerically in
+-- the GNAT runtime. Finally the chosen algorithms are analyzed numerically in
-- order to provide user-level documentation on limits for range and precision
-- of fixed point types as well as accuracy of input/output conversions.
-- Operations
-- ----------
--- 'Image and 'Wide_Image (see RM 3.5(34))
+-- [Wide_[Wide_]]Image attribute (see RM 3.5(27.1/2))
-- These attributes return a decimal real literal best approximating
-- the value (rounded away from zero if halfway between) with a
-- attributes, although it would be nice to be able to output more
-- than S'Aft digits after the decimal point for values of subtype S.
--- 'Value and 'Wide_Value attribute (RM 3.5(40-55))
+-- [Wide_[Wide_]]Value attribute (RM 3.5(39.1/2))
-- Since the input can be given in any base in the range 2..16,
-- accurate conversion to a fixed point number may require
-- be less than 2.0**(-53).
-- In GNAT, Fine_Delta is 2.0**(-63), and Duration for example is a 64-bit
--- type. This means that a floating-point type with 63 bits of mantissa needs
+-- type. This means that a floating-point type with 64 bits of mantissa needs
-- to be used, which is only generally available on the x86 architecture. It
-- would still be possible to use multi-precision floating point to perform
-- calculations using longer mantissas, but this is a much harder approach.
-- Fixed-precision integer arithmetic has the advantage of simplicity and
-- speed. For the most common fixed point types this would be a perfect
--- solution. The downside however may be a too limited set of acceptable
+-- solution. The downside however may be a restricted set of acceptable
-- fixed point types.
+-- Implementation Choices
+-- ----------------------
+
+-- The current implementation in the GNAT runtime uses fixed-precision integer
+-- arithmetic for fixed point types whose Small is the ratio of two integers
+-- whose magnitude is bounded relatively to the size of the mantissa, with a
+-- two-tiered approach for 32-bit and 64-bit fixed point types. For the other
+-- fixed point types, the implementation uses floating-point arithmetic.
+
+-- The exact requirements of the algorithms are analyzed and documented along
+-- with the implementation in their respective units.
+
with Interfaces;
with Ada.Text_IO.Fixed_Aux;
with Ada.Text_IO.Float_Aux;
-- static (although it is not a static expressions in the RM sense).
OK_Get_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- --
------------------------------------------------------------------------------
--- Fixed point I/O
--- ---------------
+-- -------------------
+-- - Fixed point I/O -
+-- -------------------
-- The following text documents implementation details of the fixed point
-- input/output routines in the GNAT runtime. The first part describes the
-- Subsequently these are reduced to implementation constraints and the impact
-- of these constraints on a few possible approaches to input/output is given.
-- Based on this analysis, a specific implementation is selected for use in
--- the GNAT runtime. Finally, the chosen algorithm is analyzed numerically in
+-- the GNAT runtime. Finally the chosen algorithms are analyzed numerically in
-- order to provide user-level documentation on limits for range and precision
-- of fixed point types as well as accuracy of input/output conversions.
-- Operations
-- ----------
--- 'Image and 'Wide_Image (see RM 3.5(34))
+-- [Wide_[Wide_]]Image attribute (see RM 3.5(27.1/2))
-- These attributes return a decimal real literal best approximating
-- the value (rounded away from zero if halfway between) with a
-- attributes, although it would be nice to be able to output more
-- than S'Aft digits after the decimal point for values of subtype S.
--- 'Value and 'Wide_Value attribute (RM 3.5(40-55))
+-- [Wide_[Wide_]]Value attribute (RM 3.5(39.1/2))
-- Since the input can be given in any base in the range 2..16,
-- accurate conversion to a fixed point number may require
-- available has 53 bits of mantissa. This means that Fine_Delta cannot
-- be less than 2.0**(-53).
--- In GNAT, Fine_Delta is 2.0**(-63), and Duration for example is a 64-bit
--- type. This means that a floating-point type with 63 bits of mantissa needs
--- to be used, which is only generally available on the x86 architecture. It
+-- In GNAT, Fine_Delta is 2.0**(-127), and Duration for example is a 64-bit
+-- type. This means that a floating-point type with 128 bits of mantissa needs
+-- to be used, which currently does not exist in any common architecture. It
-- would still be possible to use multi-precision floating point to perform
-- calculations using longer mantissas, but this is a much harder approach.
-- Fixed-precision integer arithmetic has the advantage of simplicity and
-- speed. For the most common fixed point types this would be a perfect
--- solution. The downside however may be a too limited set of acceptable
+-- solution. The downside however may be a restricted set of acceptable
-- fixed point types.
+-- Implementation Choices
+-- ----------------------
+
+-- The current implementation in the GNAT runtime uses fixed-precision integer
+-- arithmetic for fixed point types whose Small is the ratio of two integers
+-- whose magnitude is bounded relatively to the size of the mantissa, with a
+-- three-tiered approach for 32-bit, 64-bit and 128-bit fixed point types. For
+-- other fixed point types, the implementation uses floating-point arithmetic.
+
+-- The exact requirements of the algorithms are analyzed and documented along
+-- with the implementation in their respective units.
+
with Interfaces;
with Ada.Text_IO.Fixed_Aux;
with Ada.Text_IO.Float_Aux;
-- in the RM sense).
OK_Get_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_128 : constant Boolean :=
- Num'Object_Size <= 128
+ Num'Base'Object_Size <= 128
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**127)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_128 : constant Boolean :=
- Num'Object_Size <= 128
+ Num'Base'Object_Size <= 128
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**127)
or else
-- static (although it is not a static expressions in the RM sense).
OK_Get_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- in the RM sense).
OK_Get_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_128 : constant Boolean :=
- Num'Object_Size <= 128
+ Num'Base'Object_Size <= 128
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**127)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_128 : constant Boolean :=
- Num'Object_Size <= 128
+ Num'Base'Object_Size <= 128
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**127)
or else
-- static (although it is not a static expressions in the RM sense).
OK_Get_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- in the RM sense).
OK_Get_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_32 : constant Boolean :=
- Num'Object_Size <= 32
+ Num'Base'Object_Size <= 32
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**31)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_64 : constant Boolean :=
- Num'Object_Size <= 64
+ Num'Base'Object_Size <= 64
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**63)
or else
-- These conditions are derived from the prerequisites of System.Image_F
OK_Get_128 : constant Boolean :=
- Num'Object_Size <= 128
+ Num'Base'Object_Size <= 128
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**127)
or else
-- These conditions are derived from the prerequisites of System.Value_F
OK_Put_128 : constant Boolean :=
- Num'Object_Size <= 128
+ Num'Base'Object_Size <= 128
and then
((Num'Small_Numerator = 1 and then Num'Small_Denominator <= 2**127)
or else