Skip to content

Commit a32f5d6

Browse files
committed
Style tweaks
1 parent abdad72 commit a32f5d6

File tree

9 files changed

+90
-56
lines changed

9 files changed

+90
-56
lines changed

Diff for: binary.c

+20-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@ unsigned clz(uint32_t x)
55
{
66
if (x == 0) return 32;
77
int n = 0;
8-
if (x <= 0x0000FFFF) { n += 16; x <<= 16; }
9-
if (x <= 0x00FFFFFF) { n += 8; x <<= 8; }
10-
if (x <= 0x0FFFFFFF) { n += 4; x <<= 4; }
11-
if (x <= 0x3FFFFFFF) { n += 2; x <<= 2; }
12-
if (x <= 0x7FFFFFFF) { n += 1; x <<= 1; }
8+
if (x <= 0x0000FFFF) {
9+
n += 16;
10+
x <<= 16;
11+
}
12+
if (x <= 0x00FFFFFF) {
13+
n += 8;
14+
x <<= 8;
15+
}
16+
if (x <= 0x0FFFFFFF) {
17+
n += 4;
18+
x <<= 4;
19+
}
20+
if (x <= 0x3FFFFFFF) {
21+
n += 2;
22+
x <<= 2;
23+
}
24+
if (x <= 0x7FFFFFFF) {
25+
n += 1;
26+
x <<= 1;
27+
}
1328
return n;
1429
}

Diff for: byte.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ unsigned clz(uint32_t x)
55
{
66
if (x == 0) return 32;
77
int n = 1;
8-
if ((x >> 16) == 0) { n += 16; x <<= 16; }
9-
if ((x >> 24) == 0) { n += 8; x <<= 8; }
10-
if ((x >> 28) == 0) { n += 4; x <<= 4; }
11-
if ((x >> 30) == 0) { n += 2; x <<= 2; }
8+
if ((x >> 16) == 0) {
9+
n += 16;
10+
x <<= 16;
11+
}
12+
if ((x >> 24) == 0) {
13+
n += 8;
14+
x <<= 8;
15+
}
16+
if ((x >> 28) == 0) {
17+
n += 4;
18+
x <<= 4;
19+
}
20+
if ((x >> 30) == 0) {
21+
n += 2;
22+
x <<= 2;
23+
}
1224
n = n - (x >> 31);
1325
return n;
1426
}

