SST 12.1.0
Structural Simulation Toolkit
sqrt.h
1// Copyright 2009-2022 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-2022, 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#ifndef SST_CORE_MATH_SQRT_H
13#define SST_CORE_MATH_SQRT_H
14
15namespace SST {
16namespace Math {
17
18// Implements uint32_t square root based on algorithm from:
19// Reference: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
20static inline uint32_t
21square_root(const uint32_t input)
22{
23
24 uint32_t op = input;
25 uint32_t res = 0;
26 uint32_t one = 1uL << 30;
27
28 while ( one > op ) {
29 one >>= 2;
30 }
31
32 while ( one != 0 ) {
33 if ( op >= res + one ) {
34 op = op - (res + one);
35 res = res + 2 * one;
36 }
37
38 res >>= 1;
39 one >>= 2;
40 }
41
42 return res;
43};
44
45} // namespace Math
46} // namespace SST
47
48#endif // SST_CORE_MATH_SQRT_H