Skip to contentSkip to conversation

x86/intel_pstate: add some calculation related support

PATCH In review Wei Wang 2 participants 2 messages May 13, 2015 – May 26, 2015

lore
Share & tools
lore

Wei Wang

author
#0 lore raw

PATCH v2 2/9 x86/intel_pstate: add some calculation related support

The added calculation related functions will be used in the intel_pstate.c.

---
 xen/include/asm-x86/div64.h | 68 +++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/kernel.h    | 30 ++++++++++++++++++++
 2 files changed, 98 insertions(+)
xen/include/asm-x86/div64.h +68 -0
index dd49f64..10f1009 100644
@@ -11,4 +11,72 @@
     __rem;                                      \
 })
 
+static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
+{
+    *remainder = do_div(dividend, divisor);
+    return dividend;
+}
+
+static inline u64 div_u64(u64 dividend, u32 divisor)
+{
+    u32 remainder;
+    return div_u64_rem(dividend, divisor, &remainder);
+}
+
+/*
+ * div64_u64 - unsigned 64bit divide with 64bit divisor
+ * @dividend:    64bit dividend
+ * @divisor:    64bit divisor
+ *
+ * This implementation is a modified version of the algorithm proposed
+ * by the book 'Hacker's Delight'.  The original source and full proof
+ * can be found here and is available for use without restriction.
+ *
+ * 'http://www.hackersdelight.org/HDcode/newCode/divDouble.c.txt'
+ */
+static inline u64 div64_u64(u64 dividend, u64 divisor) {
+    u32 high = divisor >> 32;
+    u64 quot;
+
+    if (high == 0) {
+        quot = div_u64(dividend, divisor);
+    } else {
+        int n = 1 + fls(high);
+        quot = div_u64(dividend >> n, divisor >> n);
+
+        if (quot != 0)
+            quot--;
+        if ((dividend - quot * divisor) >= divisor)
+            quot++;
+    }
+    return quot;
+}
+
+static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) {
+    u64 quotient;
+
+    if (dividend < 0) {
+        quotient = div_u64_rem(-dividend, abs(divisor),
+                        (u32 *)remainder);
+        *remainder = -*remainder;
+        if (divisor > 0)
+            quotient = -quotient;
+    } else {
+        quotient = div_u64_rem(dividend, abs(divisor),
+                        (u32 *)remainder);
+        if (divisor < 0)
+            quotient = -quotient;
+    }
+    return quotient;
+}
+
+/*
+ * div_s64 - signed 64bit divide with 32bit divisor
+ */
+static inline s64 div_s64(s64 dividend, s32 divisor)
+{
+    s32 remainder;
+    return div_s64_rem(dividend, divisor, &remainder);
+}
+
 #endif
xen/include/xen/kernel.h +30 -0
index 548b64d..cb0ce03 100644
@@ -42,6 +42,36 @@
 #define MIN(x,y) ((x) < (y) ? (x) : (y))
 #define MAX(x,y) ((x) > (y) ? (x) : (y))
 
+/*
+ * clamp_t - return a value clamped to a given range using a given type
+ * @type: the type of variable to use
+ * @val: current value
+ * @lo: minimum allowable value
+ * @hi: maximum allowable value
+ *
+ * This macro does no typechecking and uses temporary variables of type
+ * 'type' to make all the comparisons.
+ */
+#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
+
+/*
+ * abs() handles unsigned and signed longs, ints, shorts and chars. For all
+ * input types abs() returns a signed long.
+ * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
+ * for those.
+ */
+#define abs(x) ({                      \
+    long ret;                          \
+    if (sizeof(x) == sizeof(long)) {   \
+        long __x = (x);                \
+        ret = (__x < 0) ? -__x : __x;  \
+    } else {                           \
+        int __x = (x);                 \
+        ret = (__x < 0) ? -__x : __x;  \
+    }                                  \
+    ret;                               \
+})
+
 /**
  * container_of - cast a member of a structure out to the containing structure
  *
Signature · 1 line
1.9.1
#1 lore raw
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 13.05.16 at 09:49, <[email protected]> wrote:

The added calculation related functions will be used in the intel_pstate.c.

If these are taken from Linux, please say so here (including the version they got cloned from), as in that case close review can be considered unnecessary. That said - do you really need all of them, considering that you don't need to support a 32-bit environment?

xen/include/asm-x86/div64.h

Quoted 2 lines
xen/include/asm-x86/div64.h +0 -0

All changes to this file that are indeed needed should follow Xen coding style.

Quoted 7 lines
xen/include/asm-x86/div64.h +7 -0
+/*
+ * abs() handles unsigned and signed longs, ints, shorts and chars. For all
+ * input types abs() returns a signed long.
+ * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
+ * for those.
+ */
+#define abs(x) ({                      \

xen/include/asm-x86/div64.h

Quoted 10 lines
xen/include/asm-x86/div64.h +10 -0
+    long ret;                          \
+    if (sizeof(x) == sizeof(long)) {   \
+        long __x = (x);                \
+        ret = (__x < 0) ? -__x : __x;  \
+    } else {                           \
+        int __x = (x);                 \
+        ret = (__x < 0) ? -__x : __x;  \
+    }                                  \
+    ret;                               \
+})

Please use the already present and more generic (less the unsigned types support, which I highly question to be necessary here) ABS() instead.

Jan