OSDN Git Service

2007-04-20 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-altive.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                         G N A T . A L T I V E C                          --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2006, 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 -------------------------
35 -- General description --
36 -------------------------
37
38 --  This is the root of a package hierarchy offering an Ada binding to the
39 --  PowerPC AltiVec extensions. These extensions basically consist in a set of
40 --  128bit vector types together with a set of subprograms operating on such
41 --  vectors. On a real Altivec capable target, vector objects map to hardware
42 --  vector registers and the subprograms map to a set of specific hardware
43 --  instructions.
44
45 --  Relevant documents are:
46
47 --  o AltiVec Technology, Programming Interface Manual (1999-06)
48 --    to which we will refer as [PIM], describes the data types, the
49 --    functional interface and the ABI conventions.
50
51 --  o AltiVec Technology, Programming Environments Manual (2002-02)
52 --    to which we will refer as [PEM], describes the hardware architecture
53 --    and instruction set.
54
55 --  These documents, as well as a number of others of general interest on the
56 --  AltiVec technology, are available from the Motorola/AltiVec Web site at
57
58 --  http://www.motorola.com/altivec
59
60 --  We offer two versions of this binding: one for real AltiVec capable
61 --  targets, and one for other targets. In the latter case, everything is
62 --  emulated in software. We will refer to the two bindings as:
63
64 --  o The Hard binding for AltiVec capable targets (with the appropriate
65 --    hardware support and corresponding instruction set)
66
67 --  o The Soft binding for other targets (with the low level primitives
68 --    emulated in software).
69
70 --  The two versions of the binding are expected to be equivalent from the
71 --  functional standpoint. The same client application code should observe no
72 --  difference in operation results, even if the Soft version is used on a
73 --  non-powerpc target. The Hard binding is naturally expected to run faster
74 --  than the Soft version on the same target.
75
76 --  We also offer interfaces not strictly part of the base AltiVec API, such
77 --  as vector conversions to/from array representations, which are of interest
78 --  for client applications (e.g. for vector initialization purposes) and may
79 --  also be used as implementation facilities.
80
81 -----------------------------------------
82 -- General package architecture survey --
83 -----------------------------------------
84
85 --  The various vector representations are all "containers" of elementary
86 --  values, the possible types of which are declared in this root package to
87 --  be generally accessible.
88
89 --  From the user standpoint, the two versions of the binding are available
90 --  through a consistent hierarchy of units providing identical services:
91
92 --                             GNAT.Altivec
93 --                           (component types)
94 --                                   |
95 --          o----------------o----------------o-------------o
96 --          |                |                |             |
97 --    Vector_Types   Vector_Operations   Vector_Views   Conversions
98
99 --  The user can manipulate vectors through two families of types: Vector
100 --  types and View types.
101
102 --  Vector types are defined in the GNAT.Altivec.Vector_Types package
103
104 --  On these types, the user can apply the Altivec operations defined in
105 --  GNAT.Altivec.Vector_Operations. Their layout is opaque and may vary across
106 --  configurations, for it is typically target-endianness dependant.
107
108 --  Vector_Types and Vector_Operations implement the core binding to the
109 --  AltiVec API, as described in [PIM-2.1 data types] and [PIM-4 AltiVec
110 --  operations and predicates].
111
112 --  View types are defined in the GNAT.Altivec.Vector_Views package
113
114 --  These types do not represent Altivec vectors per se, in the sense that the
115 --  Altivec_Operations are not available for them. They are intended to allow
116 --  Vector initializations as well as access to the Vector component values.
117
118 --  The GNAT.Altivec.Conversions package is provided to convert a View to the
119 --  corresponding Vector and vice-versa.
120
121 --  The two versions of the binding rely on a low level internal interface,
122 --  and switching from one version to the other amounts to select one low
123 --  level implementation instead of the other.
124
125 --  The bindings are provided as a set of sources together with a project file
126 --  (altivec.gpr). The hard/soft binding selection is controlled by a project
127 --  variable on targets where switching makes sense. See the example usage
128 --  section below.
129
130 ---------------------------
131 -- Underlying principles --
132 ---------------------------
133
134 --  The general organization sketched above has been devised from a number
135 --  of driving ideas:
136
137 --  o From the clients standpoint, the two versions of the binding should be
138 --    as easily exchangable as possible,
139
140 --  o From the maintenance standpoint, we want to avoid as much code
141 --    duplication as possible.
142
143 --  o From both standpoints above, we want to maintain a clear interface
144 --    separation between the base bindings to the Motorola API and the
145 --    additional facilities.
146
147 --  The identification of the low level interface is directly inspired by the
148 --  the base API organization, basically consisting of a rich set of functions
149 --  around a core of low level primitives mapping to AltiVec instructions.
150
151 --  See for instance "vec_add" in [PIM-4.4 Generic and Specific AltiVec
152 --  operations]: no less than six result/arguments combinations of byte vector
153 --  types map to "vaddubm".
154
155 --  The "hard" version of the low level primitives map to real AltiVec
156 --  instructions via the corresponding GCC builtins. The "soft" version is
157 --  a software emulation of those.
158
159 -------------------
160 -- Example usage --
161 -------------------
162
163 --  Here is a sample program declaring and initializing two vectors, 'add'ing
164 --  them and displaying the result components:
165
166 --  with GNAT.Altivec.Vector_Types;      use GNAT.Altivec.Vector_Types;
167 --  with GNAT.Altivec.Vector_Operations; use GNAT.Altivec.Vector_Operations;
168 --  with GNAT.Altivec.Vector_Views;      use GNAT.Altivec.Vector_Views;
169 --  with GNAT.Altivec.Conversions;       use GNAT.Altivec.Conversions;
170
171 --  use GNAT.Altivec;
172
173 --  procedure Sample is
174 --     Va : Vector_Unsigned_Int := To_Vector ((Values => (1, 2, 3, 4)));
175 --     Vb : Vector_Unsigned_Int := To_Vector ((Values => (1, 2, 3, 4)));
176
177 --     Vs : Vector_Unsigned_Int;
178 --     Vs_View : VUI_View;
179 --  begin
180 --     Vs := Vec_Add (Va, Vb);
181 --     Vs_View := To_View (Vs);
182
183 --     for I in Vs_View.Values'Range loop
184 --        Put_Line (Unsigned_Int'Image (Vs_View.Values (I)));
185 --     end loop;
186 --  end;
187
188 --  This currently requires the GNAT project management facilities to compile,
189 --  to automatically retrieve the set of necessary sources and switches
190 --  depending on your configuration. For the example above, customizing the
191 --  switches to include -g also, this would be something like:
192
193 --  sample.gpr
194 --
195 --  with "altivec.gpr";
196 --
197 --  project Sample is
198
199 --    for Source_Dirs use (".");
200 --    for Main use ("sample");
201
202 --    package Compiler is
203 --       for Default_Switches ("Ada") use
204 --           Altivec.Compiler'Default_Switches ("Ada") & "-g";
205 --    end Compiler;
206
207 --  end Sample;
208
209 --  $ gnatmake -Psample
210 --  [...]
211 --  $ ./sample
212 --  2
213 --  4
214 --  6
215 --  8
216
217 ------------------------------------------------------------------------------
218
219 with System;
220
221 package GNAT.Altivec is
222
223    --  Definitions of constants and vector/array component types common to all
224    --  the versions of the binding.
225
226    --  All the vector types are 128bits
227
228    VECTOR_BIT : constant := 128;
229
230    -------------------------------------------
231    -- [PIM-2.3.1 Alignment of vector types] --
232    -------------------------------------------
233
234    --  "A defined data item of any vector data type in memory is always
235    --  aligned on a 16-byte boundary. A pointer to any vector data type always
236    --  points to a 16-byte boundary. The compiler is responsible for aligning
237    --  vector data types on 16-byte boundaries."
238
239    VECTOR_ALIGNMENT : constant := Natural'Min (16, Standard'Maximum_Alignment);
240    --  This value is used to set the alignment of vector datatypes in both the
241    --  hard and the soft binding implementations.
242    --
243    --  We want this value to never be greater than 16, because none of the
244    --  binding implementations requires larger alignments and such a value
245    --  would cause useless space to be allocated/wasted for vector objects.
246    --  Furthermore, the alignment of 16 matches the hard binding leading to
247    --  a more faithful emulation.
248    --
249    --  It needs to be exactly 16 for the hard binding, and the initializing
250    --  expression is just right for this purpose since Maximum_Alignment is
251    --  expected to be 16 for the real Altivec ABI.
252    --
253    --  The soft binding doesn't rely on strict 16byte alignment, and we want
254    --  the value to be no greater than Standard'Maximum_Alignment in this case
255    --  to ensure it is supported on every possible target.
256
257    -------------------------------------------------------
258    -- [PIM-2.1] Data Types - Interpretation of contents --
259    -------------------------------------------------------
260
261    ---------------------
262    -- char components --
263    ---------------------
264
265    CHAR_BIT    : constant := 8;
266    SCHAR_MIN   : constant := -2 ** (CHAR_BIT - 1);
267    SCHAR_MAX   : constant := 2 ** (CHAR_BIT - 1) - 1;
268    UCHAR_MAX   : constant := 2 ** CHAR_BIT - 1;
269
270    type unsigned_char is mod UCHAR_MAX + 1;
271    for unsigned_char'Size use CHAR_BIT;
272
273    type signed_char is range SCHAR_MIN .. SCHAR_MAX;
274    for signed_char'Size use CHAR_BIT;
275
276    subtype bool_char is unsigned_char;
277    --  ??? There is a difference here between what the Altivec Technology
278    --  Programming Interface Manual says and what GCC says. In the manual,
279    --  vector_bool_char is a vector_unsigned_char, while in altivec.h it
280    --  is a vector_signed_char.
281
282    bool_char_True  : constant bool_char := bool_char'Last;
283    bool_char_False : constant bool_char := 0;
284
285    ----------------------
286    -- short components --
287    ----------------------
288
289    SHORT_BIT   : constant := 16;
290    SSHORT_MIN  : constant := -2 ** (SHORT_BIT - 1);
291    SSHORT_MAX  : constant := 2 ** (SHORT_BIT - 1) - 1;
292    USHORT_MAX  : constant := 2 ** SHORT_BIT - 1;
293
294    type unsigned_short is mod USHORT_MAX + 1;
295    for unsigned_short'Size use SHORT_BIT;
296
297    subtype unsigned_short_int is unsigned_short;
298
299    type signed_short is range SSHORT_MIN .. SSHORT_MAX;
300    for signed_short'Size use SHORT_BIT;
301
302    subtype signed_short_int is signed_short;
303
304    subtype bool_short is unsigned_short;
305    --  ??? See bool_char
306
307    bool_short_True  : constant bool_short := bool_short'Last;
308    bool_short_False : constant bool_short := 0;
309
310    subtype bool_short_int is bool_short;
311
312    --------------------
313    -- int components --
314    --------------------
315
316    INT_BIT     : constant := 32;
317    SINT_MIN    : constant := -2 ** (INT_BIT - 1);
318    SINT_MAX    : constant := 2 ** (INT_BIT - 1) - 1;
319    UINT_MAX    : constant := 2 ** INT_BIT - 1;
320
321    type unsigned_int is mod UINT_MAX + 1;
322    for unsigned_int'Size use INT_BIT;
323
324    type signed_int is range SINT_MIN .. SINT_MAX;
325    for signed_int'Size use INT_BIT;
326
327    subtype bool_int is unsigned_int;
328    --  ??? See bool_char
329
330    bool_int_True  : constant bool_int := bool_int'Last;
331    bool_int_False : constant bool_int := 0;
332
333    ----------------------
334    -- float components --
335    ----------------------
336
337    FLOAT_BIT   : constant := 32;
338    FLOAT_DIGIT : constant := 6;
339    FLOAT_MIN   : constant := -16#0.FFFF_FF#E+32;
340    FLOAT_MAX   : constant := 16#0.FFFF_FF#E+32;
341
342    type C_float is digits FLOAT_DIGIT range FLOAT_MIN .. FLOAT_MAX;
343    for C_float'Size use FLOAT_BIT;
344
345    ----------------------
346    -- pixel components --
347    ----------------------
348
349    subtype pixel is unsigned_short;
350
351    -----------------------------------------------------------
352    -- Subtypes for variants found in the GCC implementation --
353    -----------------------------------------------------------
354
355    subtype c_int is signed_int;
356    subtype c_short is c_int;
357
358    LONG_BIT  : constant := 32;
359    --  Some of the GCC builtins are built with "long" arguments and
360    --  expect SImode to come in.
361
362    SLONG_MIN : constant := -2 ** (LONG_BIT - 1);
363    SLONG_MAX : constant :=  2 ** (LONG_BIT - 1) - 1;
364    ULONG_MAX : constant :=  2 ** LONG_BIT - 1;
365
366    type signed_long   is range SLONG_MIN .. SLONG_MAX;
367    type unsigned_long is mod ULONG_MAX + 1;
368
369    subtype c_long is signed_long;
370
371    subtype c_ptr is System.Address;
372
373    ---------------------------------------------------------
374    -- Access types, for the sake of some argument passing --
375    ---------------------------------------------------------
376
377    type signed_char_ptr    is access all signed_char;
378    type unsigned_char_ptr  is access all unsigned_char;
379
380    type short_ptr          is access all c_short;
381    type signed_short_ptr   is access all signed_short;
382    type unsigned_short_ptr is access all unsigned_short;
383
384    type int_ptr            is access all c_int;
385    type signed_int_ptr     is access all signed_int;
386    type unsigned_int_ptr   is access all unsigned_int;
387
388    type long_ptr           is access all c_long;
389    type signed_long_ptr    is access all signed_long;
390    type unsigned_long_ptr  is access all unsigned_long;
391
392    type float_ptr          is access all Float;
393
394    --
395
396    type const_signed_char_ptr    is access constant signed_char;
397    type const_unsigned_char_ptr  is access constant unsigned_char;
398
399    type const_short_ptr          is access constant c_short;
400    type const_signed_short_ptr   is access constant signed_short;
401    type const_unsigned_short_ptr is access constant unsigned_short;
402
403    type const_int_ptr            is access constant c_int;
404    type const_signed_int_ptr     is access constant signed_int;
405    type const_unsigned_int_ptr   is access constant unsigned_int;
406
407    type const_long_ptr           is access constant c_long;
408    type const_signed_long_ptr    is access constant signed_long;
409    type const_unsigned_long_ptr  is access constant unsigned_long;
410
411    type const_float_ptr          is access constant Float;
412
413    --  Access to const volatile arguments need specialized types
414
415    type volatile_float is new Float;
416    pragma Volatile (volatile_float);
417
418    type volatile_signed_char is new signed_char;
419    pragma Volatile (volatile_signed_char);
420
421    type volatile_unsigned_char is new unsigned_char;
422    pragma Volatile (volatile_unsigned_char);
423
424    type volatile_signed_short is new signed_short;
425    pragma Volatile (volatile_signed_short);
426
427    type volatile_unsigned_short is new unsigned_short;
428    pragma Volatile (volatile_unsigned_short);
429
430    type volatile_signed_int is new signed_int;
431    pragma Volatile (volatile_signed_int);
432
433    type volatile_unsigned_int is new unsigned_int;
434    pragma Volatile (volatile_unsigned_int);
435
436    type volatile_signed_long is new signed_long;
437    pragma Volatile (volatile_signed_long);
438
439    type volatile_unsigned_long is new unsigned_long;
440    pragma Volatile (volatile_unsigned_long);
441
442    type constv_char_ptr           is access constant volatile_signed_char;
443    type constv_signed_char_ptr    is access constant volatile_signed_char;
444    type constv_unsigned_char_ptr  is access constant volatile_unsigned_char;
445
446    type constv_short_ptr          is access constant volatile_signed_short;
447    type constv_signed_short_ptr   is access constant volatile_signed_short;
448    type constv_unsigned_short_ptr is access constant volatile_unsigned_short;
449
450    type constv_int_ptr            is access constant volatile_signed_int;
451    type constv_signed_int_ptr     is access constant volatile_signed_int;
452    type constv_unsigned_int_ptr   is access constant volatile_unsigned_int;
453
454    type constv_long_ptr           is access constant volatile_signed_long;
455    type constv_signed_long_ptr    is access constant volatile_signed_long;
456    type constv_unsigned_long_ptr  is access constant volatile_unsigned_long;
457
458    type constv_float_ptr  is access constant volatile_float;
459
460 private
461
462    -----------------------
463    -- Various constants --
464    -----------------------
465
466    CR6_EQ     : constant := 0;
467    CR6_EQ_REV : constant := 1;
468    CR6_LT     : constant := 2;
469    CR6_LT_REV : constant := 3;
470
471 end GNAT.Altivec;