89 integer, parameter :: wp = kind(1.e0)
90
91
92
93
94
95
96 real(wp), parameter :: zero = 0.0_wp
97 real(wp), parameter :: one = 1.0_wp
98 complex(wp), parameter :: czero = 0.0_wp
99
100
101 real(wp), parameter :: safmin = real(radix(0._wp),wp)**max( &
102 minexponent(0._wp)-1, &
103 1-maxexponent(0._wp) &
104 )
105 real(wp), parameter :: safmax = real(radix(0._wp),wp)**max( &
106 1-minexponent(0._wp), &
107 maxexponent(0._wp)-1 &
108 )
109 real(wp), parameter :: rtmin = sqrt( safmin )
110
111
112 real(wp) :: c
113 complex(wp) :: a, b, s
114
115
116 real(wp) :: d, f1, f2, g1, g2, h2, u, v, w, rtmax
117 complex(wp) :: f, fs, g, gs, r, t
118
119
120 intrinsic :: abs, aimag, conjg, max, min, real, sqrt
121
122
123 real(wp) :: ABSSQ
124
125
126 abssq( t ) = real( t )**2 + aimag( t )**2
127
128
129
130 f = a
131 g = b
132 if( g == czero ) then
133 c = one
134 s = czero
135 r = f
136 else if( f == czero ) then
137 c = zero
138 if( real(g) == zero ) then
139 r = abs(aimag(g))
140 s = conjg( g ) / r
141 elseif( aimag(g) == zero ) then
142 r = abs(real(g))
143 s = conjg( g ) / r
144 else
145 g1 = max( abs(real(g)), abs(aimag(g)) )
146 rtmax = sqrt( safmax/2 )
147 if( g1 > rtmin .and. g1 < rtmax ) then
148
149
150
151
152
153 g2 = abssq( g )
154 d = sqrt( g2 )
155 s = conjg( g ) / d
156 r = d
157 else
158
159
160
161 u = min( safmax, max( safmin, g1 ) )
162 gs = g / u
163
164
165 g2 = abssq( gs )
166 d = sqrt( g2 )
167 s = conjg( gs ) / d
168 r = d*u
169 end if
170 end if
171 else
172 f1 = max( abs(real(f)), abs(aimag(f)) )
173 g1 = max( abs(real(g)), abs(aimag(g)) )
174 rtmax = sqrt( safmax/4 )
175 if( f1 > rtmin .and. f1 < rtmax .and. &
176 g1 > rtmin .and. g1 < rtmax ) then
177
178
179
180 f2 = abssq( f )
181 g2 = abssq( g )
182 h2 = f2 + g2
183
184 if( f2 >= h2 * safmin ) then
185
186 c = sqrt( f2 / h2 )
187 r = f / c
188 rtmax = rtmax * 2
189 if( f2 > rtmin .and. h2 < rtmax ) then
190
191 s = conjg( g ) * ( f / sqrt( f2*h2 ) )
192 else
193 s = conjg( g ) * ( r / h2 )
194 end if
195 else
196
197
198
199
200
201
202 d = sqrt( f2 * h2 )
203 c = f2 / d
204 if( c >= safmin ) then
205 r = f / c
206 else
207
208
209 r = f * ( h2 / d )
210 end if
211 s = conjg( g ) * ( f / d )
212 end if
213 else
214
215
216
217 u = min( safmax, max( safmin, f1, g1 ) )
218 gs = g / u
219 g2 = abssq( gs )
220 if( f1 / u < rtmin ) then
221
222
223
224
225 v = min( safmax, max( safmin, f1 ) )
226 w = v / u
227 fs = f / v
228 f2 = abssq( fs )
229 h2 = f2*w**2 + g2
230 else
231
232
233
234 w = one
235 fs = f / u
236 f2 = abssq( fs )
237 h2 = f2 + g2
238 end if
239
240 if( f2 >= h2 * safmin ) then
241
242 c = sqrt( f2 / h2 )
243 r = fs / c
244 rtmax = rtmax * 2
245 if( f2 > rtmin .and. h2 < rtmax ) then
246
247 s = conjg( gs ) * ( fs / sqrt( f2*h2 ) )
248 else
249 s = conjg( gs ) * ( r / h2 )
250 end if
251 else
252
253
254
255
256
257
258 d = sqrt( f2 * h2 )
259 c = f2 / d
260 if( c >= safmin ) then
261 r = fs / c
262 else
263
264
265 r = fs * ( h2 / d )
266 end if
267 s = conjg( gs ) * ( fs / d )
268 end if
269
270 c = c * w
271 r = r * u
272 end if
273 end if
274 a = r
275 return