1// REQUIRES-ANY: arm-target-arch,armv6m-target-arch
2// RUN: %clang_builtins %s %librt -o %t && %run %t
3//===-- aeabi_idivmod_test.c - Test __aeabi_idivmod -----------------------===//
4//
5// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6// See https://llvm.org/LICENSE.txt for license information.
7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8//
9//===----------------------------------------------------------------------===//
10//
11// This file tests __aeabi_idivmod for the compiler_rt library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "int_lib.h"
16#include <stdio.h>
17
18#if __arm__
19// Based on divmodsi4_test.c
20
21extern du_int __aeabi_idivmod(si_int a, si_int b);
22
23int test__aeabi_idivmod(si_int a, si_int b,
24						si_int expected_result, si_int expected_rem)
25{
26	  si_int rem;
27    du_int ret = __aeabi_idivmod(a, b);
28    rem = ret >> 32;
29    si_int result = ret & 0xFFFFFFFF;
30    if (result != expected_result) {
31        printf("error in __aeabi_idivmod: %d / %d = %d, expected %d\n",
32               a, b, result, expected_result);
33		return 1;
34	}
35    if (rem != expected_rem) {
36        printf("error in __aeabi_idivmod: %d mod %d = %d, expected %d\n",
37               a, b, rem, expected_rem);
38		return 1;
39	}
40
41    return 0;
42}
43#endif
44
45
46int main()
47{
48#if __arm__
49    if (test__aeabi_idivmod(0, 1, 0, 0))
50        return 1;
51    if (test__aeabi_idivmod(0, -1, 0, 0))
52        return 1;
53
54    if (test__aeabi_idivmod(2, 1, 2, 0))
55        return 1;
56    if (test__aeabi_idivmod(2, -1, -2, 0))
57        return 1;
58    if (test__aeabi_idivmod(-2, 1, -2, 0))
59        return 1;
60    if (test__aeabi_idivmod(-2, -1, 2, 0))
61        return 1;
62
63	if (test__aeabi_idivmod(7, 5, 1, 2))
64        return 1;
65	if (test__aeabi_idivmod(-7, 5, -1, -2))
66        return 1;
67	if (test__aeabi_idivmod(19, 5, 3, 4))
68        return 1;
69	if (test__aeabi_idivmod(19, -5, -3, 4))
70        return 1;
71
72	if (test__aeabi_idivmod(0x80000000, 8, 0xf0000000, 0))
73        return 1;
74	if (test__aeabi_idivmod(0x80000007, 8, 0xf0000001, -1))
75        return 1;
76#else
77    printf("skipped\n");
78#endif
79
80    return 0;
81}
82