Loading...
Searching...
No Matches
Operators.tpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25// Operator "sign" ------------------------------------------------------------------- //
37template <class T>
39 const T & val
40 ){
41 return (T(0) < val) - (val < T(0)) ;
42};
43
54template <class T>
55T uipow(const T & base, unsigned int exponent)
56{
57 switch (exponent) {
58
59 case 0:
60 {
61 return 1;
62 }
63
64 case 1:
65 {
66 return base;
67 }
68
69 case 2:
70 {
71 return base * base;
72 }
73
74 case 3:
75 {
76 return base * base * base;
77 }
78
79 default:
80 {
81 // The integer power is evluated using the exponentiating by squaring
82 // algorithm. For an explanation of the algorith see the following
83 // link:
84 //
85 // https://en.wikipedia.org/wiki/Exponentiation_by_squaring
86 T result = static_cast<T>(1);
87 T base_work = base;
88 while (exponent) {
89 if (exponent % 2) {
90 result *= base_work;
91 }
92
93 exponent /= 2;
94 base_work *= base_work;
95 }
96
97 return result;
98 }
99
100 }
101}
T sign(const T &val)
Definition Operators.tpp:38
T uipow(const T &base, unsigned int exponent)
Definition Operators.tpp:55
--- layout: doxygen_footer ---