]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/libgnat/a-ngcoty.adb
[Ada] Bump copyright year
[thirdparty/gcc.git] / gcc / ada / libgnat / a-ngcoty.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . N U M E R I C S . G E N E R I C _ C O M P L E X _ T Y P E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2020, 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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with Ada.Numerics.Aux; use Ada.Numerics.Aux;
33
34 package body Ada.Numerics.Generic_Complex_Types is
35
36 subtype R is Real'Base;
37
38 Two_Pi : constant R := R (2.0) * Pi;
39 Half_Pi : constant R := Pi / R (2.0);
40
41 ---------
42 -- "*" --
43 ---------
44
45 function "*" (Left, Right : Complex) return Complex is
46
47 Scale : constant R := R (R'Machine_Radix) ** ((R'Machine_Emax - 1) / 2);
48 -- In case of overflow, scale the operands by the largest power of the
49 -- radix (to avoid rounding error), so that the square of the scale does
50 -- not overflow itself.
51
52 X : R;
53 Y : R;
54
55 begin
56 X := Left.Re * Right.Re - Left.Im * Right.Im;
57 Y := Left.Re * Right.Im + Left.Im * Right.Re;
58
59 -- If either component overflows, try to scale (skip in fast math mode)
60
61 if not Standard'Fast_Math then
62
63 -- Note that the test below is written as a negation. This is to
64 -- account for the fact that X and Y may be NaNs, because both of
65 -- their operands could overflow. Given that all operations on NaNs
66 -- return false, the test can only be written thus.
67
68 if not (abs (X) <= R'Last) then
69 pragma Annotate
70 (CodePeer, Intentional,
71 "test always false", "test for infinity");
72
73 X := Scale**2 * ((Left.Re / Scale) * (Right.Re / Scale) -
74 (Left.Im / Scale) * (Right.Im / Scale));
75 end if;
76
77 if not (abs (Y) <= R'Last) then
78 pragma Annotate
79 (CodePeer, Intentional,
80 "test always false", "test for infinity");
81
82 Y := Scale**2 * ((Left.Re / Scale) * (Right.Im / Scale)
83 + (Left.Im / Scale) * (Right.Re / Scale));
84 end if;
85 end if;
86
87 return (X, Y);
88 end "*";
89
90 function "*" (Left, Right : Imaginary) return Real'Base is
91 begin
92 return -(R (Left) * R (Right));
93 end "*";
94
95 function "*" (Left : Complex; Right : Real'Base) return Complex is
96 begin
97 return Complex'(Left.Re * Right, Left.Im * Right);
98 end "*";
99
100 function "*" (Left : Real'Base; Right : Complex) return Complex is
101 begin
102 return (Left * Right.Re, Left * Right.Im);
103 end "*";
104
105 function "*" (Left : Complex; Right : Imaginary) return Complex is
106 begin
107 return Complex'(-(Left.Im * R (Right)), Left.Re * R (Right));
108 end "*";
109
110 function "*" (Left : Imaginary; Right : Complex) return Complex is
111 begin
112 return Complex'(-(R (Left) * Right.Im), R (Left) * Right.Re);
113 end "*";
114
115 function "*" (Left : Imaginary; Right : Real'Base) return Imaginary is
116 begin
117 return Left * Imaginary (Right);
118 end "*";
119
120 function "*" (Left : Real'Base; Right : Imaginary) return Imaginary is
121 begin
122 return Imaginary (Left * R (Right));
123 end "*";
124
125 ----------
126 -- "**" --
127 ----------
128
129 function "**" (Left : Complex; Right : Integer) return Complex is
130 Result : Complex := (1.0, 0.0);
131 Factor : Complex := Left;
132 Exp : Integer := Right;
133
134 begin
135 -- We use the standard logarithmic approach, Exp gets shifted right
136 -- testing successive low order bits and Factor is the value of the
137 -- base raised to the next power of 2. For positive exponents we
138 -- multiply the result by this factor, for negative exponents, we
139 -- divide by this factor.
140
141 if Exp >= 0 then
142
143 -- For a positive exponent, if we get a constraint error during
144 -- this loop, it is an overflow, and the constraint error will
145 -- simply be passed on to the caller.
146
147 while Exp /= 0 loop
148 if Exp rem 2 /= 0 then
149 Result := Result * Factor;
150 end if;
151
152 Factor := Factor * Factor;
153 Exp := Exp / 2;
154 end loop;
155
156 return Result;
157
158 else -- Exp < 0 then
159
160 -- For the negative exponent case, a constraint error during this
161 -- calculation happens if Factor gets too large, and the proper
162 -- response is to return 0.0, since what we essentially have is
163 -- 1.0 / infinity, and the closest model number will be zero.
164
165 begin
166 while Exp /= 0 loop
167 if Exp rem 2 /= 0 then
168 Result := Result * Factor;
169 end if;
170
171 Factor := Factor * Factor;
172 Exp := Exp / 2;
173 end loop;
174
175 return R'(1.0) / Result;
176
177 exception
178 when Constraint_Error =>
179 return (0.0, 0.0);
180 end;
181 end if;
182 end "**";
183
184 function "**" (Left : Imaginary; Right : Integer) return Complex is
185 M : constant R := R (Left) ** Right;
186 begin
187 case Right mod 4 is
188 when 0 => return (M, 0.0);
189 when 1 => return (0.0, M);
190 when 2 => return (-M, 0.0);
191 when 3 => return (0.0, -M);
192 when others => raise Program_Error;
193 end case;
194 end "**";
195
196 ---------
197 -- "+" --
198 ---------
199
200 function "+" (Right : Complex) return Complex is
201 begin
202 return Right;
203 end "+";
204
205 function "+" (Left, Right : Complex) return Complex is
206 begin
207 return Complex'(Left.Re + Right.Re, Left.Im + Right.Im);
208 end "+";
209
210 function "+" (Right : Imaginary) return Imaginary is
211 begin
212 return Right;
213 end "+";
214
215 function "+" (Left, Right : Imaginary) return Imaginary is
216 begin
217 return Imaginary (R (Left) + R (Right));
218 end "+";
219
220 function "+" (Left : Complex; Right : Real'Base) return Complex is
221 begin
222 return Complex'(Left.Re + Right, Left.Im);
223 end "+";
224
225 function "+" (Left : Real'Base; Right : Complex) return Complex is
226 begin
227 return Complex'(Left + Right.Re, Right.Im);
228 end "+";
229
230 function "+" (Left : Complex; Right : Imaginary) return Complex is
231 begin
232 return Complex'(Left.Re, Left.Im + R (Right));
233 end "+";
234
235 function "+" (Left : Imaginary; Right : Complex) return Complex is
236 begin
237 return Complex'(Right.Re, R (Left) + Right.Im);
238 end "+";
239
240 function "+" (Left : Imaginary; Right : Real'Base) return Complex is
241 begin
242 return Complex'(Right, R (Left));
243 end "+";
244
245 function "+" (Left : Real'Base; Right : Imaginary) return Complex is
246 begin
247 return Complex'(Left, R (Right));
248 end "+";
249
250 ---------
251 -- "-" --
252 ---------
253
254 function "-" (Right : Complex) return Complex is
255 begin
256 return (-Right.Re, -Right.Im);
257 end "-";
258
259 function "-" (Left, Right : Complex) return Complex is
260 begin
261 return (Left.Re - Right.Re, Left.Im - Right.Im);
262 end "-";
263
264 function "-" (Right : Imaginary) return Imaginary is
265 begin
266 return Imaginary (-R (Right));
267 end "-";
268
269 function "-" (Left, Right : Imaginary) return Imaginary is
270 begin
271 return Imaginary (R (Left) - R (Right));
272 end "-";
273
274 function "-" (Left : Complex; Right : Real'Base) return Complex is
275 begin
276 return Complex'(Left.Re - Right, Left.Im);
277 end "-";
278
279 function "-" (Left : Real'Base; Right : Complex) return Complex is
280 begin
281 return Complex'(Left - Right.Re, -Right.Im);
282 end "-";
283
284 function "-" (Left : Complex; Right : Imaginary) return Complex is
285 begin
286 return Complex'(Left.Re, Left.Im - R (Right));
287 end "-";
288
289 function "-" (Left : Imaginary; Right : Complex) return Complex is
290 begin
291 return Complex'(-Right.Re, R (Left) - Right.Im);
292 end "-";
293
294 function "-" (Left : Imaginary; Right : Real'Base) return Complex is
295 begin
296 return Complex'(-Right, R (Left));
297 end "-";
298
299 function "-" (Left : Real'Base; Right : Imaginary) return Complex is
300 begin
301 return Complex'(Left, -R (Right));
302 end "-";
303
304 ---------
305 -- "/" --
306 ---------
307
308 function "/" (Left, Right : Complex) return Complex is
309 a : constant R := Left.Re;
310 b : constant R := Left.Im;
311 c : constant R := Right.Re;
312 d : constant R := Right.Im;
313
314 begin
315 if c = 0.0 and then d = 0.0 then
316 raise Constraint_Error;
317 else
318 return Complex'(Re => ((a * c) + (b * d)) / (c ** 2 + d ** 2),
319 Im => ((b * c) - (a * d)) / (c ** 2 + d ** 2));
320 end if;
321 end "/";
322
323 function "/" (Left, Right : Imaginary) return Real'Base is
324 begin
325 return R (Left) / R (Right);
326 end "/";
327
328 function "/" (Left : Complex; Right : Real'Base) return Complex is
329 begin
330 return Complex'(Left.Re / Right, Left.Im / Right);
331 end "/";
332
333 function "/" (Left : Real'Base; Right : Complex) return Complex is
334 a : constant R := Left;
335 c : constant R := Right.Re;
336 d : constant R := Right.Im;
337 begin
338 return Complex'(Re => (a * c) / (c ** 2 + d ** 2),
339 Im => -((a * d) / (c ** 2 + d ** 2)));
340 end "/";
341
342 function "/" (Left : Complex; Right : Imaginary) return Complex is
343 a : constant R := Left.Re;
344 b : constant R := Left.Im;
345 d : constant R := R (Right);
346
347 begin
348 return (b / d, -(a / d));
349 end "/";
350
351 function "/" (Left : Imaginary; Right : Complex) return Complex is
352 b : constant R := R (Left);
353 c : constant R := Right.Re;
354 d : constant R := Right.Im;
355
356 begin
357 return (Re => b * d / (c ** 2 + d ** 2),
358 Im => b * c / (c ** 2 + d ** 2));
359 end "/";
360
361 function "/" (Left : Imaginary; Right : Real'Base) return Imaginary is
362 begin
363 return Imaginary (R (Left) / Right);
364 end "/";
365
366 function "/" (Left : Real'Base; Right : Imaginary) return Imaginary is
367 begin
368 return Imaginary (-(Left / R (Right)));
369 end "/";
370
371 ---------
372 -- "<" --
373 ---------
374
375 function "<" (Left, Right : Imaginary) return Boolean is
376 begin
377 return R (Left) < R (Right);
378 end "<";
379
380 ----------
381 -- "<=" --
382 ----------
383
384 function "<=" (Left, Right : Imaginary) return Boolean is
385 begin
386 return R (Left) <= R (Right);
387 end "<=";
388
389 ---------
390 -- ">" --
391 ---------
392
393 function ">" (Left, Right : Imaginary) return Boolean is
394 begin
395 return R (Left) > R (Right);
396 end ">";
397
398 ----------
399 -- ">=" --
400 ----------
401
402 function ">=" (Left, Right : Imaginary) return Boolean is
403 begin
404 return R (Left) >= R (Right);
405 end ">=";
406
407 -----------
408 -- "abs" --
409 -----------
410
411 function "abs" (Right : Imaginary) return Real'Base is
412 begin
413 return abs R (Right);
414 end "abs";
415
416 --------------
417 -- Argument --
418 --------------
419
420 function Argument (X : Complex) return Real'Base is
421 a : constant R := X.Re;
422 b : constant R := X.Im;
423 arg : R;
424
425 begin
426 if b = 0.0 then
427
428 if a >= 0.0 then
429 return 0.0;
430 else
431 return R'Copy_Sign (Pi, b);
432 end if;
433
434 elsif a = 0.0 then
435
436 if b >= 0.0 then
437 return Half_Pi;
438 else
439 return -Half_Pi;
440 end if;
441
442 else
443 arg := R (Atan (Double (abs (b / a))));
444
445 if a > 0.0 then
446 if b > 0.0 then
447 return arg;
448 else -- b < 0.0
449 return -arg;
450 end if;
451
452 else -- a < 0.0
453 if b >= 0.0 then
454 return Pi - arg;
455 else -- b < 0.0
456 return -(Pi - arg);
457 end if;
458 end if;
459 end if;
460
461 exception
462 when Constraint_Error =>
463 if b > 0.0 then
464 return Half_Pi;
465 else
466 return -Half_Pi;
467 end if;
468 end Argument;
469
470 function Argument (X : Complex; Cycle : Real'Base) return Real'Base is
471 begin
472 if Cycle > 0.0 then
473 return Argument (X) * Cycle / Two_Pi;
474 else
475 raise Argument_Error;
476 end if;
477 end Argument;
478
479 ----------------------------
480 -- Compose_From_Cartesian --
481 ----------------------------
482
483 function Compose_From_Cartesian (Re, Im : Real'Base) return Complex is
484 begin
485 return (Re, Im);
486 end Compose_From_Cartesian;
487
488 function Compose_From_Cartesian (Re : Real'Base) return Complex is
489 begin
490 return (Re, 0.0);
491 end Compose_From_Cartesian;
492
493 function Compose_From_Cartesian (Im : Imaginary) return Complex is
494 begin
495 return (0.0, R (Im));
496 end Compose_From_Cartesian;
497
498 ------------------------
499 -- Compose_From_Polar --
500 ------------------------
501
502 function Compose_From_Polar (
503 Modulus, Argument : Real'Base)
504 return Complex
505 is
506 begin
507 if Modulus = 0.0 then
508 return (0.0, 0.0);
509 else
510 return (Modulus * R (Cos (Double (Argument))),
511 Modulus * R (Sin (Double (Argument))));
512 end if;
513 end Compose_From_Polar;
514
515 function Compose_From_Polar (
516 Modulus, Argument, Cycle : Real'Base)
517 return Complex
518 is
519 Arg : Real'Base;
520
521 begin
522 if Modulus = 0.0 then
523 return (0.0, 0.0);
524
525 elsif Cycle > 0.0 then
526 if Argument = 0.0 then
527 return (Modulus, 0.0);
528
529 elsif Argument = Cycle / 4.0 then
530 return (0.0, Modulus);
531
532 elsif Argument = Cycle / 2.0 then
533 return (-Modulus, 0.0);
534
535 elsif Argument = 3.0 * Cycle / R (4.0) then
536 return (0.0, -Modulus);
537 else
538 Arg := Two_Pi * Argument / Cycle;
539 return (Modulus * R (Cos (Double (Arg))),
540 Modulus * R (Sin (Double (Arg))));
541 end if;
542 else
543 raise Argument_Error;
544 end if;
545 end Compose_From_Polar;
546
547 ---------------
548 -- Conjugate --
549 ---------------
550
551 function Conjugate (X : Complex) return Complex is
552 begin
553 return Complex'(X.Re, -X.Im);
554 end Conjugate;
555
556 --------
557 -- Im --
558 --------
559
560 function Im (X : Complex) return Real'Base is
561 begin
562 return X.Im;
563 end Im;
564
565 function Im (X : Imaginary) return Real'Base is
566 begin
567 return R (X);
568 end Im;
569
570 -------------
571 -- Modulus --
572 -------------
573
574 function Modulus (X : Complex) return Real'Base is
575 Re2, Im2 : R;
576
577 begin
578
579 begin
580 Re2 := X.Re ** 2;
581
582 -- To compute (a**2 + b**2) ** (0.5) when a**2 may be out of bounds,
583 -- compute a * (1 + (b/a) **2) ** (0.5). On a machine where the
584 -- squaring does not raise constraint_error but generates infinity,
585 -- we can use an explicit comparison to determine whether to use
586 -- the scaling expression.
587
588 -- The scaling expression is computed in double format throughout
589 -- in order to prevent inaccuracies on machines where not all
590 -- immediate expressions are rounded, such as PowerPC.
591
592 -- ??? same weird test, why not Re2 > R'Last ???
593 if not (Re2 <= R'Last) then
594 raise Constraint_Error;
595 end if;
596
597 exception
598 when Constraint_Error =>
599 pragma Assert (X.Re /= 0.0);
600 return R (Double (abs (X.Re))
601 * Sqrt (1.0 + (Double (X.Im) / Double (X.Re)) ** 2));
602 end;
603
604 begin
605 Im2 := X.Im ** 2;
606
607 -- ??? same weird test
608 if not (Im2 <= R'Last) then
609 raise Constraint_Error;
610 end if;
611
612 exception
613 when Constraint_Error =>
614 pragma Assert (X.Im /= 0.0);
615 return R (Double (abs (X.Im))
616 * Sqrt (1.0 + (Double (X.Re) / Double (X.Im)) ** 2));
617 end;
618
619 -- Now deal with cases of underflow. If only one of the squares
620 -- underflows, return the modulus of the other component. If both
621 -- squares underflow, use scaling as above.
622
623 if Re2 = 0.0 then
624
625 if X.Re = 0.0 then
626 return abs (X.Im);
627
628 elsif Im2 = 0.0 then
629
630 if X.Im = 0.0 then
631 return abs (X.Re);
632
633 else
634 if abs (X.Re) > abs (X.Im) then
635 return
636 R (Double (abs (X.Re))
637 * Sqrt (1.0 + (Double (X.Im) / Double (X.Re)) ** 2));
638 else
639 return
640 R (Double (abs (X.Im))
641 * Sqrt (1.0 + (Double (X.Re) / Double (X.Im)) ** 2));
642 end if;
643 end if;
644
645 else
646 return abs (X.Im);
647 end if;
648
649 elsif Im2 = 0.0 then
650 return abs (X.Re);
651
652 -- In all other cases, the naive computation will do
653
654 else
655 return R (Sqrt (Double (Re2 + Im2)));
656 end if;
657 end Modulus;
658
659 --------
660 -- Re --
661 --------
662
663 function Re (X : Complex) return Real'Base is
664 begin
665 return X.Re;
666 end Re;
667
668 ------------
669 -- Set_Im --
670 ------------
671
672 procedure Set_Im (X : in out Complex; Im : Real'Base) is
673 begin
674 X.Im := Im;
675 end Set_Im;
676
677 procedure Set_Im (X : out Imaginary; Im : Real'Base) is
678 begin
679 X := Imaginary (Im);
680 end Set_Im;
681
682 ------------
683 -- Set_Re --
684 ------------
685
686 procedure Set_Re (X : in out Complex; Re : Real'Base) is
687 begin
688 X.Re := Re;
689 end Set_Re;
690
691 end Ada.Numerics.Generic_Complex_Types;