ROUNDDOWN can handle uint64_t on 32 bit (XCC)
[akaros.git] / kern / include / ros / common.h
index 6670875..46d2fa9 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef ROS_COMMON_H
 #define ROS_COMMON_H
 
+#ifndef __ASSEMBLER__
 #ifndef __IVY__
 #include <ros/noivy.h>
 #endif
@@ -11,8 +12,8 @@
 #include <stdbool.h>
 
 typedef uintptr_t physaddr_t;
-typedef ssize_t intreg_t;
-typedef size_t uintreg_t;
+typedef long intreg_t;
+typedef unsigned long uintreg_t;
 
 #ifndef NULL
 #define NULL ((void*) 0)
@@ -47,31 +48,35 @@ typedef size_t uintreg_t;
 })
 #endif
 
-// Rounding operations (efficient when n is a power of 2)
-// Round down to the nearest multiple of n
-#define ROUNDDOWN(a, n)                                                \
-({                                                             \
-       uintptr_t __a = (uintptr_t) (a);                                \
-       (typeof(a)) (__a - __a % (n));                          \
-})
-// Round up to the nearest multiple of n
-#define ROUNDUP(a, n)                                          \
-({                                                             \
-       uintptr_t __n = (uintptr_t) (n);                                \
-       (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));        \
+/* Rounding operations (efficient when n is a power of 2)
+ * Round down to the nearest multiple of n.
+ * The compiler should compile out the branch.  This is needed for 32 bit, so
+ * that we can round down uint64_t, without chopping off the top 32 bits. */
+#define ROUNDDOWN(a, n)                                                        \
+({                                                                             \
+       typeof(a) __b;                                                             \
+       if (sizeof(a) == 8) {                                                      \
+               uint64_t __a = (uint64_t) (a);                                         \
+               __b = (typeof(a)) (__a - __a % (n));                                   \
+       } else {                                                                   \
+               uintptr_t __a = (uintptr_t) (a);                                       \
+               __b = (typeof(a)) (__a - __a % (n));                                   \
+       }                                                                          \
+       __b;                                                                       \
 })
 
-// Round down to the nearest multiple of n
-#define PTRROUNDDOWN(a, n)                                             \
-({                                                             \
-       char * __a = (char *) (a);                              \
-       (typeof(a)) (__a - (uintptr_t)__a % (n));                               \
-})
-// Round pointer up to the nearest multiple of n
-#define PTRROUNDUP(a, n)                                               \
-({                                                             \
-       uintptr_t __n = (uintptr_t) (n);                                \
-       (typeof(a)) (PTRROUNDDOWN((char *) (a) + __n - 1, __n));        \
+/* Round up to the nearest multiple of n */
+#define ROUNDUP(a, n)                                                          \
+({                                                                             \
+       typeof(a) __b;                                                             \
+       if (sizeof(a) == 8) {                                                      \
+               uint64_t __n = (uint64_t) (n);                                         \
+               __b = (typeof(a)) (ROUNDDOWN((uint64_t) (a) + __n - 1, __n));          \
+       } else {                                                                   \
+               uintptr_t __n = (uintptr_t) (n);                                       \
+               __b = (typeof(a)) (ROUNDDOWN((uintptr_t) (a) + __n - 1, __n));         \
+       }                                                                          \
+       __b;                                                                       \
 })
 
 // Return the integer logarithm of the value provided rounded down
@@ -115,6 +120,10 @@ static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
        (type*)((char*)ptr - offsetof(type, member));                             \
 })
 
+/* Force the reading exactly once of x.  You may still need mbs().  See
+ * http://lwn.net/Articles/508991/ for more info. */
+#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
+
 // Ivy currently can only handle 63 bits (OCaml thing), so use this to make
 // a uint64_t programatically
 #define UINT64(upper, lower) ( (((uint64_t)(upper)) << 32) | (lower) )
@@ -162,4 +171,6 @@ static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
        initialized = TRUE;                                                        \
 }
 
+#endif /* __ASSEMBLER__ */
+
 #endif /* ROS_COMMON_H */