OSDN Git Service

PR 33870
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-reatim.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4 --                                                                          --
5 --                         A D A . R E A L _ T I M E                        --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --             Copyright (C) 1991-1994, Florida State University            --
10 --                     Copyright (C) 1995-2006, AdaCore                     --
11 --                                                                          --
12 -- GNARL is free software; you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
20 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
21 -- Boston, MA 02110-1301, USA.                                              --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNARL was developed by the GNARL team at Florida State University.       --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 package body Ada.Real_Time is
36
37    ---------
38    -- "*" --
39    ---------
40
41    --  Note that Constraint_Error may be propagated
42
43    function "*" (Left : Time_Span; Right : Integer) return Time_Span is
44       pragma Unsuppress (Overflow_Check);
45    begin
46       return Time_Span (Duration (Left) * Right);
47    end "*";
48
49    function "*" (Left : Integer; Right : Time_Span) return Time_Span is
50       pragma Unsuppress (Overflow_Check);
51    begin
52       return Time_Span (Left * Duration (Right));
53    end "*";
54
55    ---------
56    -- "+" --
57    ---------
58
59    --  Note that Constraint_Error may be propagated
60
61    function "+" (Left : Time; Right : Time_Span) return Time is
62       pragma Unsuppress (Overflow_Check);
63    begin
64       return Time (Duration (Left) + Duration (Right));
65    end "+";
66
67    function "+" (Left : Time_Span; Right : Time) return Time is
68       pragma Unsuppress (Overflow_Check);
69    begin
70       return Time (Duration (Left) + Duration (Right));
71    end "+";
72
73    function "+" (Left, Right : Time_Span) return Time_Span is
74       pragma Unsuppress (Overflow_Check);
75    begin
76       return Time_Span (Duration (Left) + Duration (Right));
77    end "+";
78
79    ---------
80    -- "-" --
81    ---------
82
83    --  Note that Constraint_Error may be propagated
84
85    function "-" (Left : Time; Right : Time_Span) return Time is
86       pragma Unsuppress (Overflow_Check);
87    begin
88       return Time (Duration (Left) - Duration (Right));
89    end "-";
90
91    function "-" (Left, Right : Time) return Time_Span is
92       pragma Unsuppress (Overflow_Check);
93    begin
94       return Time_Span (Duration (Left) - Duration (Right));
95    end "-";
96
97    function "-" (Left, Right : Time_Span) return Time_Span is
98       pragma Unsuppress (Overflow_Check);
99    begin
100       return Time_Span (Duration (Left) - Duration (Right));
101    end "-";
102
103    function "-" (Right : Time_Span) return Time_Span is
104       pragma Unsuppress (Overflow_Check);
105    begin
106       return Time_Span_Zero - Right;
107    end "-";
108
109    ---------
110    -- "/" --
111    ---------
112
113    --  Note that Constraint_Error may be propagated
114
115    function "/" (Left, Right : Time_Span) return Integer is
116       pragma Unsuppress (Overflow_Check);
117    begin
118       return Integer (Duration (Left) / Duration (Right));
119    end "/";
120
121    function "/" (Left : Time_Span; Right : Integer) return Time_Span is
122       pragma Unsuppress (Overflow_Check);
123    begin
124       return Time_Span (Duration (Left) / Right);
125    end "/";
126
127    -----------
128    -- Clock --
129    -----------
130
131    function Clock return Time is
132    begin
133       return Time (System.Task_Primitives.Operations.Monotonic_Clock);
134    end Clock;
135
136    ------------------
137    -- Microseconds --
138    ------------------
139
140    function Microseconds (US : Integer) return Time_Span is
141    begin
142       return Time_Span_Unit * US * 1_000;
143    end Microseconds;
144
145    ------------------
146    -- Milliseconds --
147    ------------------
148
149    function Milliseconds (MS : Integer) return Time_Span is
150    begin
151       return Time_Span_Unit * MS * 1_000_000;
152    end Milliseconds;
153
154    -------------
155    -- Minutes --
156    -------------
157
158    function Minutes (M : Integer) return Time_Span is
159    begin
160       return Milliseconds (M) * Integer'(60_000);
161    end Minutes;
162
163    -----------------
164    -- Nanoseconds --
165    -----------------
166
167    function Nanoseconds (NS : Integer) return Time_Span is
168    begin
169       return Time_Span_Unit * NS;
170    end Nanoseconds;
171
172    -------------
173    -- Seconds --
174    -------------
175
176    function Seconds (S : Integer) return Time_Span is
177    begin
178       return Milliseconds (S) * Integer'(1000);
179    end Seconds;
180
181    -----------
182    -- Split --
183    -----------
184
185    procedure Split (T : Time; SC : out Seconds_Count; TS : out Time_Span) is
186       T_Val : Time;
187
188    begin
189       --  Special-case for Time_First, whose absolute value is anomalous,
190       --  courtesy of two's complement.
191
192       if T = Time_First then
193          T_Val := abs (Time_Last);
194       else
195          T_Val := abs (T);
196       end if;
197
198       --  Extract the integer part of T, truncating towards zero
199
200       if T_Val < 0.5 then
201          SC := 0;
202       else
203          SC := Seconds_Count (Time_Span'(T_Val - 0.5));
204       end if;
205
206       if T < 0.0 then
207          SC := -SC;
208       end if;
209
210       --  If original time is negative, need to truncate towards negative
211       --  infinity, to make TS non-negative, as per ARM.
212
213       if Time (SC) > T then
214          SC := SC - 1;
215       end if;
216
217       TS := Time_Span (Duration (T) - Duration (SC));
218    end Split;
219
220    -------------
221    -- Time_Of --
222    -------------
223
224    function Time_Of (SC : Seconds_Count; TS : Time_Span) return Time is
225    begin
226       return Time (SC) + TS;
227    end Time_Of;
228
229    -----------------
230    -- To_Duration --
231    -----------------
232
233    function To_Duration (TS : Time_Span) return Duration is
234    begin
235       return Duration (TS);
236    end To_Duration;
237
238    ------------------
239    -- To_Time_Span --
240    ------------------
241
242    function To_Time_Span (D : Duration) return Time_Span is
243    begin
244       --  Note regarding AI-00432 requiring range checking on this conversion.
245       --  In almost all versions of GNAT (and all to which this version of the
246       --  Ada.Real_Time package apply), the range of Time_Span and Duration are
247       --  the same, so there is no issue of overflow.
248
249       return Time_Span (D);
250    end To_Time_Span;
251
252 end Ada.Real_Time;