Xen Test Framework
div.h
Go to the documentation of this file.
1 #ifndef XTF_X86_DIV_H
2 #define XTF_X86_DIV_H
3 
4 #include <xtf/types.h>
5 
6 /*
7  * Divide a 64bit number by 32bit divisor without software support.
8  *
9  * The dividend is modified in place, and the modulus is returned.
10  */
11 static inline uint32_t divmod64(uint64_t *dividend, uint32_t divisor)
12 {
13  uint32_t mod;
14 
15 #ifdef __x86_64__
16 
17  /*
18  * On 64bit, issue a straight 'div' instruction.
19  */
20 
21  mod = *dividend % divisor;
22  *dividend /= divisor;
23 #else
24  {
25  /*
26  * On 32bit, this is harder.
27  *
28  * In x86, 'divl' can take a 64bit dividend, but the resulting
29  * quotient must fit in %eax or a #DE will occur.
30  *
31  * To avoid this, we split the division in two. The remainder from
32  * the higher divide can safely be used in the upper 32bits of the
33  * lower divide, as it will not cause an overflow.
34  */
35  uint32_t high = *dividend >> 32, low = *dividend, umod = 0;
36 
37  if ( high )
38  {
39  umod = high % divisor;
40  high /= divisor;
41  }
42 
43  asm ("divl %[divisor]"
44  : "+a" (low), "=d" (mod)
45  : [divisor] "rm" (divisor), "d" (umod));
46 
47  *dividend = (((uint64_t)high) << 32) | low;
48  }
49 #endif
50 
51  return mod;
52 }
53 
54 #endif /* XTF_X86_DIV_H */
55 
56 /*
57  * Local variables:
58  * mode: C
59  * c-file-style: "BSD"
60  * c-basic-offset: 4
61  * tab-width: 4
62  * indent-tabs-mode: nil
63  * End:
64  */
Common declarations for all tests.
static uint32_t divmod64(uint64_t *dividend, uint32_t divisor)
Definition: div.h:11
__UINT64_TYPE__ uint64_t
Definition: stdint.h:17
__UINT32_TYPE__ uint32_t
Definition: stdint.h:16