Skip to contentSkip to conversation

drm/amd: Use portable do_div helper

PATCH In review Felix Kuehling 2 participants 3 messages Sep 19, 2017 – Sep 20, 2017 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/amd: Use portable do_div helper

---
 drivers/gpu/drm/amd/lib/chash.c | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/lib/chash.c +23 -7
index 1bc4287..e07e6f3 100644
@@ -27,6 +27,7 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/sched/clock.h>
+#include <asm/div64.h>
 #include <linux/chash.h>
 
 /**
@@ -70,9 +71,21 @@ EXPORT_SYMBOL(chash_table_free);
 #ifdef CONFIG_CHASH_STATS
 
 #define DIV_FRAC(nom, denom, quot, frac, frac_digits) do {		\
-		(quot) = (nom) / (denom);				\
-		(frac) = ((nom) % (denom) * (frac_digits) +		\
-			  (denom) / 2) / (denom);			\
+		u64 __nom = (nom);					\
+		u64 __denom = (denom);					\
+		u64 __quot, __frac;					\
+		u32 __rem;						\
+									\
+		while (__denom >> 32) {					\
+			__nom   >>= 1;					\
+			__denom >>= 1;					\
+		}							\
+		__quot = __nom;						\
+		__rem  = do_div(__quot, __denom);			\
+		__frac = __rem * (frac_digits) + (__denom >> 1);	\
+		do_div(__frac, __denom);				\
+		(quot) = __quot;					\
+		(frac) = __frac;					\
 	} while (0)
 
 void __chash_table_dump_stats(struct __chash_table *table)
@@ -562,7 +575,7 @@ module_param_named(test_iters, chash_test_iters, ulong, 0444);
 static int __init chash_init(void)
 {
 	int ret;
-	u64 ts1_ns, ts_delta_us;
+	u64 ts1_ns;
 
 	/* Skip self test on user errors */
 	if (chash_test_bits < 4 || chash_test_bits > 20) {
@@ -603,10 +616,13 @@ static int __init chash_init(void)
 			      chash_test_minfill, chash_test_maxfill,
 			      chash_test_iters);
 	if (!ret) {
-		ts_delta_us = (local_clock() - ts1_ns) / 1000;
+		u64 ts_delta_us = local_clock() - ts1_ns;
+		u64 iters_per_second = (u64)chash_test_iters * 1000000;
+
+		do_div(ts_delta_us, 1000);
+		do_div(iters_per_second, ts_delta_us);
 		pr_info("chash: self test took %llu us, %llu iterations/s\n",
-			ts_delta_us,
-			(u64)chash_test_iters * 1000000 / ts_delta_us);
+			ts_delta_us, iters_per_second);
 	} else {
 		pr_err("chash: self test failed: %d\n", ret);
 	}
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1505852597-1774-1-git-send-email-Felix.Kuehling-5C7GfCeVMHo@public.gmane.org

#2 lore raw

PATCH 2/2 drm/amd: Avoid using x86-specific _BITOPS_LONG_SHIFT

---
 drivers/gpu/drm/amd/include/linux/chash.h | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/include/linux/chash.h +15 -7
index c89b92b..6dc1599 100644
@@ -27,7 +27,15 @@
 #include <linux/types.h>
 #include <linux/hash.h>
 #include <linux/bug.h>
