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