pythag.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*Daala video codec
  2. Copyright (c) 2005-2008 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #if !defined(_cpack_linalg_pythag_H)
  21. # define _cpack_linalg_pythag_H (1)
  22. # include <math.h>
  23. /*Computes the sqrt(a*a+b*b) without destructive underflow.*/
  24. # if defined(__GNUC__)&& \
  25. (defined(__USE_MISC)||defined(__USE_XOPEN)||defined(__USE_ISOC99))
  26. # define pythag hypot
  27. # else
  28. static double pythag(double _a,double _b){
  29. double absa;
  30. double absb;
  31. double s;
  32. int sb;
  33. absa=fabs(_a);
  34. absb=fabs(_b);
  35. /*frexp() should be good enough (instead of using the recriprocal), and is
  36. finally properly optimized in gcc 4.3, so it might actually be faster.
  37. It also doesn't introduce rounding error due to the division and reciprocal
  38. multiplication.*/
  39. s=frexp(absa>absb?absa:absb,&sb);
  40. /*Technically <1 is sufficient, but <= is perhaps safer against
  41. non-conforming implementations of frexp().*/
  42. if(s>0&&s<=1){
  43. double a2;
  44. double b2;
  45. a2=ldexp(absa,-sb);
  46. b2=ldexp(absb,-sb);
  47. return ldexp(sqrt(a2*a2+b2*b2),sb);
  48. }
  49. /*frexp() returns 0 when its argument is 0, and +/- Inf when its argument is
  50. +/- Inf.*/
  51. else return s;
  52. }
  53. # endif
  54. #endif