Sunteți pe pagina 1din 14

Intermediate C Workshop Alignment & Bitwise

W IP RO CO
Version: <1.3> Date: < 23/10/2006>

Operations
FI N D TI EN

By FCG Team

AL

Alignment & Padding


For todays session, assume following sizes and study the following program

Char: 1 byte Int: 4 bytes Short: 2 bytes

typedef struct { char name[30]; int empno; int salary; } EmpRec, * EmpRecPtr;

W IP

main() { int x = 1; char y = 2; int z = 3; EmpRec abc; EmpRecPtr empr = &abc;

printf(Sizes: int=%d, char=%d, EmpRec=%d, EmpRecPtr=%d\n, sizeof(x), sizeof(y), sizeof(abc), sizeof(empr)); printf(Address: &x=%p, &y=%p, &z=%p, &abc=%p, &empr=%p\n, &x, &y, &z, &abc, &empr); printf("Address: &abc.name=%p, &abc.empno=%p, &abc.salary=%p\n", &abc.name, &abc.empno, &abc.salary); }
Wipro confidential 2

RO CO FI N D TI EN AL

Alignment & Padding


For todays session, assume following sizes and study the following program

Char: 1 byte Int: 4 bytes Short: 2 bytes

typedef struct { char name[30]; int empno; int salary; } EmpRec, * EmpRecPtr;

Sizes: int=4, char=1, EmpRec=40, EmpRecPtr=4 Address: &x=0xbffffb3c, &y=0xbffffb3b, &z=0xbffffb34, &abc=0xbffffb00, &empr=0xbffffafc Address: &abc.name=0xbffffb00, &abc.empno=0xbffffb20, &abc.salary=0xbffffb24
W IP

main() { int x = 1; char y = 2; int z = 3; EmpRec abc; EmpRecPtr empr = &abc;

printf(Sizes: int=%d, char=%d, EmpRec=%d, EmpRecPtr=%d\n, sizeof(x), sizeof(y), sizeof(abc), sizeof(empr)); printf(Address: &x=%p, &y=%p, &z=%p, &abc=%p, &empr=%p\n, &x, &y, &z, &abc, &empr); printf("Address: &abc.name=%p, &abc.empno=%p, &abc.salary=%p\n", &abc.name, &abc.empno, &abc.salary);}
Wipro confidential 3

RO CO FI N D TI EN AL

Memory view
&(abc.salary)
typedef struct { char name[30]; int empno; int salary; } EmpRec, *EmpRecPtr; main() { int x = 1; char y = 2; int z = 3; EmpRec abc; EmpRecPtr empr = &abc; printf("Sizes: int=%d, char=%d, EmpRec=%d, EmpRecPtr=%d\n", sizeof(x), sizeof(y), sizeof(abc), sizeof(empr)); printf("Address: &x=%p, &y=%p, &z=%p, &abc=%p, &empr=%p\n", &x, &y, &z, &abc, &empr); printf("Address: &abc.name=%p, &abc.empno=%p, &abc.salary=%p\n", &abc.name, &abc.empno, &abc.salary); }

&(abc.empno)

&(abc.name) b00
| | | | | | | | | | | | | | | | | | | | | p | | | p | p | p

Lower Memory

Stack grows this way

| |

b20 b24 b28

| |

b1f b27

Higher Memory

W IP RO
&z

| | p | p | p | p | p 3 p 1 | | | | p 0 p 0

p p

CO
&x

b34 b38 b3c

FI N

| p | p | 0 | 0 | p | 2 | 0 | 0

b37 b3b b3f

TI EN

AL

&y

Wipro confidential

Alignment & Padding

Memory address of a variable will align with their size. In some architectures, unaligned access can result in SIGBUS.

In some cases, the memory access will be split

This is applicable for global variables, local variables, arguments passed to function and structure/union as well. To ensure address is correctly aligned, padding is done. Keep this in mind:
W IP RO

To calculate the size of structure/union

Beware if this is mapped to a set of registers.

Assembly/C intermixing Suitable mask & shifting may needed Bit fields doesnt have such alignment requirements
Compiler generates code with masks & shifts to handle this.

CO FI N D TI EN AL
Wipro confidential 5

SIGBUS or SPLIT ACCESS EXAMPLE


main() { char x = 10; char y = 20; int *p = &y; printf(&y=%p &p=%p\n, &y, &p); printf(%d\n, (*p) & 0xff); /* SIGBUS or unaligned split access */ }
W IP RO CO FI N D TI EN AL
Wipro confidential 6

