89 integer, parameter :: wp = kind(1.d0)
90 real(wp) :: DNRM2
91
92
93
94
95
96
97
98 real(wp), parameter :: zero = 0.0_wp
99 real(wp), parameter :: one = 1.0_wp
100 real(wp), parameter :: maxN = huge(0.0_wp)
101
102
103 real(wp), parameter :: tsml = real(radix(0._wp), wp)**ceiling( &
104 (minexponent(0._wp) - 1) * 0.5_wp)
105 real(wp), parameter :: tbig = real(radix(0._wp), wp)**floor( &
106 (maxexponent(0._wp) - digits(0._wp) + 1) * 0.5_wp)
107 real(wp), parameter :: ssml = real(radix(0._wp), wp)**( - floor( &
108 (minexponent(0._wp) - digits(0._wp)) * 0.5_wp))
109 real(wp), parameter :: sbig = real(radix(0._wp), wp)**( - ceiling( &
110 (maxexponent(0._wp) + digits(0._wp) - 1) * 0.5_wp))
111
112
113 integer :: incx, n
114
115
116 real(wp) :: x(*)
117
118
119 integer :: i, ix
120 logical :: notbig
121 real(wp) :: abig, amed, asml, ax, scl, sumsq, ymax, ymin
122
123
124
126 if( n <= 0 ) return
127
128 scl = one
129 sumsq = zero
130
131
132
133
134
135
136
137
138
139 notbig = .true.
140 asml = zero
141 amed = zero
142 abig = zero
143 ix = 1
144 if( incx < 0 ) ix = 1 - (n-1)*incx
145 do i = 1, n
146 ax = abs(x(ix))
147 if (ax > tbig) then
148 abig = abig + (ax*sbig)**2
149 notbig = .false.
150 else if (ax < tsml) then
151 if (notbig) asml = asml + (ax*ssml)**2
152 else
153 amed = amed + ax**2
154 end if
155 ix = ix + incx
156 end do
157
158
159
160
161 if (abig > zero) then
162
163
164
165 if ( (amed > zero) .or. (amed > maxn) .or. (amed /= amed) ) then
166 abig = abig + (amed*sbig)*sbig
167 end if
168 scl = one / sbig
169 sumsq = abig
170 else if (asml > zero) then
171
172
173
174 if ( (amed > zero) .or. (amed > maxn) .or. (amed /= amed) ) then
175 amed = sqrt(amed)
176 asml = sqrt(asml) / ssml
177 if (asml > amed) then
178 ymin = amed
179 ymax = asml
180 else
181 ymin = asml
182 ymax = amed
183 end if
184 scl = one
185 sumsq = ymax**2*( one + (ymin/ymax)**2 )
186 else
187 scl = one / ssml
188 sumsq = asml
189 end if
190 else
191
192
193
194 scl = one
195 sumsq = amed
196 end if
197 dnrm2 = scl*sqrt( sumsq )
198 return
real(wp) function dnrm2(n, x, incx)
DNRM2