MATHLIB User Guide
MATHLIB_sqrt_scalar.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  * MATHLIB -- TI Floating-Point Math Function Library *
3  * *
4  * *
5  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/ *
6  * *
7  * *
8  * Redistribution and use in source and binary forms, with or without *
9  * modification, are permitted provided that the following conditions *
10  * are met: *
11  * *
12  * Redistributions of source code must retain the above copyright *
13  * notice, this list of conditions and the following disclaimer. *
14  * *
15  * Redistributions in binary form must reproduce the above copyright *
16  * notice, this list of conditions and the following disclaimer in the *
17  * documentation and/or other materials provided with the *
18  * distribution. *
19  * *
20  * Neither the name of Texas Instruments Incorporated nor the names of *
21  * its contributors may be used to endorse or promote products derived *
22  * from this software without specific prior written permission. *
23  * *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
27  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
28  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
34  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
35  * ======================================================================== */
36 
37 /* ======================================================================= */
38 /* MATHLIB_sqrt_scalar.h - single and double precision floating point sqrt */
39 /* optimized inlined C implementation (w/ intrinsics) */
40 /* ======================================================================= */
41 
42 #ifndef MATHLIB_SQRT_SCALAR_H_
43 #define MATHLIB_SQRT_SCALAR_H_ 1
44 
45 #include <c6x_migration.h>
46 #include <float.h>
47 
48 #ifndef __cplusplus /* FOR PROTECTION PURPOSE - C++ NOT SUPPORTED. */
49 #pragma CODE_SECTION(MATHLIB_sqrt_scalar, ".text:optci");
50 #endif
51 
52 /* =========================================================================== */
53 /* The sqrtsp function returns the square root of a real floating-point value. */
54 /* Newton-Rhapson algorithm is applied for better precision. */
55 /* =========================================================================== */
56 
57 template <typename T> static inline T MATHLIB_sqrt_scalar_ci(T a);
58 
59 template <> inline float MATHLIB_sqrt_scalar_ci<float>(float a)
60 {
61  const float Half = 0.5f;
62  const float OneP5 = 1.5f;
63  float x, y;
64  int i;
65 
66  x = _rsqrsp(a); /* compute square root reciprocal */
67 
68 #pragma UNROLL(1) /* PRAGMA: do not unroll this loop */
69  for (i = 0; i < 2; i++) {
70  x = x * (OneP5 - (a * x * x * Half));
71  }
72  y = a * x;
73 
74  if (a <= 0.0f) {
75  y = 0.0f;
76  }
77  if (a > FLT_MAX) {
78  y = FLT_MAX;
79  }
80 
81  return (y);
82 }
83 
84 template <> inline double MATHLIB_sqrt_scalar_ci<double>(double a)
85 {
86  double half = 0.5;
87  double OneP5 = 1.5;
88  double x, y;
89  int i;
90 
91  x = _rsqrdp(a);
92 
93 #pragma UNROLL(1) /* PRAGMA: do not unroll this loop */
94  for (i = 0; i < 3; i++) {
95  x = x * (OneP5 - (a * x * x * half));
96  }
97  y = a * x;
98 
99  if (a <= 0.0) {
100  y = 0.0;
101  }
102  if (a > DBL_MAX) {
103  y = DBL_MAX;
104  }
105 
106  return (y);
107 }
108 
109 extern "C" {
110 static inline float MATHLIB_sqrt_scalar_sp(float a)
111 {
112  float result = MATHLIB_sqrt_scalar_ci<float>(a);
113  return result;
114 }
115 
116 static inline double MATHLIB_sqrt_scalar_dp(double a)
117 {
118  double result = MATHLIB_sqrt_scalar_ci<double>(a);
119  return result;
120 }
121 }
122 
123 #endif /* MATHLIB_SQRT_SCALAR_H_ */
124 
125 /* ======================================================================== */
126 /* End of file: MATHLIB_sqrt_scalar.h */
127 /* ======================================================================== */
float MATHLIB_sqrt_scalar_ci< float >(float a)
static double MATHLIB_sqrt_scalar_dp(double a)
double MATHLIB_sqrt_scalar_ci< double >(double a)
static float MATHLIB_sqrt_scalar_sp(float a)
static T MATHLIB_sqrt_scalar_ci(T a)