1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . A L T I V E C . C O N V E R S I O N S --
9 -- Copyright (C) 2005-2007, Free Software Foundation, Inc. --
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. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.Unchecked_Conversion
;
36 with System
; use System
;
38 package body GNAT
.Altivec
.Conversions
is
40 -- All the vector/view conversions operate similarly: bare unchecked
41 -- conversion on big endian targets, and elements permutation on little
42 -- endian targets. We call "Mirroring" the elements permutation process.
44 -- We would like to provide a generic version of the conversion routines
45 -- and just have a set of "renaming as body" declarations to satisfy the
46 -- public interface. This unfortunately prevents inlining, which we must
47 -- preserve at least for the hard binding.
49 -- We instead provide a generic version of facilities needed by all the
50 -- conversion routines and use them repeatedly.
53 type Vitem_Type
is private;
55 type Varray_Index_Type
is range <>;
56 type Varray_Type
is array (Varray_Index_Type
) of Vitem_Type
;
58 type Vector_Type
is private;
59 type View_Type
is private;
61 package Generic_Conversions
is
63 subtype Varray
is Varray_Type
;
64 -- This provides an easy common way to refer to the type parameter
65 -- in contexts where a specific instance of this package is "use"d.
67 procedure Mirror
(A
: Varray_Type
; Into
: out Varray_Type
);
68 pragma Inline
(Mirror
);
69 -- Mirror the elements of A into INTO, not touching the per-element
72 -- A procedure with an out parameter is a bit heavier to use than a
73 -- function but reduces the amount of temporary creations around the
74 -- call. Instances are typically not front-end inlined. They can still
75 -- be back-end inlined on request with the proper command-line option.
77 -- Below are Unchecked Conversion routines for various purposes,
78 -- relying on internal knowledge about the bits layout in the different
79 -- types (all 128 value bits blocks).
81 -- View<->Vector straight bitwise conversions on BE targets
83 function UNC_To_Vector
is
84 new Ada
.Unchecked_Conversion
(View_Type
, Vector_Type
);
86 function UNC_To_View
is
87 new Ada
.Unchecked_Conversion
(Vector_Type
, View_Type
);
89 -- Varray->Vector/View for returning mirrored results on LE targets
91 function UNC_To_Vector
is
92 new Ada
.Unchecked_Conversion
(Varray_Type
, Vector_Type
);
94 function UNC_To_View
is
95 new Ada
.Unchecked_Conversion
(Varray_Type
, View_Type
);
97 -- Vector/View->Varray for to-be-permuted source on LE targets
99 function UNC_To_Varray
is
100 new Ada
.Unchecked_Conversion
(Vector_Type
, Varray_Type
);
102 function UNC_To_Varray
is
103 new Ada
.Unchecked_Conversion
(View_Type
, Varray_Type
);
105 end Generic_Conversions
;
107 package body Generic_Conversions
is
109 procedure Mirror
(A
: Varray_Type
; Into
: out Varray_Type
) is
111 for J
in A
'Range loop
112 Into
(J
) := A
(A
'Last - J
+ A
'First);
116 end Generic_Conversions
;
118 -- Now we declare the instances and implement the interface function
119 -- bodies simply calling the instantiated routines.
121 ---------------------
122 -- Char components --
123 ---------------------
125 package SC_Conversions
is new Generic_Conversions
126 (signed_char
, Vchar_Range
, Varray_signed_char
, VSC
, VSC_View
);
128 function To_Vector
(S
: VSC_View
) return VSC
is
131 if Default_Bit_Order
= High_Order_First
then
132 return UNC_To_Vector
(S
);
137 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
138 return UNC_To_Vector
(M
);
143 function To_View
(S
: VSC
) return VSC_View
is
146 if Default_Bit_Order
= High_Order_First
then
147 return UNC_To_View
(S
);
152 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
153 return UNC_To_View
(M
);
160 package UC_Conversions
is new Generic_Conversions
161 (unsigned_char
, Vchar_Range
, Varray_unsigned_char
, VUC
, VUC_View
);
163 function To_Vector
(S
: VUC_View
) return VUC
is
166 if Default_Bit_Order
= High_Order_First
then
167 return UNC_To_Vector
(S
);
172 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
173 return UNC_To_Vector
(M
);
178 function To_View
(S
: VUC
) return VUC_View
is
181 if Default_Bit_Order
= High_Order_First
then
182 return UNC_To_View
(S
);
187 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
188 return UNC_To_View
(M
);
195 package BC_Conversions
is new Generic_Conversions
196 (bool_char
, Vchar_Range
, Varray_bool_char
, VBC
, VBC_View
);
198 function To_Vector
(S
: VBC_View
) return VBC
is
201 if Default_Bit_Order
= High_Order_First
then
202 return UNC_To_Vector
(S
);
207 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
208 return UNC_To_Vector
(M
);
213 function To_View
(S
: VBC
) return VBC_View
is
216 if Default_Bit_Order
= High_Order_First
then
217 return UNC_To_View
(S
);
222 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
223 return UNC_To_View
(M
);
228 ----------------------
229 -- Short components --
230 ----------------------
232 package SS_Conversions
is new Generic_Conversions
233 (signed_short
, Vshort_Range
, Varray_signed_short
, VSS
, VSS_View
);
235 function To_Vector
(S
: VSS_View
) return VSS
is
238 if Default_Bit_Order
= High_Order_First
then
239 return UNC_To_Vector
(S
);
244 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
245 return UNC_To_Vector
(M
);
250 function To_View
(S
: VSS
) return VSS_View
is
253 if Default_Bit_Order
= High_Order_First
then
254 return UNC_To_View
(S
);
259 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
260 return UNC_To_View
(M
);
267 package US_Conversions
is new Generic_Conversions
268 (unsigned_short
, Vshort_Range
, Varray_unsigned_short
, VUS
, VUS_View
);
270 function To_Vector
(S
: VUS_View
) return VUS
is
273 if Default_Bit_Order
= High_Order_First
then
274 return UNC_To_Vector
(S
);
279 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
280 return UNC_To_Vector
(M
);
285 function To_View
(S
: VUS
) return VUS_View
is
288 if Default_Bit_Order
= High_Order_First
then
289 return UNC_To_View
(S
);
294 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
295 return UNC_To_View
(M
);
302 package BS_Conversions
is new Generic_Conversions
303 (bool_short
, Vshort_Range
, Varray_bool_short
, VBS
, VBS_View
);
305 function To_Vector
(S
: VBS_View
) return VBS
is
308 if Default_Bit_Order
= High_Order_First
then
309 return UNC_To_Vector
(S
);
314 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
315 return UNC_To_Vector
(M
);
320 function To_View
(S
: VBS
) return VBS_View
is
323 if Default_Bit_Order
= High_Order_First
then
324 return UNC_To_View
(S
);
329 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
330 return UNC_To_View
(M
);
339 package SI_Conversions
is new Generic_Conversions
340 (signed_int
, Vint_Range
, Varray_signed_int
, VSI
, VSI_View
);
342 function To_Vector
(S
: VSI_View
) return VSI
is
345 if Default_Bit_Order
= High_Order_First
then
346 return UNC_To_Vector
(S
);
351 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
352 return UNC_To_Vector
(M
);
357 function To_View
(S
: VSI
) return VSI_View
is
360 if Default_Bit_Order
= High_Order_First
then
361 return UNC_To_View
(S
);
366 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
367 return UNC_To_View
(M
);
374 package UI_Conversions
is new Generic_Conversions
375 (unsigned_int
, Vint_Range
, Varray_unsigned_int
, VUI
, VUI_View
);
377 function To_Vector
(S
: VUI_View
) return VUI
is
380 if Default_Bit_Order
= High_Order_First
then
381 return UNC_To_Vector
(S
);
386 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
387 return UNC_To_Vector
(M
);
392 function To_View
(S
: VUI
) return VUI_View
is
395 if Default_Bit_Order
= High_Order_First
then
396 return UNC_To_View
(S
);
401 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
402 return UNC_To_View
(M
);
409 package BI_Conversions
is new Generic_Conversions
410 (bool_int
, Vint_Range
, Varray_bool_int
, VBI
, VBI_View
);
412 function To_Vector
(S
: VBI_View
) return VBI
is
415 if Default_Bit_Order
= High_Order_First
then
416 return UNC_To_Vector
(S
);
421 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
422 return UNC_To_Vector
(M
);
427 function To_View
(S
: VBI
) return VBI_View
is
430 if Default_Bit_Order
= High_Order_First
then
431 return UNC_To_View
(S
);
436 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
437 return UNC_To_View
(M
);
442 ----------------------
443 -- Float components --
444 ----------------------
446 package F_Conversions
is new Generic_Conversions
447 (C_float
, Vfloat_Range
, Varray_float
, VF
, VF_View
);
449 function To_Vector
(S
: VF_View
) return VF
is
452 if Default_Bit_Order
= High_Order_First
then
453 return UNC_To_Vector
(S
);
458 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
459 return UNC_To_Vector
(M
);
464 function To_View
(S
: VF
) return VF_View
is
467 if Default_Bit_Order
= High_Order_First
then
468 return UNC_To_View
(S
);
473 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
474 return UNC_To_View
(M
);
479 ----------------------
480 -- Pixel components --
481 ----------------------
483 package P_Conversions
is new Generic_Conversions
484 (pixel
, Vpixel_Range
, Varray_pixel
, VP
, VP_View
);
486 function To_Vector
(S
: VP_View
) return VP
is
489 if Default_Bit_Order
= High_Order_First
then
490 return UNC_To_Vector
(S
);
495 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
496 return UNC_To_Vector
(M
);
501 function To_View
(S
: VP
) return VP_View
is
504 if Default_Bit_Order
= High_Order_First
then
505 return UNC_To_View
(S
);
510 Mirror
(UNC_To_Varray
(S
), Into
=> M
);
511 return UNC_To_View
(M
);
516 end GNAT
.Altivec
.Conversions
;