SST  10.1.0
StructuralSimulationToolkit
sqrt.h
1 // Copyright 2009-2020 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2020, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 
13 #ifndef _H_SST_CORE_MATH_SQRT
14 #define _H_SST_CORE_MATH_SQRT
15 
16 namespace SST {
17 namespace Math {
18 
19 // Implements uint32_t square root based on algorithm from:
20 // Reference: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
21 static inline uint32_t square_root(const uint32_t input) {
22 
23  uint32_t op = input;
24  uint32_t res = 0;
25  uint32_t one = 1uL << 30;
26 
27  while (one > op)
28  {
29  one >>= 2;
30  }
31 
32  while (one != 0)
33  {
34  if (op >= res + one)
35  {
36  op = op - (res + one);
37  res = res + 2 * one;
38  }
39 
40  res >>= 1;
41  one >>= 2;
42  }
43 
44  return res;
45 };
46 
47 }
48 }
49 
50 #endif