- added optimization notes in readme
[dosdemo] / src / util.h
1 #ifndef UTIL_H_
2 #define UTIL_H_
3
4 #include "inttypes.h"
5
6 #ifdef __GNUC__
7 #define INLINE __inline
8
9 #elif defined(__WATCOMC__)
10 #define INLINE __inline
11
12 #else
13 #define INLINE
14 #endif
15
16 /* fast conversion of double -> 32bit int
17  * for details see:
18  *  - http://chrishecker.com/images/f/fb/Gdmfp.pdf
19  *  - http://stereopsis.com/FPU.html#convert
20  */
21 static INLINE int32_t cround64(double val)
22 {
23         val += 6755399441055744.0;
24         return *(int32_t*)&val;
25 }
26
27 extern uint32_t perf_start_count, perf_interval_count;
28
29 #ifdef __WATCOMC__
30 void perf_start(void);
31 #pragma aux perf_start = \
32         "xor eax, eax" \
33         "cpuid" \
34         "rdtsc" \
35         "mov [perf_start_count], eax" \
36         modify[eax ebx ecx edx];
37
38 void perf_end(void);
39 #pragma aux perf_end = \
40         "xor eax, eax" \
41         "cpuid" \
42         "rdtsc" \
43         "sub eax, [perf_start_count]" \
44         "mov [perf_interval_count], eax" \
45         modify [eax ebx ecx edx];
46 #endif
47
48 #ifdef __GNUC__
49 #define perf_start()  asm volatile ( \
50         "xor %%eax, %%eax\n" \
51         "cpuid\n" \
52         "rdtsc\n" \
53         "mov %%eax, %0\n" \
54         : "=m"(perf_start_count) \
55         :: "%eax", "%ebx", "%ecx", "%edx")
56
57 #define perf_end() asm volatile ( \
58         "xor %%eax, %%eax\n" \
59         "cpuid\n" \
60         "rdtsc\n" \
61         "sub %1, %%eax\n" \
62         "mov %%eax, %0\n" \
63         : "=m"(perf_interval_count) \
64         : "m"(perf_start_count) \
65         : "%eax", "%ebx", "%ecx", "%edx")
66 #endif
67
68 #ifdef _MSC_VER
69 #define perf_start() \
70         do { \
71                 __asm { \
72                         xor eax, eax \
73                         cpuid \
74                         rdtsc \
75                         mov [perf_start_count], eax \
76                 } \
77         } while(0)
78
79 #define perf_end() \
80         do { \
81                 __asm { \
82                         xor eax, eax \
83                         cpuid \
84                         rdtsc \
85                         sub eax, [perf_start_count] \
86                         mov [perf_interval_count], eax \
87                 } \
88         } while(0)
89 #endif
90
91 #endif  /* UTIL_H_ */