OSDN Git Service

2003-10-21 Arnaud Charlet <charlet@act-europe.fr>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-caun16.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                   GNU ADA RUNTIME LIBRARY COMPONENTS                     --
4 --                                                                          --
5 --      S Y S T E M . C O M P A R E _ A R R A Y _ U N S I G N E D _ 16      --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --             Copyright (C) 2002 Free Software Foundation, Inc.            --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Unchecked_Conversion;
35
36 package body System.Compare_Array_Unsigned_16 is
37
38    type Word is mod 2 ** 32;
39    --  Used to process operands by words
40
41    type Half is mod 2 ** 16;
42    for Half'Size use 16;
43    --  Used to process operands by half words
44
45    type Uhalf is record
46       H : Half;
47    end record;
48    pragma Pack (Uhalf);
49    for Uhalf'Alignment use 1;
50    --  Used to process operands when unaligned
51
52    type WP is access Word;
53    type HP is access Half;
54    type UP is access Uhalf;
55
56    function W is new Unchecked_Conversion (Address, WP);
57    function H is new Unchecked_Conversion (Address, HP);
58    function U is new Unchecked_Conversion (Address, UP);
59
60    -----------------------
61    -- Compare_Array_U16 --
62    -----------------------
63
64    function Compare_Array_U16
65      (Left      : System.Address;
66       Right     : System.Address;
67       Left_Len  : Natural;
68       Right_Len : Natural)
69       return      Integer
70    is
71       Clen : Natural := Natural'Min (Left_Len, Right_Len);
72       --  Number of elements left to compare
73
74       L : Address := Left;
75       R : Address := Right;
76       --  Pointers to next elements to compare
77
78    begin
79       --  Go by words if possible
80
81       if ((Left or Right) and (4 - 1)) = 0 then
82          while Clen > 1
83            and then W (L).all = W (R).all
84          loop
85             Clen := Clen - 2;
86             L := L + 4;
87             R := R + 4;
88          end loop;
89       end if;
90
91       --  Case of going by aligned half words
92
93       if ((Left or Right) and (2 - 1)) = 0 then
94          while Clen /= 0 loop
95             if H (L).all /= H (R).all then
96                if H (L).all > H (R).all then
97                   return +1;
98                else
99                   return -1;
100                end if;
101             end if;
102
103             Clen := Clen - 1;
104             L := L + 2;
105             R := R + 2;
106          end loop;
107
108       --  Case of going by unaligned half words
109
110       else
111          while Clen /= 0 loop
112             if U (L).H /= U (R).H then
113                if U (L).H > U (R).H then
114                   return +1;
115                else
116                   return -1;
117                end if;
118             end if;
119
120             Clen := Clen - 1;
121             L := L + 2;
122             R := R + 2;
123          end loop;
124       end if;
125
126       --  Here if common section equal, result decided by lengths
127
128       if Left_Len = Right_Len then
129          return 0;
130       elsif Left_Len > Right_Len then
131          return +1;
132       else
133          return -1;
134       end if;
135    end Compare_Array_U16;
136
137 end System.Compare_Array_Unsigned_16;