aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@gmail.com>2013-05-20 09:28:28 -0500
committerErik Schnetter <schnetter@gmail.com>2013-05-20 09:28:28 -0500
commit94934bc46af8fe2f049f2171ca3fb90dcdc4cc5e (patch)
treefd263ddb115c0eadad312dbbdc8cd4547eaf250d /Carpet/CarpetLib
parentc515a4b4786c50b21ee4b943fe693ff3c2903652 (diff)
CarpetLib: Add routines for integer division that always round down
Standard integer division truncates, which can lead to problems if an intermediat result is negative.
Diffstat (limited to 'Carpet/CarpetLib')
-rw-r--r--Carpet/CarpetLib/src/defs.hh29
1 files changed, 23 insertions, 6 deletions
diff --git a/Carpet/CarpetLib/src/defs.hh b/Carpet/CarpetLib/src/defs.hh
index 2fbe4e836..e2c6325af 100644
--- a/Carpet/CarpetLib/src/defs.hh
+++ b/Carpet/CarpetLib/src/defs.hh
@@ -104,20 +104,37 @@ enum centering { error_centered, vertex_centered, cell_centered };
+// Divide, rounding to minus infinity
template<typename T>
-inline T div_down(T const x, T const align)
+inline T idiv(T const x, T const y)
+{
+ // round down manually if the result is negative
+ return (x^y) >= T(0) ? x/y : (x-y+1)/y;
+}
+// Modulo, rounding to minus infinity
+template<typename T>
+inline T imod(T const x, T const y)
+{
+ // return x - idiv(x,y)*y;
+ return (x^y) >= T(0) ? x%y : (x-y+1)%y + y-1;
+}
+
+
+
+template<typename T>
+inline T div_down(T const x, T const y)
{
assert(x >= 0);
- assert(align > 0);
- return x / align;
+ assert(y > 0);
+ return x / y;
}
template<typename T>
-inline T div_up(T const x, T const align)
+inline T div_up(T const x, T const y)
{
assert(x >= 0);
- assert(align > 0);
- return (x + align - 1) / align;
+ assert(y > 0);
+ return (x + y - 1) / y;
}
template<typename T>