bitwise alignment

14
Intermediate C Workshop Alignment & Bitwise Operations Version: <1.3> Date: < 23/10/2006> By FCG Team WIPRO CONFIDENTIAL

Upload: preetamn

Post on 16-Apr-2015

66 views

Category:

Documents


2 download

DESCRIPTION

align

TRANSCRIPT

Page 1: bitwise alignment

Intermediate C Workshop

Alignment & Bitwise Operations

Version: <1.3>

Date: < 23/10/2006>

By

FCG Team

WIPRO CONFIDENTIAL

Page 2: bitwise alignment

2Wipro confidential

Alignment & Padding

§ For today’s 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;

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

Page 3: bitwise alignment

3Wipro confidential

Alignment & Padding

§ For today’s 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;

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);}

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

WIPRO CONFIDENTIAL

Page 4: bitwise alignment

4Wipro confidential

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);

}

Memory view

Stack grows

this way

Lower Memory

Higher Memory

1 | 0 | 0 | 0b3c b3fp | p | p | 2b383 | 0 | 0 | 0

b3bb37b34

p | p | p | pp | p | p | pp | p | p | p

| | | b27b28b24

| | | b20| | p | p

&(abc.empno)

| | |

| | |

&(abc.salary)

| | |

| | | | | | | | |

b00

b1f

&(abc.name)

&x&z &y

| | |

WIPRO CONFIDENTIAL

Page 5: bitwise alignment

5Wipro 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:• 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 doesn’t have such alignment requirements

• Compiler generates code with masks & shifts to handle this.

WIPRO CONFIDENTIAL

Page 6: bitwise alignment

6Wipro confidential

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 */

}

SIGBUS or SPLIT ACCESS EXAMPLE

WIPRO CONFIDENTIAL

Page 7: bitwise alignment

7Wipro confidential

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 thisstruct 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

WIPRO CONFIDENTIAL

Page 8: bitwise alignment

8Wipro confidential

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 CNTL3One 8 bit register ERRBad 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;

WIPRO CONFIDENTIAL

Page 9: bitwise alignment

9Wipro confidential

Bit Manipulations

main(){

int x = -10;printf(“%d\n”, ~x+1);

}

main(){

unsigned int x = 5;while(--x >= 0){

printf(“Hello World\n”);}

}

WIPRO CONFIDENTIAL

Page 10: bitwise alignment

10Wipro confidential

Bit Manipulations

main(){

int x = -10;printf(“%d\n”, ~x+1);

}

main(){

unsigned int x = 5;while(--x >= 0){

printf(“Hello World\n”);}

}

Output: 10~x represents one’s compliment of x.~x+1 represents two’s compliment of x. (i.enegative of x)

HelloWorld is printed infinitely.X becomes MAXINT when it is decremented while it is having a value of 0.

WIPRO CONFIDENTIAL

Page 11: bitwise alignment

11Wipro confidential

Arithmetic and Logical Shifts

main(){

int x = 0x80000000;printf(“%x\n”, x >> 1);

}

main(){

unsigned int x = 0x80000000;printf(“%x\n”, x >> 1);

}

Output: 0xC0000000Signed number, “sign extension” happens for right shift. (Arithmetic shift)

Output: 0x40000000Unsigned number, “sign extension”doesn’t happen for right shift. (Logical shift)

WIPRO CONFIDENTIAL

Page 12: bitwise alignment

12Wipro confidential

Handy Bitwise Expressions

Mask contains all zeros if X is positive and all ones if X is negative.

ABS(X) without using comparisions

Mask = X >> 31Result = (~Mask & X) | (Mask & (-X))

7

Multiply by 2X << 12

Divide by 2X >> 13

Mask contains all zeros if X is greater than or equal to Y and all ones if Y is greater than X.

Max(X, Y) without using comparisions

Mask = (X-Y) >> 31Result = (Mask & Y) | (~Mask & X)

9

Mask contains all zeros if Y is less than or equal to X and all ones if X is less than Y.

Min(X, Y) without using comparisions

Mask = (X-Y) >> 31Result = (Mask & X) | (~Mask & Y)

8

Result is 0 or -1 based on the sign.To find out the sign of the number of X

X >> 316

Bit toggling (equivalent to ~)

X ^ all’ones5

Negative numbers are represented by two complement. (one’s complement +1)

Negative the number~X+14

Check whether number is odd or even

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

X & 11

RemarksExpression

WIPRO CONFIDENTIAL

Page 13: bitwise alignment

13Wipro confidential

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

WIPRO CONFIDENTIAL

Page 14: bitwise alignment

14Wipro confidential

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.

WIPRO CONFIDENTIAL