intdiv.c revision 436e89c602e787e7a27dd6624b09beed41a0da8a
1
2#include <stdio.h>
3
4typedef    signed int  Int;
5typedef  unsigned int  UInt;
6
7__attribute__((noinline)) UInt do_udiv32 ( UInt x, UInt y )
8{
9  UInt res;
10  __asm__ __volatile__(
11     "mov r9, %1 ; mov r10, %2 ; udiv r3,r9,r10 ; mov %0, r3"
12     : "=r"(res) : "r"(x), "r"(y) : "r3", "r9", "r10"
13  );
14  return res;
15}
16
17__attribute__((noinline)) Int do_sdiv32 ( Int x, Int y )
18{
19  UInt res;
20  __asm__ __volatile__(
21     "mov r9, %1 ; mov r10, %2 ; sdiv r3,r9,r10 ; mov %0, r3"
22     : "=r"(res) : "r"(x), "r"(y) : "r3", "r9", "r10"
23  );
24  return res;
25}
26
27void test ( UInt x, UInt y )
28{
29  UInt ru = do_udiv32(x,y);
30  Int rs = do_sdiv32(x,y);
31  printf( "%08x %08x -> u:%08x s:%08x\n", x, y, ru, (UInt)rs);
32}
33
34int main ( void )
35{
36  // Check basic operation
37  test( 500, 50 );
38  test( 500, -50 );
39  test( -500, 50 );
40  test( -500, -50 );
41  // Check for rounding towards zero
42  test( 100, 7 );   // 14.285
43  test( -100, 7 );
44  test( 100, -7 );
45  test( -100, -7 );
46  // Division by zero produces zero
47  test( 1, 0 );
48  test( 0, 0 );
49  test( -1, 0 );
50  test( 0x80000000, 0 );
51  test( 0x7FFFFFFF, 0 );
52  // Test signed range ends
53  test( 0x80000000, -1 ); // unrepresentable as signed 32
54  return 0;
55}
56