Debian Codegraph @

gcc/testsuite/g++.dg/modules/cexpr-2_a.C

raw
1 // { dg-additional-options "-fmodules-ts" }
2 export module sqrt;
3 // { dg-module-cmi "sqrt" }
4
5 export constexpr unsigned sqrt (unsigned X, unsigned x = 1)
6 {
7 // Newton-Raphson, not binary restoring
8 // x <= x - f(x)/f'(x)
9 // f(x) = x^2 - X
10 // f'(x) = 2x
11 // x <= x - (x^2 - X) / 2x
12 // x <= x - x/2 + X/2x
13 // x <= x/2 + X/2x
14 // x <= 1/2(x + X/x)
15 unsigned nx = (x + X/x) / 2;
16 if (nx != x)
17 nx = sqrt (X, nx);
18 return nx;
19 }