summaryrefslogtreecommitdiff
path: root/bit/bit.h
blob: 9e1b09ab844bb3d70d92665b504d69b3cfedeec4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef BIT_H
#define BIT_H

/*
	Most of these functions are courtesy of
	Hacker's Delight, 2nd edition (Henry S. Warren, Jr.)
*/

#include <stdio.h>
#include "types/types.h"


// rounds down (floors) to the nearest power of 2
u64 flp2(u64 x);

// rounds down (floors) to the nearest power of 2 in 32 bits
u32 flp2_32(u32 x);

// rounds up (ceils) to the nearest power of 2
u64 clp2(u64 x);

// rounds up (ceils) to the nearest power of 2 in 32 bits
u32 clp2_32(u32 x);

// prints the bits of the data pointed to by ptr of size size to fp
void _bitstofile(void const * const ptr, size_t const size, FILE *fp);

#define fprintb(n, f) ({	\
		typeof(n) _n = (n);	\
		_bitstofile(&(_n), sizeof(_n), f);	\
	})

#define printb(n) fprintb(n, stdout)

#define fputb(n, f) ({	\
		fprintb(n, f);	\
		fputc('\n', f);	\
	})

#define putb(n) fputb(n, stdout)

#endif