-#include <linux/bitops.h>
+#include <asm/bitsperlong.h>
+
+#if BITS_PER_LONG == 32
+# define _CHASH_LONG_SHIFT 5
+#elif BITS_PER_LONG == 64
+# define _CHASH_LONG_SHIFT 6
+#else
+# error "Unexpected BITS_PER_LONG"
+#endif
 
 struct __chash_table {
 	u8 bits;
@@ -283,31 +291,31 @@ struct chash_iter {
 static inline bool chash_iter_is_valid(const struct chash_iter iter)
 {
 	BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
-	return !!(iter.table->valid_bitmap[iter.slot >> _BITOPS_LONG_SHIFT] &
+	return !!(iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &
 		  iter.mask);
 }
 static inline bool chash_iter_is_empty(const struct chash_iter iter)
 {
 	BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
-	return !(iter.table->occup_bitmap[iter.slot >> _BITOPS_LONG_SHIFT] &
+	return !(iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &
 		 iter.mask);
 }
 
 static inline void chash_iter_set_valid(const struct chash_iter iter)
 {
 	BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
-	iter.table->valid_bitmap[iter.slot >> _BITOPS_LONG_SHIFT] |= iter.mask;
-	iter.table->occup_bitmap[iter.slot >> _BITOPS_LONG_SHIFT] |= iter.mask;
+	iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] |= iter.mask;
+	iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] |= iter.mask;
 }
 static inline void chash_iter_set_invalid(const struct chash_iter iter)
 {
 	BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
-	iter.table->valid_bitmap[iter.slot >> _BITOPS_LONG_SHIFT] &= ~iter.mask;
+	iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &= ~iter.mask;
 }
 static inline void chash_iter_set_empty(const struct chash_iter iter)
 {
 	BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
-	iter.table->occup_bitmap[iter.slot >> _BITOPS_LONG_SHIFT] &= ~iter.mask;
+	iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &= ~iter.mask;
 }
 
 static inline u32 chash_iter_key32(const struct chash_iter iter)
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#3 lore raw
In reply to a message not in the archive

Am 19.09.2017 um 22:23 schrieb Felix Kuehling:

Quoted 1 line
Quoted 57 lines
---
  drivers/gpu/drm/amd/lib/chash.c | 30 +++++++++++++++++++++++-------
  1 file changed, 23 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/lib/chash.c +19 -5
index 1bc4287..e07e6f3 100644
@@ -27,6 +27,7 @@
  #include <linux/slab.h>
  #include <linux/module.h>
  #include <linux/sched/clock.h>
+#include <asm/div64.h>
  #include <linux/chash.h>
 
  /**
@@ -70,9 +71,21 @@ EXPORT_SYMBOL(chash_table_free);
  #ifdef CONFIG_CHASH_STATS
 
  #define DIV_FRAC(nom, denom, quot, frac, frac_digits) do {		\
-		(quot) = (nom) / (denom);				\
-		(frac) = ((nom) % (denom) * (frac_digits) +		\
-			  (denom) / 2) / (denom);			\
+		u64 __nom = (nom);					\
+		u64 __denom = (denom);					\
+		u64 __quot, __frac;					\
+		u32 __rem;						\
+									\
+		while (__denom >> 32) {					\
+			__nom   >>= 1;					\
+			__denom >>= 1;					\
+		}							\
+		__quot = __nom;						\
+		__rem  = do_div(__quot, __denom);			\
+		__frac = __rem * (frac_digits) + (__denom >> 1);	\
+		do_div(__frac, __denom);				\
+		(quot) = __quot;					\
+		(frac) = __frac;					\
  	} while (0)
 
  void __chash_table_dump_stats(struct __chash_table *table)
@@ -562,7 +575,7 @@ module_param_named(test_iters, chash_test_iters, ulong, 0444);
  static int __init chash_init(void)
  {
  	int ret;
-	u64 ts1_ns, ts_delta_us;
+	u64 ts1_ns;
 
  	/* Skip self test on user errors */
  	if (chash_test_bits < 4 || chash_test_bits > 20) {
@@ -603,10 +616,13 @@ static int __init chash_init(void)
  			      chash_test_minfill, chash_test_maxfill,
  			      chash_test_iters);
  	if (!ret) {
-		ts_delta_us = (local_clock() - ts1_ns) / 1000;
+		u64 ts_delta_us = local_clock() - ts1_ns;
+		u64 iters_per_second = (u64)chash_test_iters * 1000000;

drivers/gpu/drm/amd/lib/chash.c static int __init chash_init(void) · line 628

Quoted 10 lines
drivers/gpu/drm/amd/lib/chash.c +4 -2
+
+		do_div(ts_delta_us, 1000);
+		do_div(iters_per_second, ts_delta_us);
  		pr_info("chash: self test took %llu us, %llu iterations/s\n",
-			ts_delta_us,
-			(u64)chash_test_iters * 1000000 / ts_delta_us);
+			ts_delta_us, iters_per_second);
  	} else {
  		pr_err("chash: self test failed: %d\n", ret);
  	}

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx