public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/users/kubaneko/heads/histogram)] messed with logarithms
@ 2023-02-16 16:28 Ondrej Kubanek
  0 siblings, 0 replies; 3+ messages in thread
From: Ondrej Kubanek @ 2023-02-16 16:28 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:fdb7878232b77209916d5b5813af3b922edccfdb

commit fdb7878232b77209916d5b5813af3b922edccfdb
Author: kubaneko <kubanek0ondrej@gmail.com>
Date:   Tue Nov 1 17:48:30 2022 +0000

    messed with logarithms

Diff:
---
 libgcc/libgcov-profiler.c | 51 +++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/libgcc/libgcov-profiler.c b/libgcc/libgcov-profiler.c
index f150d75fee1..8088f3d647a 100644
--- a/libgcc/libgcov-profiler.c
+++ b/libgcc/libgcov-profiler.c
@@ -32,6 +32,28 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
  * otherwise we take its logarithm and increment corresponding counter
  */
 
+/* For convenience, define 0 -> word_size.  */
+static inline int
+clz_hwi (gcov_type x)
+{
+  if (x == 0)
+    return 64;
+# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
+  return __builtin_clzl (x);
+# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
+  return __builtin_clzll (x);
+# else
+  return __builtin_clz (x);
+# endif
+}
+
+static inline int
+floor_log2 (gcov_type x)
+{
+  return 63 - clz_hwi (x);
+}
+
+
 void
 __gcov_histogram_profiler (gcov_type *counters, gcov_type value)
 {
@@ -39,19 +61,10 @@ __gcov_histogram_profiler (gcov_type *counters, gcov_type value)
     counters[value]++;
   }else{
     gcc_assert(value>0);
-    int pow2 = 3;
-    while (1 << pow2 <= value || 1 << pow2 > 1 << (pow2 + 1)){
-      ++pow2;
-    }
-    // pow2 is first bigger power of 2
-    // we increment closer power of 2
-    if ((1<<pow2+1<<(pow2-1))>>1<value){
-      counters[6+(pow2-3)]++;
-    }
-    else{
-      counters[7+(pow2-3)]++;
-    }
+    int pow2=floor_log2(value);
+    counters[pow2+5]++;
   }
+  printf("\n %d %d \n", floor_log2(value), value);
 }
 
 #endif
@@ -71,18 +84,8 @@ __gcov_histogram_profiler_atomic (gcov_type *counters, gcov_type value)
     __atomic_fetch_add (&counters[value], 1, __ATOMIC_RELAXED);
   }else{
     gcc_assert(value>0);
-    int pow2 = 3;
-    while (1 << pow2 <= value || 1 << pow2 > 1 << (pow2 + 1)){
-      ++pow2;
-    }
-    // pow2 is first bigger power of 2
-    // we increment closer power of 2
-    if ((1<<pow2+1<<(pow2-1))>>1<value){
-      __atomic_fetch_add (&counters[6+(pow2-3)], 1, __ATOMIC_RELAXED);
-    }
-    else{
-      __atomic_fetch_add (&counters[7+(pow2-3)], 1, __ATOMIC_RELAXED);
-    }
+    int pow2=floor_log2(value);
+    __atomic_fetch_add (&counters[pow2+5], 1, __ATOMIC_RELAXED);
   }
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [gcc(refs/users/kubaneko/heads/histogram)] messed with logarithms
@ 2023-02-23 23:21 Ondrej Kubanek
  0 siblings, 0 replies; 3+ messages in thread
From: Ondrej Kubanek @ 2023-02-23 23:21 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:09ffc042ed1918f3b99b59428e7670406da0d042

commit 09ffc042ed1918f3b99b59428e7670406da0d042
Author: kubaneko <kubanek0ondrej@gmail.com>
Date:   Tue Nov 1 17:48:30 2022 +0000

    messed with logarithms

Diff:
---
 libgcc/libgcov-profiler.c | 51 +++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/libgcc/libgcov-profiler.c b/libgcc/libgcov-profiler.c
index 4b1a3b423e2..03ee8f6abc1 100644
--- a/libgcc/libgcov-profiler.c
+++ b/libgcc/libgcov-profiler.c
@@ -32,6 +32,28 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
  * otherwise we take its logarithm and increment corresponding counter
  */
 
+/* For convenience, define 0 -> word_size.  */
+static inline int
+clz_hwi (gcov_type x)
+{
+  if (x == 0)
+    return 64;
+# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
+  return __builtin_clzl (x);
+# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
+  return __builtin_clzll (x);
+# else
+  return __builtin_clz (x);
+# endif
+}
+
+static inline int
+floor_log2 (gcov_type x)
+{
+  return 63 - clz_hwi (x);
+}
+
+
 void
 __gcov_histogram_profiler (gcov_type *counters, gcov_type value)
 {
@@ -39,19 +61,10 @@ __gcov_histogram_profiler (gcov_type *counters, gcov_type value)
     counters[value]++;
   }else{
     gcc_assert(value>0);
-    int pow2 = 3;
-    while (1 << pow2 <= value || 1 << pow2 > 1 << (pow2 + 1)){
-      ++pow2;
-    }
-    // pow2 is first bigger power of 2
-    // we increment closer power of 2
-    if ((1<<pow2+1<<(pow2-1))>>1<value){
-      counters[6+(pow2-3)]++;
-    }
-    else{
-      counters[7+(pow2-3)]++;
-    }
+    int pow2=floor_log2(value);
+    counters[pow2+5]++;
   }
