Posts

Showing posts from July, 2011

Structure member Alignment & Padding

Structure and union variable declarations A structure or union declaration has the same form as a definition except the declaration does not have a brace-enclosed list of members. You must declare the structure or union data type before you can define a variable having that type. Read syntax diagramSkip visual syntax diagramStructure or union variable declaration syntax .-----------------------------. V | >>---+-------------------------+-+--+-struct-+------------------> +-storage_class_specifier-+ '-union--' '-type_qualifier----------' >--tag_identifier--declarator--;------------------------------->< Bit Fields Alignment If a series of bit fields does not add up to the size of an int, padding can take place. The amount of padding is determined by the alignment characteristics of the members of the structure. The following example demonstrates padding, and is valid for all implementations. Suppose th

typeof, offsetof, container_of

http://gcc.gnu.org/onlinedocs/gcc/Typeof.html http://kerneltrap.com/node/15927 http://en.wikipedia.org/wiki/Offsetof #define offsetof(st, m)      \     ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) http://www.kroah.com/log/linux/container_of.html #define container_of(ptr, type, member) ({ \     const typeof( ((type *)0)->member ) *__mptr = (ptr);  \     (type *)( (char *)__mptr - offsetof(type,member) );}) C pre processor http://tigcc.ticalc.org/doc/cpp.html http://stackoverflow.com/questions/1552454/can-i-substitute-func-into-an-identifier-name-in-a-c-macro

Signed integer representeton in memory

For char range -128 to +127; MSB is used for sign representation, 2 complement is used to represent -ve val for any -ve value take 2s complement to convert it to hex form. and to return to its original decimal value also do 2s complement. for example  -30  in hex will be 30 = 0x1E => 2s complement = ~0x1E + 1 = 0xE2 To get a -ve decimal value 0xE2 's 2s complement = 1E =30; Note:  2s complement is used for negative integers (negation of positive int) Note:char a= 0x80 (== -128); now a<<1 gives -256 (though it's out of range) Ref: http://en.wikipedia.org/wiki/Signed_number_representations http://www.hoomanb.com/cs/yorku/2021/data_representation.pdf http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html

Floating point Calculation

http://www.fsinc.com/reference/html/com9anm.htm http://www.math.uic.edu/~hanson/mcs471/FloatingPointRep.html http://www.randelshofer.ch/fhw/gri/float.html http://www.keil.com/support/man/docs/c51/c51_ap_floatingpt.htm http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF

Bit counting in a UINT32

unsigned int v; // count bits set in this (32-bit value) unsigned int c; // store the total here   /* Naive way */ for(c = 0; v; c++) v &= v - 1 ;     /* Parallel computing */     static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers static const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF}; c = v - ((v >> 1) & B[0]); c = ((c >> S[1]) & B[1]) + (c & B[1]); c = ((c >> S[2]) + c) & B[2]; c = ((c >> S[3]) + c) & B[3]; c = ((c >> S[4]) + c) & B[4];   The B array, which is in binary pattern: B[0] = 0x55555555 = 01010101 01010101 01010101 01010101 B[1] = 0x33333333 = 00110011 00110011 00110011 00110011 B[2] = 0x0F0F0F0F = 00001111 00001111 00001111 00001111 B[3] = 0x00FF00FF = 00000000 11111111 00000000 11111111 B[4] = 0x0000FFFF = 00000000 00000000 11111111 11111111 We can adjust the method for larger integer sizes by continuing with the patterns for the Binary Magic Numbers, B and S. If

Circle draw without floating point calculation

Drawing x^2 +y^2 =r^2 without floating point calculation based on   Bresenham's line algorithm. ( raster circle drawing) ------------------------------------------------------------------------------------ void CircleDraw ( int x0 , int y0 , int radius ) { int f = 1 - radius ; int ddF_x = 1 ; int ddF_y = - 2 * radius ; int x = 0 ; int y = radius ; putPixel ( x0 , y0 + radius ) ; putPixel ( x0 , y0 - radius ) ; putPixel ( x0 + radius , y0 ) ; putPixel ( x0 - radius , y0 ) ; while ( x < y ) { // ddF_x == 2 * x + 1; // ddF_y == -2 * y; // f == x*x + y*y - radius*radius + 2*x - y + 1; if ( f >= 0 ) { y --; ddF_y += 2 ; f += ddF_y ; } x ++; ddF_x += 2 ; f += ddF_x ; putPixel ( x0 + x , y0 + y ) ; putPixel ( x0 - x , y0 + y ) ; putPixel ( x0 + x , y0 - y ) ; putPixel ( x0 - x , y0 - y ) ; putPixel ( x0