-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path1.7_align_size.c
executable file
·60 lines (50 loc) · 1.4 KB
/
1.7_align_size.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**********************************************************************
** 1.7 : Using bit operation to align a size
**********************************************************************/
#include <stdio.h>
#include <ctype.h>
/*
** we can use a combination of left and right shifts using the power of two
** align. If align = 5 we will align on 2^5 = 32 bytes
** Constraint : align < 32
*/
size_t ft_align_power_of_two(size_t size, size_t align)
{
return (((size + (1 << align) - 1) >> align) << align);
}
/*
** equivalently we can use a mask
** if size is 37 and we add the mask (31) we get 68.
** Then we ask to keep only the bits above the 5th since '~mask' toggle bits
** will becomes 0b1...10000
** Constraint : mask must be a power of 2 - 1
*/
size_t ft_align_mask(size_t size, size_t mask)
{
return ((size + mask) & ~mask);
}
int main(void)
{
int n = 37;
printf("%lu\n", ft_align_power_of_two(n, 5)); // 2^5 = 32
printf("%lu\n", ft_align_mask(n, 0x1f)); // 0x1f + 1 = 0x20 = 32
return (0);
}
/*
Resulting assembly:
ft_align_power_of_two(unsigned long long, unsigned long long):
mov rcx, rsi
mov eax, 1
sal eax, cl
cdqe
lea rax, [rdi-1+rax]
shr rax, cl
sal rax, cl
ret
ft_align_mask(unsigned long long, unsigned long long):
lea rax, [rdi+rsi]
not rsi
and rax, rsi
ret
You can hence witness that the function aligning using the mask is much more optimized !!
*/