Diff for: calculate.c

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
int main()
55
{
66
FILE *fp = fopen("iteration.txt","r");
7-
// FILE *output = fopen("output.txt","w");
87
if (!fp) {
98
printf("ERROR opening input file iteration.txt\n");
109
exit(0);

Diff for: clz.h

+22-11
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,46 @@
33

44
#include <stdint.h>
55

6-
#if defined(recursive)
7-
#define clz(x) clz2(x,0)
8-
static const int mask[]={0,8,12,14};
9-
static const int magic[]={2,1,0,0};
6+
#if defined(recursive)
7+
#define clz(x) clz2(x, 0)
8+
static const int mask[] = {0, 8, 12, 14};
9+
static const int magic[] = {2, 1, 0, 0};
1010

1111
//static inline __attribute((always_inline))
1212
unsigned clz2(uint32_t x,int c)
1313
{
14+
// *INDENT-OFF*
1415
if (!x && !c) return 32;
1516

1617
uint32_t upper = (x >> (16 >> c));
1718
uint32_t lower = (x & (0xFFFF>>mask[c]));
1819
if (c == 3) return upper ? magic[upper] : 2 + magic[lower];
1920
return upper ? clz2(upper, c + 1) : (16 >> (c)) + clz2(lower, c + 1);
21+
// *INDENT-ON*
2022
}
2123

22-
#elif defined(iteration)
24+
#elif defined(iteration)
2325

2426
static inline __attribute((always_inline))
2527
unsigned clz(uint32_t x)
2628
{
29+
// *INDENT-OFF*
2730
int n = 32, c = 16;
2831
do {
2932
uint32_t y = x >> c;
3033
if (y) { n -= c; x = y; }
3134
c >>= 1;
3235
} while (c);
3336
return (n - x);
37+
// *INDENT-ON*
3438
}
3539

36-
#elif defined(byte)
40+
#elif defined(byte)
3741

3842
static inline __attribute((always_inline))
3943
unsigned clz(uint32_t x)
4044
{
45+
// *INDENT-OFF*
4146
if (x == 0) return 32;
4247
int n = 1;
4348
if ((x >> 16) == 0) { n += 16; x <<= 16; }
@@ -46,13 +51,15 @@ unsigned clz(uint32_t x)
4651
if ((x >> 30) == 0) { n += 2; x <<= 2; }
4752
n = n - (x >> 31);
4853
return n;
54+
// *INDENT-ON*
4955
}
5056

51-
#elif defined(binary)
57+
#elif defined(binary)
5258

5359
static inline __attribute((always_inline))
5460
unsigned clz(uint32_t x)
5561
{
62+
// *INDENT-OFF*
5663
if (x == 0) return 32;
5764
int n = 0;
5865
if (x <= 0x0000FFFF) { n += 16; x <<= 16; }
@@ -61,15 +68,16 @@ unsigned clz(uint32_t x)
6168
if (x <= 0x3FFFFFFF) { n += 2; x <<= 2; }
6269
if (x <= 0x7FFFFFFF) { n += 1; x <<= 1; }
6370
return n;
71+
// *INDENT-ON*
6472
}
6573

66-
#elif defined(harley)
74+
#elif defined(harley)
6775

6876
static inline __attribute((always_inline))
6977
unsigned clz(uint32_t x)
7078
{
71-
// CTZ table
7279
#ifdef CTZ
80+
// *INDENT-OFF*
7381
static uint8_t const Table[] = {
7482
0xFF, 0, 0xFF, 15, 0xFF, 1, 28, 0xFF,
7583
16, 0xFF, 0xFF, 0xFF, 2, 21, 29, 0xFF,
@@ -80,23 +88,26 @@ unsigned clz(uint32_t x)
8088
26, 0xFF, 0xFF, 8, 0xFF, 4, 0xFF, 25,
8189
0xFF, 7, 24, 0xFF, 23, 0xFF, 31, 0xFF,
8290
};
91+
// *INDENT-ON*
8392

84-
// CLZ table
8593
#else
94+
// *INDENT-OFF*
8695
static uint8_t const Table[] = {
8796
32, 31, 0, 16, 0, 30, 3, 0, 15, 0, 0, 0, 29, 10, 2, 0,
8897
0, 0, 12, 14, 21, 0, 19, 0, 0, 28, 0, 25, 0, 9, 1, 0,
8998
17, 0, 4, 0, 0, 0, 11, 0, 13, 22, 20, 0, 26, 0, 0, 18,
9099
5, 0, 0, 23, 0, 27, 0, 6, 0, 24, 7, 0, 8, 0, 0, 0
91100
};
101+
// *INDENT-ON*
92102
#endif
103+
93104
/* Propagate leftmost 1-bit to the right */
94105
x = x | (x >> 1);
95106
x = x | (x >> 2);
96107
x = x | (x >> 4);
97108
x = x | (x >> 8);
98109
x = x | (x >> 16);
99-
110+
100111
/* x = x * 0x6EB14F9 */
101112
x = (x << 3) - x; /* Multiply by 7. */
102113
x = (x << 8) - x; /* Multiply by 255. */

Diff for: clz2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef CLZ2_H
22
#define CLZ2_H
3-
#define clz(x) clz2(x,0)
3+
#define clz(x) clz2(x, 0)
44
#include <stdint.h>
55

66
static inline __attribute((always_inline))

Diff for: harley.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
static inline __attribute((always_inline))
44
unsigned clz(uint32_t x)
55
{
6-
// CTZ table
76
#ifdef CTZ
87
static uint8_t const Table[] = {
98
0xFF, 0, 0xFF, 15, 0xFF, 1, 28, 0xFF,
@@ -16,9 +15,8 @@ unsigned clz(uint32_t x)
1615
0xFF, 7, 24, 0xFF, 23, 0xFF, 31, 0xFF,
1716
};
1817

19-
// CLZ table
2018
#else
21-
static uint8_t const Table[] ={
19+
static uint8_t const Table[] = {
2220
32,31, 0,16, 0,30, 3, 0,15, 0, 0, 0,29,10, 2, 0,
2321
0, 0,12,14,21, 0,19, 0, 0,28, 0,25, 0, 9, 1, 0,
2422
17, 0, 4, 0, 0, 0,11, 0,13,22,20, 0,26, 0, 0,18,
@@ -32,7 +30,7 @@ unsigned clz(uint32_t x)
3230
x = x | (x >> 4);
3331
x = x | (x >> 8);
3432
x = x | (x >> 16);
35-
33+
3634
/* x = x * 0x6EB14F9 */
3735
x = (x << 3) - x; /* Multiply by 7. */
3836
x = (x << 8) - x; /* Multiply by 255. */

Diff for: iteration.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ unsigned clz(uint32_t x)
66
int n = 32, c = 16;
77
do {
88
uint32_t y = x >> c;
9-
if (y) { n -= c; x = y; }
9+
if (y) {
10+
n -= c;
11+
x = y;
12+
}
1013
c >>= 1;
1114
} while (c);
1215
return (n - x);

Diff for: main.c

+21-25
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
#include <assert.h>
77
#include <omp.h>
88

9-
#if defined(recursive)
10-
#define clz(x) clz2(x,0)
11-
#endif
12-
139
#include "clz.h"
1410

1511
static inline __attribute__((always_inline))
@@ -46,7 +42,7 @@ static inline __attribute__((unused))
4642
double diff_in_second(struct timespec t1, struct timespec t2)
4743
{
4844
struct timespec diff;
49-
if (t2.tv_nsec-t1.tv_nsec < 0) {
45+
if (t2.tv_nsec - t1.tv_nsec < 0) {
5046
diff.tv_sec = t2.tv_sec - t1.tv_sec - 1;
5147
diff.tv_nsec = t2.tv_nsec - t1.tv_nsec + 1000000000;
5248
} else {
@@ -63,27 +59,27 @@ int main(int argc, char *argv[])
6359
unsigned timec_high1, timec_low1, timec_high2, timec_low2;
6460

6561
#if defined(correct)
66-
62+
// *INDENT-OFF*
6763
for (int try = 0; try < 20; try++) {
68-
timec = 0;
69-
get_cycles(&timec_high1, &timec_low1);
70-
printf("%u:%d \n", 0, clz(0));
71-
assert((sizeof(uint32_t) * 8) == clz(0));
72-
for (uint32_t i = 0; i < 31; i++) {
73-
printf("%u:%d \n", 1 << i, clz(1 << i));
74-
for (uint32_t j = (1 << i); j < (1 << (i + 1)); j++) {
75-
assert( __builtin_clz (j) == clz(j));
76-
}
77-
}
78-
printf("%u:%d \n", 1u << 31, clz(1u << 31));
79-
for (uint32_t j = (1u << 31); j < UINT32_MAX; j++)
80-
assert(__builtin_clz(j) == clz(j));
81-
assert(__builtin_clz(UINT32_MAX) == clz(UINT32_MAX));
82-
get_cycles_end(&timec_high2, &timec_low2);
83-
timec = diff_in_cycles(timec_high1, timec_low1, timec_high2, timec_low2);
84-
printf("executiom time : %lu cycles\n", timec);
85-
}
86-
64+
timec = 0;
65+
get_cycles(&timec_high1, &timec_low1);
66+
printf("%u:%d \n", 0, clz(0));
67+
assert((sizeof(uint32_t) * 8) == clz(0));
68+
for (uint32_t i = 0; i < 31; i++) {
69+
printf("%u:%d \n", 1 << i, clz(1 << i));
70+
for (uint32_t j = (1 << i); j < (1 << (i + 1)); j++) {
71+
assert( __builtin_clz (j) == clz(j));
72+
}
73+
}
74+
printf("%u:%d \n", 1u << 31, clz(1u << 31));
75+
for (uint32_t j = (1u << 31); j < UINT32_MAX; j++)
76+
assert(__builtin_clz(j) == clz(j));
77+
assert(__builtin_clz(UINT32_MAX) == clz(UINT32_MAX));
78+
get_cycles_end(&timec_high2, &timec_low2);
79+
timec = diff_in_cycles(timec_high1, timec_low1, timec_high2, timec_low2);
80+
printf("executiom time : %lu cycles\n", timec);
81+
}
82+
// *INDENT-ON*
8783
#else
8884
assert(argv[1] && argv[2] && "insert argument");
8985
unsigned int min = atoi(argv[1]);

Diff for: recursive.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "clz2.h"
22

3-
static const int mask[]={0,8,12,14};
4-
static const int magic[]={2,1,0,0};
3+
static const int mask[] = { 0, 8, 12,14 };
4+
static const int magic[] = { 2, 1, 0, 0 };
55

66
unsigned clz2(uint32_t x,int c)
77
{
88
if (!x && !c) return 32;
99

10-
uint32_t upper = (x >> (16>>c));
11-
uint32_t lower = (x & (0xFFFF>>mask[c]));
10+
uint32_t upper = (x >> (16 >> c));
11+
uint32_t lower = (x & (0xFFFF >> mask[c]));
1212
if (c == 3) return upper ? magic[upper] : 2 + magic[lower];
1313
return upper ? clz2(upper, c + 1) : (16 >> (c)) + clz2(lower, c + 1);
1414
}

0 commit comments

Comments
 (0)