aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/defs.cc
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@aei.mpg.de>2004-12-25 18:31:00 +0000
committerErik Schnetter <schnetter@aei.mpg.de>2004-12-25 18:31:00 +0000
commit2cbafb5c128033ed2c9ab919ecfaa07fb6c0b403 (patch)
tree65b312381f1c020f4e809f3163f36a3d75fb6e4c /Carpet/CarpetLib/src/defs.cc
parent50dfcec56cd4b5fdc2b9bffb0dade1005342ca78 (diff)
CarpetLib: Update ipow function
darcs-hash:20041225183123-891bb-51654b84d53463c1d04fa05f901082a558136155.gz
Diffstat (limited to 'Carpet/CarpetLib/src/defs.cc')
-rw-r--r--Carpet/CarpetLib/src/defs.cc29
1 files changed, 17 insertions, 12 deletions
diff --git a/Carpet/CarpetLib/src/defs.cc b/Carpet/CarpetLib/src/defs.cc
index 330a62bca..257d158ac 100644
--- a/Carpet/CarpetLib/src/defs.cc
+++ b/Carpet/CarpetLib/src/defs.cc
@@ -16,20 +16,25 @@ using namespace std;
-template<class T>
-T ipow (T x, int y) {
- if (y<0) {
- y = -y;
- x = T(1)/x;
- }
- T res = T(1);
- for (;;) {
- if (y%2) res *= x;
- y /= 2;
- if (y==0) break;
+template <typename T>
+inline T ipow_helper (T x, unsigned int y)
+{
+ T z = y&1 ? x : 1;
+ while (y >>= 1)
+ {
x *= x;
+ if (y & 1) z *= x;
}
- return res;
+ return z;
+}
+
+template<class T>
+T ipow (T x, int y)
+{
+ if (y < 0)
+ return T(1) / ipow_helper(x, -y);
+ else
+ return ipow_helper(x, y);
}