source: libcfa/src/bitmanip.hfa@ 391aa0e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 391aa0e was da36d25, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

first complete draft of bitmanip, performance problems with polymorphic functions

  • Property mode set to 100644
File size: 7.3 KB
Line 
1// -*- Mode: C -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// bitmanip.hfa --
9//
10// Author : Peter A. Buhr
11// Created On : Sat Mar 14 18:12:27 2020
12// Last Modified By : Peter A. Buhr
13// Last Modified On : Mon Apr 13 22:37:03 2020
14// Update Count : 110
15//
16
17#pragma once
18
19// Reference: Bit Twiddling Hacks: http://graphics.stanford.edu/%7Eseander/bithacks.html#CountBitsSetNaive
20
21// Bits are numbered 1-N.
22
23#include <assert.h>
24
25#define __bitsizeof( n ) (sizeof(n) * __CHAR_BIT__)
26
27static inline {
28 // Count leading 0 bits.
29 unsigned int leading0s( unsigned char n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
30 unsigned int leading0s( unsigned short int n ) { return n != 0 ? __builtin_clz( n ) - (__bitsizeof(unsigned int) - __bitsizeof(n)) : __bitsizeof(n); }
31 unsigned int leading0s( unsigned int n ) { return n != 0 ? __builtin_clz( n ) : __bitsizeof(n); }
32 unsigned int leading0s( unsigned long int n ) { return n != 0 ? __builtin_clzl( n ) : __bitsizeof(n); }
33 unsigned int leading0s( unsigned long long int n ) { return n != 0 ? __builtin_clzll( n ) : __bitsizeof(n); }
34
35 // Count trailing 0 bits.
36 unsigned int trailing0s( unsigned char n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
37 unsigned int trailing0s( unsigned short int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
38 unsigned int trailing0s( unsigned int n ) { return n != 0 ? __builtin_ctz( n ) : __bitsizeof(n); }
39 unsigned int trailing0s( unsigned long int n ) { return n != 0 ? __builtin_ctzl( n ) : __bitsizeof(n); }
40 unsigned int trailing0s( unsigned long long int n ) { return n != 0 ? __builtin_ctzll( n ) : __bitsizeof(n); }
41
42 // Count all 1 bits.
43 unsigned int all1s( unsigned char n ) { return __builtin_popcount( n ); }
44 unsigned int all1s( unsigned short int n ) { return __builtin_popcount( n ); }
45 unsigned int all1s( unsigned int n ) { return __builtin_popcount( n ); }
46 unsigned int all1s( unsigned long int n ) { return __builtin_popcountl( n ); }
47 unsigned int all1s( unsigned long long int n ) { return __builtin_popcountll( n ); }
48
49 // Count all 0 bits.
50 unsigned int all0s( unsigned char n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
51 unsigned int all0s( unsigned short int n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
52 unsigned int all0s( unsigned int n ) { return __bitsizeof(n) - __builtin_popcount( n ); }
53 unsigned int all0s( unsigned long int n ) { return __bitsizeof(n) - __builtin_popcountl( n ); }
54 unsigned int all0s( unsigned long long int n ) { return __bitsizeof(n) - __builtin_popcountll( n ); }
55
56 // Find least significiant zero bit. (ffs)
57 unsigned int low0( unsigned char n ) { return __builtin_ffs( (typeof(n))~n ); }
58 unsigned int low0( unsigned short int n ) { return __builtin_ffs( (typeof(n))~n ); }
59 unsigned int low0( unsigned int n ) { return __builtin_ffs( ~n ); }
60 unsigned int low0( unsigned long int n ) { return __builtin_ffsl( ~n ); }
61 unsigned int low0( unsigned long long int n ) { return __builtin_ffsll( ~n ); }
62
63 // Find least significiant one bit.
64 unsigned int low1( unsigned int n ) { return __builtin_ffs( n ); }
65 unsigned int low1( unsigned long int n ) { return __builtin_ffsl( n ); }
66 unsigned int low1( unsigned long long int n ) { return __builtin_ffsll( n ); }
67
68 // Find most significiant zero bit.
69 unsigned int high0( unsigned char n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
70 unsigned int high0( unsigned short int n ) { return n == (typeof(n))-1 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( (typeof(n))~n ); }
71 unsigned int high0( unsigned int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clz( ~n ); }
72 unsigned int high0( unsigned long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzl( ~n ); }
73 unsigned int high0( unsigned long long int n ) { return n == -1 ? 0 : __bitsizeof(n) - __builtin_clzll( ~n ); }
74
75 // Find most significiant one bit.
76 unsigned int high1( unsigned char n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
77 unsigned int high1( unsigned short int n ) { return n == 0 ? 0 : __bitsizeof(unsigned int) - __builtin_clz( n ); }
78 unsigned int high1( unsigned int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clz( n ); }
79 unsigned int high1( unsigned long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzl( n ); }
80 unsigned int high1( unsigned long long int n ) { return n == 0 ? 0 : __bitsizeof(n) - __builtin_clzll( n ); }
81
82 // Check for power of 2, clears bits below n, rounding down to the next lower multiple of n.
83 bool is_pow2( unsigned char n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
84 bool is_pow2( unsigned short int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
85 bool is_pow2( unsigned int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
86 bool is_pow2( unsigned long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
87 bool is_pow2( unsigned long long int n ) { return n == 0 ? false : (n & (n - 1)) == 0; }
88
89 // Returns n aligned at the floor of align, clear bits above or equal to align, giving n % align.
90 // signed char floor2( signed char n, signed char align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
91 // unsigned char floor2( unsigned char n, unsigned char align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
92 // short int floor2( short int n, unsigned short int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
93 // unsigned short int floor2( unsigned short int n, unsigned short int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
94 // int floor2( int n, unsigned int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
95 // unsigned int floor2( unsigned int n, unsigned int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
96 // long int floor2( long int n, unsigned long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
97 // unsigned long int floor2( unsigned long int n, unsigned long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
98 // long long int floor2( long long int n, unsigned long long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
99 // unsigned long long int floor2( unsigned long long int n, unsigned long long int align ) { /*assert( is_pow2( align ) );*/ return n & -align; }
100
101 forall( otype T | { T ?&?( T, T ); T -?( T ); } )
102 T floor2( T n, T align ) { /* assert( is_pow2( align ) ); */ return n & -align; }
103
104 forall( otype T | { T ?/?( T, T ); T ?*?( T, T ); } )
105 T floor( T n, T align ) { return n / align * align; }
106
107 // Returns n aligned at the ceiling of align, negate, round down, negate is the same as round up.
108 forall( otype T | { T floor2( T, T ); T -?( T ); } )
109 T ceiling2( T n, T align ) { /* assert( is_pow2( align ) ); */ return -floor2( -n, align ); }
110
111 forall( otype T | { void ?{}( T &, one_t ); T ?+?( T, T ); T ?-?( T, T ); T ?/?( T, T ); } )
112 T ceiling( T n, T align ) { return (n + (align - (T){1})) / align; }
113} // distribution
114
115// Local Variables: //
116// tab-width: 4 //
117// End: //
Note: See TracBrowser for help on using the repository browser.