blob: 53f7ec38c376304e67b2901b72908044076922d8 [file]
{ Process this file with sppp.awk -*- mode: a68 -*- }
{ gnu.a68 - GNU prelude.
Copyright (C) 2026 Jose E. Marchesi
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
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/>. }
module GNU =
def
{ GNU68-2026-004-fixed-sized-modes }
{iter L {} {long} {long long} }
{ Operations for identifying NaN (not a number) and Inf (infinities). }
pub op ISNAN = ({L} real x) bool: (x=x | false | true);
pub op ISINF = ({L} real x) bool:
( x = {L} 1.0 / {L} 0.0 OR x = - {L} 1.0 / {L} 0.0 | true | false);
pub op ISNAN = ({L} compl x) bool: (ISNAN re'x OR ISNAN im'x);
pub op ISINF = ({L} compl x) bool: (ISINF re'x OR ISINF im'x);
{reti}
{iter L {} {word}}
{iter K {} {WORDEN}}
{iter L_ {} {word_}}
pub op = = ({L} compl a,b) bool: (re'a = re'b AND im'a = im'b);
pub op EQ = ({L} compl a,b) bool: a = b;
pub op /= = ({L} compl a,b) bool: NOT (a = b);
pub op NE = ({L} compl a,b) bool: a /= b;
pub op + = ({L} compl a,b) {L} compl: (re'a + re'b, im'a + im'b);
pub op +:= = (ref {L} compl a, {L} compl b) ref {L} compl:
(a := a + b; a);
pub op PLUSAB = (ref {L} compl a, {L} compl b) ref {L} compl:
a +:= b;
pub op - = ({L} compl a) {L} compl: (-re'a, -im'a);
pub op - = ({L} compl a,b) {L} compl: (re'a - re'b, im'a - im'b);
pub op -:= = (ref {L} compl a, {L} compl b) ref {L} compl:
(a := a - b; a);
pub op MINUSAB = (ref {L} compl a, {L} compl b) ref {L} compl:
a -:= b;
pub op * = ({L} compl a,b) {L} compl:
(re'a * re'b - im'a * im'b, re'a * im'b + im'a * re'b);
pub op *:= = (ref {L} compl a, {L} compl b) ref {L} compl:
(a := a * b; a);
pub op TIMESAB = (ref {L} compl a, {L} compl b) ref {L} compl:
a *:= b;
pub op / = ({L} compl a,b) {L} compl:
begin
{L} real d = re'(b * CONJ b);
{L} compl n = a * CONJ b;
(re'n/d, im'n/d)
end;
pub op /:= = (ref {L} compl a, {L} compl b) ref {L} compl:
(a := a / b; a);
pub op DIVAB = (ref {L} compl a, {L} compl b) ref {L} compl:
a /:= b;
pub op CONJ = ({L} compl z) {L} compl: (re'z, -im'z);
pub op ABS = ({L} compl z) {L} real: {L_}sqrt(re'z*re'z + im'z*im'z);
pub op ARG = ({L} compl z) {L} real:
if {L} real zr = re'z, zi = im'z;
zr /= {L} 0 OR zi /= {L} 0
then if ABS zr > ABS zi
then {L_}arctan(zi/zr)
+ {L_}pi/{L} 2*(zi<{L} 0 | {K} SIGN zr - {L} 1 | {L} 1 - {K} SIGN zr)
else -{L_}arctan(zr/zi) + {L_}pi/{L} 2*{K} SIGN zi
fi
fi;
pub op ** = ({L} compl z, int n) {L} compl:
{ Handle special cases for z at origin. }
if z = {L} compl ({L} 0.0, {L} 0.0)
then if n = 0
then ({L} 1.0, {L} 0.0)
elif n < 0
then { Return NaN. }
({L} 0.0 / {L} 0.0, {L} 0.0 / {L} 0.0)
else
({L} 0.0, {L} 0.0)
fi
else
int nn := Abs n;
{L} compl z_pow_2 := z;
{L} compl z_out := ({L} 1.0, {L} 0.0);
while nn > 0
do
if nn Mod 2 = 1
then z_out := z_out*z_pow2
fi;
{ We want z_pow2 := z_pow2*z_pow2; next line is fast to calculate this. }
z_pow2 := (re'z_pow2 * re'z_pow2 - im'z_pow2 * im'z_pow2,
{L} 2.0 * re'z_pow2 * im'z_pow2);
nn := nn % 2
od;
(n < 0 | {L} compl ({L} 1.0, {L} 0.0)/z_out | z_out)
fi;
{reti}
skip
fed