1
2 #include <stdio.h>
3
4 typedef signed int Int;
5 typedef unsigned int UInt;
6
do_udiv32(UInt x,UInt y)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
do_sdiv32(Int x,Int y)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
test(UInt x,UInt y)27 void 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
main(void)34 int 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