MCPcopy Create free account
hub / github.com/apache/singa / hypot

Function hypot

include/half.hpp:3290–3324  ·  view source on GitHub ↗

Hypotenuse function. This function is exact to rounding for all rounding modes. See also:** Documentation for [std::hypot](https://en.cppreference.com/w/cpp/numeric/math/hypot). \param x first argument \param y second argument \return square root of sum of squares without internal over- or underflows \exception FE_INVALID if \a x or \a y is signaling NaN \exception FE_OVERFLOW, ...UNDERFLOW, ...I

Source from the content-addressed store, hash-verified

3288 /// \exception FE_INVALID if \a x or \a y is signaling NaN
3289 /// \exception FE_OVERFLOW, ...UNDERFLOW, ...INEXACT according to rounding of the final square root
3290 inline half hypot(half x, half y)
3291 {
3292 #ifdef HALF_ARITHMETIC_TYPE
3293 detail::internal_t fx = detail::half2float<detail::internal_t>(x.data_), fy = detail::half2float<detail::internal_t>(y.data_);
3294 #if HALF_ENABLE_CPP11_CMATH
3295 return half(detail::binary, detail::float2half<half::round_style>(std::hypot(fx, fy)));
3296 #else
3297 return half(detail::binary, detail::float2half<half::round_style>(std::sqrt(fx*fx+fy*fy)));
3298 #endif
3299 #else
3300 int absx = x.data_ & 0x7FFF, absy = y.data_ & 0x7FFF, expx = 0, expy = 0;
3301 if(absx >= 0x7C00 || absy >= 0x7C00)
3302 return half(detail::binary, (absx==0x7C00) ? detail::select(0x7C00, y.data_) :
3303 (absy==0x7C00) ? detail::select(0x7C00, x.data_) : detail::signal(x.data_, y.data_));
3304 if(!absx)
3305 return half(detail::binary, absy ? detail::check_underflow(absy) : 0);
3306 if(!absy)
3307 return half(detail::binary, detail::check_underflow(absx));
3308 if(absy > absx)
3309 std::swap(absx, absy);
3310 for(; absx<0x400; absx<<=1,--expx) ;
3311 for(; absy<0x400; absy<<=1,--expy) ;
3312 detail::uint32 mx = (absx&0x3FF) | 0x400, my = (absy&0x3FF) | 0x400;
3313 mx *= mx;
3314 my *= my;
3315 int ix = mx >> 21, iy = my >> 21;
3316 expx = 2*(expx+(absx>>10)) - 15 + ix;
3317 expy = 2*(expy+(absy>>10)) - 15 + iy;
3318 mx <<= 10 - ix;
3319 my <<= 10 - iy;
3320 int d = expx - expy;
3321 my = (d<30) ? ((my>>d)|((my&((static_cast<detail::uint32>(1)<<d)-1))!=0)) : 1;
3322 return half(detail::binary, detail::hypot_post<half::round_style>(mx+my, expx));
3323 #endif
3324 }
3325
3326 /// Hypotenuse function.
3327 /// This function is exact to rounding for all rounding modes.

Callers

nothing calls this directly

Calls 5

halfClass · 0.85
selectFunction · 0.85
signalFunction · 0.85
check_underflowFunction · 0.85
sqrtFunction · 0.70

Tested by

no test coverage detected