1. double -> int //directx9c以上失效,因为其会改变单浮点精度
ftol
1inline long ftol(double x)
2{
3 #ifdef __WINDOWS__
4 static const double magic = 6755399441055744.0; // 2^51 + 2^52
5
6 double tmp = x;
7 tmp += (x > 0) ? -0.499999999999 : +0.499999999999;
8 tmp += magic;
9 return *(long*)&tmp;
10#else
11 return (long)x;
12#endif
13}
1inline long ftol(double x)
2{
3 #ifdef __WINDOWS__
4 static const double magic = 6755399441055744.0; // 2^51 + 2^52
5
6 double tmp = x;
7 tmp += (x > 0) ? -0.499999999999 : +0.499999999999;
8 tmp += magic;
9 return *(long*)&tmp;
10#else
11 return (long)x;
12#endif
13}
2. c双向链表操作
list
1struct list_head {
2 struct list_head *next, *prev;
3};
4
5
6#define LIST_HEAD_INIT(name) { &(name), &(name) }
7
8#define LIST_HEAD(name) \
9 struct list_head name = LIST_HEAD_INIT(name)
10
11#define INIT_LIST_HEAD(ptr) do { \
12 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
13} while (0)
14
15/**//*
16 * Insert a new entry between two known consecutive entries.
17 *
18 * This is only for internal list manipulation where we know
19 * the prev/next entries already!
20 */
21static inline void __list_add(struct list_head *new,
22 struct list_head *prev,
23 struct list_head *next)
24{
25 next->prev = new;
26 new->next = next;
27 new->prev = prev;
28 prev->next = new;
29}
30
31/**//**
32 * list_add - add a new entry
33 * @new: new entry to be added
34 * @head: list head to add it after
35 *
36 * Insert a new entry after the specified head.
37 * This is good for implementing stacks.
38 */
39static inline void list_add(struct list_head *new, struct list_head *head)
40{
41 __list_add(new, head, head->next);
42}
43
44..
45
46/**//**
47 * list_entry - get the struct for this entry
48 * @ptr: the &struct list_head pointer.
49 * @type: the type of the struct this is embedded in.
50 * @member: the name of the list_struct within the struct.
51 */
52#define list_entry(ptr, type, member) \
53 container_of(ptr, type, member)
54
55/**//**
56 * list_for_each - iterate over a list
57 * @pos: the &struct list_head to use as a loop counter.
58 * @head: the head for your list.
59 */
60#define list_for_each(pos, head) \
61 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
62 pos = pos->next)
1struct list_head {
2 struct list_head *next, *prev;
3};
4
5
6#define LIST_HEAD_INIT(name) { &(name), &(name) }
7
8#define LIST_HEAD(name) \
9 struct list_head name = LIST_HEAD_INIT(name)
10
11#define INIT_LIST_HEAD(ptr) do { \
12 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
13} while (0)
14
15/**//*
16 * Insert a new entry between two known consecutive entries.
17 *
18 * This is only for internal list manipulation where we know
19 * the prev/next entries already!
20 */
21static inline void __list_add(struct list_head *new,
22 struct list_head *prev,
23 struct list_head *next)
24{
25 next->prev = new;
26 new->next = next;
27 new->prev = prev;
28 prev->next = new;
29}
30
31/**//**
32 * list_add - add a new entry
33 * @new: new entry to be added
34 * @head: list head to add it after
35 *
36 * Insert a new entry after the specified head.
37 * This is good for implementing stacks.
38 */
39static inline void list_add(struct list_head *new, struct list_head *head)
40{
41 __list_add(new, head, head->next);
42}
43
44..
45
46/**//**
47 * list_entry - get the struct for this entry
48 * @ptr: the &struct list_head pointer.
49 * @type: the type of the struct this is embedded in.
50 * @member: the name of the list_struct within the struct.
51 */
52#define list_entry(ptr, type, member) \
53 container_of(ptr, type, member)
54
55/**//**
56 * list_for_each - iterate over a list
57 * @pos: the &struct list_head to use as a loop counter.
58 * @head: the head for your list.
59 */
60#define list_for_each(pos, head) \
61 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
62 pos = pos->next)
container_of 宏
1
/**/
/* GCC */
2 #define container_of(ptr, type, member) ({ \
3 const typeof ( ((type * ) 0 ) -> member ) * __mptr = (ptr); \
4 (type * )( ( char * )__mptr - offsetof(type,member) );})
5
6 // or
7
8 #define container_of(ptr, type, member) ( \
9 (type * )( ( char * )ptr - offsetof(type,member) ) )
2 #define container_of(ptr, type, member) ({ \
3 const typeof ( ((type * ) 0 ) -> member ) * __mptr = (ptr); \
4 (type * )( ( char * )__mptr - offsetof(type,member) );})
5
6 // or
7
8 #define container_of(ptr, type, member) ( \
9 (type * )( ( char * )ptr - offsetof(type,member) ) )
how to use
1
struct
my_data
{
2 int x;
3 int y;
4 struct list_head list;
5}
6
7 /**/ /* 链表头 */
8 LIST_HEAD(my_listhead);
9
10 void my_function()
11 {
12
13 /**//* 节点对象 */
14 struct my_data *node_1 = (struct my_data *) malloc(sizeof(struct my_data));
15 struct my_data *node_2 = (struct my_data *) malloc(sizeof(struct my_data));
16
17 /**//* 加入链表 */
18 list_add (node_1->list, &my_listhead);
19 list_add (node_2->list, &my_listhead);
20
21 /**//* 遍历链表 */
22 struct my_data * node;
23 struct list_head *pos;
24 list_for_each (pos, &my_listhead) {
25 node = list_entry (pos, struct my_data, list);
26
27 }
2 int x;
3 int y;
4 struct list_head list;
5}
6
7 /**/ /* 链表头 */
8 LIST_HEAD(my_listhead);
9
10 void my_function()
11 {
12
13 /**//* 节点对象 */
14 struct my_data *node_1 = (struct my_data *) malloc(sizeof(struct my_data));
15 struct my_data *node_2 = (struct my_data *) malloc(sizeof(struct my_data));
16
17 /**//* 加入链表 */
18 list_add (node_1->list, &my_listhead);
19 list_add (node_2->list, &my_listhead);
20
21 /**//* 遍历链表 */
22 struct my_data * node;
23 struct list_head *pos;
24 list_for_each (pos, &my_listhead) {
25 node = list_entry (pos, struct my_data, list);
26
27 }