aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.4.0/gcc/ada/s-gecobl.adb
blob: d20b53f31b4792c0599861925a4b667fd1e945e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT RUN-TIME COMPONENTS                         --
--                                                                          --
--           S Y S T E M . G E N E R I C _ C O M P L E X _ B L A S          --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--         Copyright (C) 2006-2009, Free Software Foundation, Inc.          --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Unchecked_Conversion;        use Ada;
with Interfaces;                      use Interfaces;
with Interfaces.Fortran;              use Interfaces.Fortran;
with Interfaces.Fortran.BLAS;         use Interfaces.Fortran.BLAS;
with System.Generic_Array_Operations; use System.Generic_Array_Operations;

package body System.Generic_Complex_BLAS is

   Is_Single : constant Boolean :=
                 Real'Machine_Mantissa = Fortran.Real'Machine_Mantissa
                  and then Fortran.Real (Real'First) = Fortran.Real'First
                  and then Fortran.Real (Real'Last) = Fortran.Real'Last;

   Is_Double : constant Boolean :=
                 Real'Machine_Mantissa = Double_Precision'Machine_Mantissa
                  and then
                    Double_Precision (Real'First) = Double_Precision'First
                  and then
                    Double_Precision (Real'Last) = Double_Precision'Last;

   subtype Complex is Complex_Types.Complex;

   --  Local subprograms

   function To_Double_Precision (X : Real) return Double_Precision;
   pragma Inline (To_Double_Precision);

   function To_Double_Complex (X : Complex) return Double_Complex;
   pragma Inline (To_Double_Complex);

   function To_Complex (X : Double_Complex) return Complex;
   function To_Complex (X : Fortran.Complex) return Complex;
   pragma Inline (To_Complex);

   function To_Fortran (X : Complex) return Fortran.Complex;
   pragma Inline (To_Fortran);

   --  Instantiations

   function To_Double_Complex is new
      Vector_Elementwise_Operation
       (X_Scalar      => Complex_Types.Complex,
        Result_Scalar => Fortran.Double_Complex,
        X_Vector      => Complex_Vector,
        Result_Vector => BLAS.Double_Complex_Vector,
        Operation     => To_Double_Complex);

   function To_Complex is new
      Vector_Elementwise_Operation
       (X_Scalar      => Fortran.Double_Complex,
        Result_Scalar => Complex,
        X_Vector      => BLAS.Double_Complex_Vector,
        Result_Vector => Complex_Vector,
        Operation     => To_Complex);

   function To_Double_Complex is new
      Matrix_Elementwise_Operation
       (X_Scalar      => Complex,
        Result_Scalar => Double_Complex,
        X_Matrix      => Complex_Matrix,
        Result_Matrix => BLAS.Double_Complex_Matrix,
        Operation     => To_Double_Complex);

   function To_Complex is new
     Matrix_Elementwise_Operation
       (X_Scalar      => Double_Complex,
        Result_Scalar => Complex,
        X_Matrix      => BLAS.Double_Complex_Matrix,
        Result_Matrix => Complex_Matrix,
        Operation     => To_Complex);

   function To_Double_Precision (X : Real) return Double_Precision is
   begin
      return Double_Precision (X);
   end To_Double_Precision;

   function To_Double_Complex (X : Complex) return Double_Complex is
   begin
      return (To_Double_Precision (X.Re), To_Double_Precision (X.Im));
   end To_Double_Complex;

   function To_Complex (X : Double_Complex) return Complex is
   begin
      return (Real (X.Re), Real (X.Im));
   end To_Complex;

   function To_Complex (X : Fortran.Complex) return Complex is
   begin
      return (Real (X.Re), Real (X.Im));
   end To_Complex;

   function To_Fortran (X : Complex) return Fortran.Complex is
   begin
      return (Fortran.Real (X.Re), Fortran.Real (X.Im));
   end To_Fortran;

   ---------
   -- dot --
   ---------

   function dot
     (N     : Positive;
      X     : Complex_Vector;
      Inc_X : Integer := 1;
      Y     : Complex_Vector;
      Inc_Y : Integer := 1) return Complex
   is
   begin
      if Is_Single then
         declare
            type X_Ptr is access all BLAS.Complex_Vector (X'Range);
            type Y_Ptr is access all BLAS.Complex_Vector (Y'Range);
            function Conv_X is new Unchecked_Conversion (Address, X_Ptr);
            function Conv_Y is new Unchecked_Conversion (Address, Y_Ptr);
         begin
            return To_Complex (BLAS.cdotu (N, Conv_X (X'Address).all, Inc_X,
                                  Conv_Y (Y'Address).all, Inc_Y));
         end;

      elsif Is_Double then
         declare
            type X_Ptr is access all BLAS.Double_Complex_Vector (X'Range);
            type Y_Ptr is access all BLAS.Double_Complex_Vector (Y'Range);
            function Conv_X is new Unchecked_Conversion (Address, X_Ptr);
            function Conv_Y is new Unchecked_Conversion (Address, Y_Ptr);
         begin
            return To_Complex (BLAS.zdotu (N, Conv_X (X'Address).all, Inc_X,
                                     Conv_Y (Y'Address).all, Inc_Y));
         end;

      else
         return To_Complex (BLAS.zdotu (N, To_Double_Complex (X), Inc_X,
                                  To_Double_Complex (Y), Inc_Y));
      end if;
   end dot;

   ----------
   -- gemm --
   ----------

   procedure gemm
     (Trans_A : access constant Character;
      Trans_B : access constant Character;
      M       : Positive;
      N       : Positive;
      K       : Positive;
      Alpha   : Complex := (1.0, 0.0);
      A       : Complex_Matrix;
      Ld_A    : Integer;
      B       : Complex_Matrix;
      Ld_B    : Integer;
      Beta    : Complex := (0.0, 0.0);
      C       : in out Complex_Matrix;
      Ld_C    : Integer)
   is
   begin
      if Is_Single then
         declare
            subtype A_Type is BLAS.Complex_Matrix (A'Range (1), A'Range (2));
            subtype B_Type is BLAS.Complex_Matrix (B'Range (1), B'Range (2));
            type C_Ptr is
              access all BLAS.Complex_Matrix (C'Range (1), C'Range (2));
            function Conv_A is
               new Unchecked_Conversion (Complex_Matrix, A_Type);
            function Conv_B is
               new Unchecked_Conversion (Complex_Matrix, B_Type);
            function Conv_C is
               new Unchecked_Conversion (Address, C_Ptr);
         begin
            BLAS.cgemm (Trans_A, Trans_B, M, N, K, To_Fortran (Alpha),
                   Conv_A (A), Ld_A, Conv_B (B), Ld_B, To_Fortran (Beta),
                   Conv_C (C'Address).all, Ld_C);
         end;

      elsif Is_Double then
         declare
            subtype A_Type is
               BLAS.Double_Complex_Matrix (A'Range (1), A'Range (2));
            subtype B_Type is
               BLAS.Double_Complex_Matrix (B'Range (1), B'Range (2));
            type C_Ptr is access all
               BLAS.Double_Complex_Matrix (C'Range (1), C'Range (2));
            function Conv_A is
               new Unchecked_Conversion (Complex_Matrix, A_Type);
            function Conv_B is
               new Unchecked_Conversion (Complex_Matrix, B_Type);
            function Conv_C is new Unchecked_Conversion (Address, C_Ptr);
         begin
            BLAS.zgemm (Trans_A, Trans_B, M, N, K, To_Double_Complex (Alpha),
                   Conv_A (A), Ld_A, Conv_B (B), Ld_B,
                   To_Double_Complex (Beta),
                   Conv_C (C'Address).all, Ld_C);
         end;

      else
         declare
            DP_C : BLAS.Double_Complex_Matrix (C'Range (1), C'Range (2));
         begin
            if Beta.Re /= 0.0 or else Beta.Im /= 0.0 then
               DP_C := To_Double_Complex (C);
            end if;

            BLAS.zgemm (Trans_A, Trans_B, M, N, K, To_Double_Complex (Alpha),
                   To_Double_Complex (A), Ld_A,
                   To_Double_Complex (B), Ld_B, To_Double_Complex (Beta),
                   DP_C, Ld_C);

            C := To_Complex (DP_C);
         end;
      end if;
   end gemm;

   ----------
   -- gemv --
   ----------

   procedure gemv
     (Trans : access constant Character;
      M     : Natural := 0;
      N     : Natural := 0;
      Alpha : Complex := (1.0, 0.0);
      A     : Complex_Matrix;
      Ld_A  : Positive;
      X     : Complex_Vector;
      Inc_X : Integer := 1;
      Beta  : Complex := (0.0, 0.0);
      Y     : in out Complex_Vector;
      Inc_Y : Integer := 1)
   is
   begin
      if Is_Single then
         declare
            subtype A_Type is BLAS.Complex_Matrix (A'Range (1), A'Range (2));
            subtype X_Type is BLAS.Complex_Vector (X'Range);
            type Y_Ptr is access all BLAS.Complex_Vector (Y'Range);
            function Conv_A is
               new Unchecked_Conversion (Complex_Matrix, A_Type);
            function Conv_X is
               new Unchecked_Conversion (Complex_Vector, X_Type);
            function Conv_Y is
               new Unchecked_Conversion (Address, Y_Ptr);
         begin
            BLAS.cgemv (Trans, M, N, To_Fortran (Alpha),
                   Conv_A (A), Ld_A, Conv_X (X), Inc_X, To_Fortran (Beta),
                   Conv_Y (Y'Address).all, Inc_Y);
         end;

      elsif Is_Double then
         declare
            subtype A_Type is
               BLAS.Double_Complex_Matrix (A'Range (1), A'Range (2));
            subtype X_Type is
               BLAS.Double_Complex_Vector (X'Range);
            type Y_Ptr is access all BLAS.Double_Complex_Vector (Y'Range);
            function Conv_A is
               new Unchecked_Conversion (Complex_Matrix, A_Type);
            function Conv_X is
               new Unchecked_Conversion (Complex_Vector, X_Type);
            function Conv_Y is
               new Unchecked_Conversion (Address, Y_Ptr);
         begin
            BLAS.zgemv (Trans, M, N, To_Double_Complex (Alpha),
                   Conv_A (A), Ld_A, Conv_X (X), Inc_X,
                   To_Double_Complex (Beta),
                   Conv_Y (Y'Address).all, Inc_Y);
         end;

      else
         declare
            DP_Y : BLAS.Double_Complex_Vector (Y'Range);
         begin
            if Beta.Re /= 0.0 or else Beta.Im /= 0.0 then
               DP_Y := To_Double_Complex (Y);
            end if;

            BLAS.zgemv (Trans, M, N, To_Double_Complex (Alpha),
                   To_Double_Complex (A), Ld_A,
                   To_Double_Complex (X), Inc_X, To_Double_Complex (Beta),
                   DP_Y, Inc_Y);

            Y := To_Complex (DP_Y);
         end;
      end if;
   end gemv;

   ----------
   -- nrm2 --
   ----------

   function nrm2
     (N     : Natural;
      X     : Complex_Vector;
      Inc_X : Integer := 1) return Real
   is
   begin
      if Is_Single then
         declare
            subtype X_Type is BLAS.Complex_Vector (X'Range);
            function Conv_X is
               new Unchecked_Conversion (Complex_Vector, X_Type);
         begin
            return Real (BLAS.scnrm2 (N, Conv_X (X), Inc_X));
         end;

      elsif Is_Double then
         declare
            subtype X_Type is BLAS.Double_Complex_Vector (X'Range);
            function Conv_X is
               new Unchecked_Conversion (Complex_Vector, X_Type);
         begin
            return Real (BLAS.dznrm2 (N, Conv_X (X), Inc_X));
         end;

      else
         return Real (BLAS.dznrm2 (N, To_Double_Complex (X), Inc_X));
      end if;
   end nrm2;

end System.Generic_Complex_BLAS;