Alignment & Padding


Assume a H/W device is memory mapped on the system bus

Occupies a address space of 16 bytes where 8 registers are located


Three 32 bit Registers(CNTL1, CNTL2, CNTL3, in that order) Four 8 bit Registers(STAT1, STAT2, STAT3, and ERR in that order) Assume it is located at 0x2000 in the memory map A structure can be defined to map this
struct hw_device { unsigned int cntl1; unsigned int cntl2; unsigned int cntl3; unsigned char stat1; unsigned char stat2; unsigned char stat3; unsigned char err; } *my_hw_device; my_hw_device_ptr = (struct hw_device *) (0x2000);

The registers can be accessed as my_hw_device->cntl1, my_hw_device->err and so on. Simplifies access to device registers, useful to device driver writers, OS developers.

Should be careful about any padding for alignment restrictions

W IP RO CO FI N D TI EN AL
Wipro confidential 7

Example Where padding causes trouble

Register sequence: Two 32 bit Registers(CNTL1, CNTL2, in that order) Three 8 bit Registers(STAT1, STAT2, STAT3, in that order) One 32 bit register CNTL3 One 8 bit register ERR Bad structure that can cause trouble: typedef struct hw_device { unsigned int cntl1; unsigned int cntl2; unsigned char stat1; unsigned char stat2; unsigned char stat3; unsigned int cntl3; unsigned char err; } MYDEVICE, *MYDEVICE_PTR;
W IP RO CO FI N D

TI EN AL
Wipro confidential 8

Bit Manipulations

main() { int x = -10; printf(%d\n, ~x+1); } main() { unsigned int x = 5; while(--x >= 0) { printf(Hello World\n); } }

W IP RO CO FI N D TI EN AL
Wipro confidential 9

Bit Manipulations

main() { int x = -10; printf(%d\n, ~x+1); }

Output: 10 ~x represents ones compliment of x. ~x+1 represents twos compliment of x. (i.e negative of x)

main() { HelloWorld is printed infinitely. unsigned int x = 5; X becomes MAXINT when it is decremented while it is having a value of 0. while(--x >= 0) { printf(Hello World\n); } }
W IP RO CO FI N D TI EN AL

Wipro confidential

10

Arithmetic and Logical Shifts

main() { int x = 0x80000000; printf(%x\n, x >> 1); }

Output: 0xC0000000 Signed number, sign extension happens for right shift. (Arithmetic shift)

main() { unsigned int x = 0x80000000; printf(%x\n, x >> 1); Output: 0x40000000 } Unsigned number, sign extension
doesnt happen for right shift. (Logical shift)
W IP RO CO FI N D TI EN AL

Wipro confidential

11

Handy Bitwise Expressions

Expression
1 2 3 4 5 6 7 8 9 X&1 X << 1 X >> 1 ~X+1 X ^ allones X >> 31 Mask = X >> 31 Result = (~Mask & X) | (Mask & (-X)) Mask = (X-Y) >> 31 Result = (Mask & X) | (~Mask & Y) Mask = (X-Y) >> 31 Result = (Mask & Y) | (~Mask & X)

Purpose
Check whether number is odd or even Multiply by 2 Divide by 2 Negative the number Bit toggling (equivalent to ~) To find out the sign of the number of X ABS(X) without using comparisions Min(X, Y) without using comparisions Max(X, Y) without using comparisions

Remarks
All numbers are interms of 0s and 1s and hence it is sufficient check the last bit

Negative numbers are represented by two complement. (ones complement +1)

Result is 0 or -1 based on the sign. Mask contains all zeros if X is positive and all ones if X is negative. Mask contains all zeros if Y is less than or equal to X and all ones if X is less than Y. Mask contains all zeros if X is greater than or equal to Y and all ones if Y is greater than X.

IP

RO CO FI N D TI EN AL
Wipro confidential 12

Exercise 1: Power of 2.

Come up with an handy expression that can be used to detect whether the given number is a power of 2. Clue: All number that is a power of 2 will have just one bit set.

Examples:
1 00000001 2 00000010 4 00000100 8 00001000 16- 00010000 32 -00100000

W IP RO CO FI N D TI EN AL
Wipro confidential 13

Thank you.

Information contained and transmitted by this presentation is proprietary to Wipro Limited and is intended for use only by the individual or entity to which it is addressed, and contains information that is privileged, confidential or exempt from disclosure under applicable law.

W IP RO CO FI N D TI EN AL
Wipro confidential 14

S-ar putea să vă placă și