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