summaryrefslogtreecommitdiff
path: root/arch/arc/lib/libgcc2.c
blob: d5ad327f655dd2675be568b0c24acb68809448e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (C) 1989-2013 Free Software Foundation, Inc.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include "libgcc2.h"

DWtype
__ashldi3(DWtype u, shift_count_type b)
{
	if (b == 0)
		return u;

	const DWunion uu = {.ll = u};
	const shift_count_type bm = W_TYPE_SIZE - b;
	DWunion w;

	if (bm <= 0) {
		w.s.low = 0;
		w.s.high = (UWtype)uu.s.low << -bm;
	} else {
		const UWtype carries = (UWtype) uu.s.low >> bm;

		w.s.low = (UWtype)uu.s.low << b;
		w.s.high = ((UWtype)uu.s.high << b) | carries;
	}

	return w.ll;
}

DWtype
__ashrdi3(DWtype u, shift_count_type b)
{
	if (b == 0)
		return u;

	const DWunion uu = {.ll = u};
	const shift_count_type bm = W_TYPE_SIZE - b;
	DWunion w;

	if (bm <= 0) {
		/* w.s.high = 1..1 or 0..0 */
		w.s.high = uu.s.high >> (W_TYPE_SIZE - 1);
		w.s.low = uu.s.high >> -bm;
	} else {
		const UWtype carries = (UWtype) uu.s.high << bm;

		w.s.high = uu.s.high >> b;
		w.s.low = ((UWtype)uu.s.low >> b) | carries;
	}

	return w.ll;
}

DWtype
__lshrdi3(DWtype u, shift_count_type b)
{
	if (b == 0)
		return u;

	const DWunion uu = {.ll = u};
	const shift_count_type bm = W_TYPE_SIZE - b;
	DWunion w;

	if (bm <= 0) {
		w.s.high = 0;
		w.s.low = (UWtype)uu.s.high >> -bm;
	} else {
		const UWtype carries = (UWtype)uu.s.high << bm;

		w.s.high = (UWtype)uu.s.high >> b;
		w.s.low = ((UWtype)uu.s.low >> b) | carries;
	}

	return w.ll;
}

unsigned long
udivmodsi4(unsigned long num, unsigned long den, int modwanted)
{
	unsigned long bit = 1;
	unsigned long res = 0;

	while (den < num && bit && !(den & (1L<<31))) {
		den <<= 1;
		bit <<= 1;
	}

	while (bit) {
		if (num >= den) {
			num -= den;
			res |= bit;
		}
		bit >>= 1;
		den >>= 1;
	}

	if (modwanted)
		return num;

	return res;
}

long
__divsi3(long a, long b)
{
	int neg = 0;
	long res;

	if (a < 0) {
		a = -a;
		neg = !neg;
	}

	if (b < 0) {
		b = -b;
		neg = !neg;
	}

	res = udivmodsi4(a, b, 0);

	if (neg)
		res = -res;

	return res;
}

long
__modsi3(long a, long b)
{
	int neg = 0;
	long res;

	if (a < 0) {
		a = -a;
		neg = 1;
	}

	if (b < 0)
		b = -b;

	res = udivmodsi4(a, b, 1);

	if (neg)
		res = -res;

	return res;
}

long
__udivsi3(long a, long b)
{
	return udivmodsi4(a, b, 0);
}

long
__umodsi3(long a, long b)
{
	return udivmodsi4(a, b, 1);
}