+  printf("\n %d %d \n", floor_log2(value), value);
 }
 
 #endif
@@ -71,18 +84,8 @@ __gcov_histogram_profiler_atomic (gcov_type *counters, gcov_type value)
     __atomic_fetch_add (&counters[value], 1, __ATOMIC_RELAXED);
   }else{
     gcc_assert(value>0);
-    int pow2 = 3;
-    while (1 << pow2 <= value || 1 << pow2 > 1 << (pow2 + 1)){
-      ++pow2;
-    }
-    // pow2 is first bigger power of 2
-    // we increment closer power of 2
-    if ((1<<pow2+1<<(pow2-1))>>1<value){
-      __atomic_fetch_add (&counters[6+(pow2-3)], 1, __ATOMIC_RELAXED);
-    }
-    else{
-      __atomic_fetch_add (&counters[7+(pow2-3)], 1, __ATOMIC_RELAXED);
-    }
+    int pow2=floor_log2(value);
+    __atomic_fetch_add (&counters[pow2+5], 1, __ATOMIC_RELAXED);
   }
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [gcc(refs/users/kubaneko/heads/histogram)] messed with logarithms
@ 2022-11-22 13:13 Ondrej Kubanek
  0 siblings, 0 replies; 3+ messages in thread
From: Ondrej Kubanek @ 2022-11-22 13:13 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:8627e4f45ad51e346755b8a0aa81a96111459399

commit 8627e4f45ad51e346755b8a0aa81a96111459399
Author: kubaneko <kubanek0ondrej@gmail.com>
Date:   Tue Nov 1 17:48:30 2022 +0000

    messed with logarithms

Diff:
---
 libgcc/libgcov-profiler.c | 51 +++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/libgcc/libgcov-profiler.c b/libgcc/libgcov-profiler.c
index f150d75fee1..8088f3d647a 100644
--- a/libgcc/libgcov-profiler.c
+++ b/libgcc/libgcov-profiler.c
@@ -32,6 +32,28 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
  * otherwise we take its logarithm and increment corresponding counter
  */
 
+/* For convenience, define 0 -> word_size.  */
+static inline int
+clz_hwi (gcov_type x)
+{
+  if (x == 0)
+    return 64;
+# if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
+  return __builtin_clzl (x);
+# elif HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONGLONG
+  return __builtin_clzll (x);
+# else
+  return __builtin_clz (x);
+# endif
+}
+
+static inline int
+floor_log2 (gcov_type x)
+{
+  return 63 - clz_hwi (x);
+}
+
+
 void
 __gcov_histogram_profiler (gcov_type *counters, gcov_type value)
 {
@@ -39,19 +61,10 @@ __gcov_histogram_profiler (gcov_type *counters, gcov_type value)
     counters[value]++;
   }else{
     gcc_assert(value>0);
-    int pow2 = 3;
-    while (1 << pow2 <= value || 1 << pow2 > 1 << (pow2 + 1)){
-      ++pow2;
-    }
-    // pow2 is first bigger power of 2
-    // we increment closer power of 2
-    if ((1<<pow2+1<<(pow2-1))>>1<value){
-      counters[6+(pow2-3)]++;
-    }
-    else{
-      counters[7+(pow2-3)]++;
-    }
+    int pow2=floor_log2(value);
+    counters[pow2+5]++;
   }
+  printf("\n %d %d \n", floor_log2(value), value);
 }
 
 #endif
@@ -71,18 +84,8 @@ __gcov_histogram_profiler_atomic (gcov_type *counters, gcov_type value)
     __atomic_fetch_add (&counters[value], 1, __ATOMIC_RELAXED);
   }else{
     gcc_assert(value>0);
-    int pow2 = 3;
-    while (1 << pow2 <= value || 1 << pow2 > 1 << (pow2 + 1)){
-      ++pow2;
-    }
-    // pow2 is first bigger power of 2
-    // we increment closer power of 2
-    if ((1<<pow2+1<<(pow2-1))>>1<value){
-      __atomic_fetch_add (&counters[6+(pow2-3)], 1, __ATOMIC_RELAXED);
-    }
-    else{
-      __atomic_fetch_add (&counters[7+(pow2-3)], 1, __ATOMIC_RELAXED);
-    }
+    int pow2=floor_log2(value);
+    __atomic_fetch_add (&counters[pow2+5], 1, __ATOMIC_RELAXED);
   }
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-23 23:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-16 16:28 [gcc(refs/users/kubaneko/heads/histogram)] messed with logarithms Ondrej Kubanek
  -- strict thread matches above, loose matches on Subject: below --
2023-02-23 23:21 Ondrej Kubanek
2022-11-22 13:13 Ondrej Kubanek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).