Skip to content

Commit b35cd98

Browse files
lib: Add shared copies of some GCC library routines
Many ports (m32r, microblaze, mips, parisc, score, and sparc) use functionally identical copies of various GCC library routine files, which came up as we were submitting the RISC-V port (which also uses some of these). This patch adds a new copy of these library routine files, which are functionally identical to the various other copies. These are availiable via Kconfig as CONFIG_GENERIC_$ROUTINE, which currently isn't used anywhere. Reviewed-by: Geert Uytterhoeven <geert@linux-m68k.org> Signed-off-by: Palmer Dabbelt <palmer@dabbelt.com>
1 parent c98cfe4 commit b35cd98

File tree

9 files changed

+353
-0
lines changed

9 files changed

+353
-0
lines changed

include/lib/libgcc.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* include/lib/libgcc.h
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, see the file COPYING, or write
16+
* to the Free Software Foundation, Inc.
17+
*/
18+
19+
#ifndef __LIB_LIBGCC_H
20+
#define __LIB_LIBGCC_H
21+
22+
#include <asm/byteorder.h>
23+
24+
typedef int word_type __attribute__ ((mode (__word__)));
25+
26+
#ifdef __BIG_ENDIAN
27+
struct DWstruct {
28+
int high, low;
29+
};
30+
#elif defined(__LITTLE_ENDIAN)
31+
struct DWstruct {
32+
int low, high;
33+
};
34+
#else
35+
#error I feel sick.
36+
#endif
37+
38+
typedef union {
39+
struct DWstruct s;
40+
long long ll;
41+
} DWunion;
42+
43+
#endif /* __ASM_LIBGCC_H */

lib/Kconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,21 @@ config STRING_SELFTEST
587587
bool "Test string functions"
588588

589589
endmenu
590+
591+
config GENERIC_ASHLDI3
592+
bool
593+
594+
config GENERIC_ASHRDI3
595+
bool
596+
597+
config GENERIC_LSHRDI3
598+
bool
599+
600+
config GENERIC_MULDI3
601+
bool
602+
603+
config GENERIC_CMPDI2
604+
bool
605+
606+
config GENERIC_UCMPDI2
607+
bool

lib/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,11 @@ UBSAN_SANITIZE_ubsan.o := n
247247
obj-$(CONFIG_SBITMAP) += sbitmap.o
248248

249249
obj-$(CONFIG_PARMAN) += parman.o
250+
251+
# GCC library routines
252+
obj-$(CONFIG_GENERIC_ASHLDI3) += ashldi3.o
253+
obj-$(CONFIG_GENERIC_ASHRDI3) += ashrdi3.o
254+
obj-$(CONFIG_GENERIC_LSHRDI3) += lshrdi3.o
255+
obj-$(CONFIG_GENERIC_MULDI3) += muldi3.o
256+
obj-$(CONFIG_GENERIC_CMPDI2) += cmpdi2.o
257+
obj-$(CONFIG_GENERIC_UCMPDI2) += ucmpdi2.o

lib/ashldi3.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, see the file COPYING, or write
14+
* to the Free Software Foundation, Inc.
15+
*/
16+
17+
#include <linux/export.h>
18+
19+
#include <lib/libgcc.h>
20+
21+
long long notrace __ashldi3(long long u, word_type b)
22+
{
23+
DWunion uu, w;
24+
word_type bm;
25+
26+
if (b == 0)
27+
return u;
28+
29+
uu.ll = u;
30+
bm = 32 - b;
31+
32+
if (bm <= 0) {
33+
w.s.low = 0;
34+
w.s.high = (unsigned int) uu.s.low << -bm;
35+
} else {
36+
const unsigned int carries = (unsigned int) uu.s.low >> bm;
37+
38+
w.s.low = (unsigned int) uu.s.low << b;
39+
w.s.high = ((unsigned int) uu.s.high << b) | carries;
40+
}
41+
42+
return w.ll;
43+
}
44+
EXPORT_SYMBOL(__ashldi3);

lib/ashrdi3.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, see the file COPYING, or write
14+
* to the Free Software Foundation, Inc.
15+
*/
16+
17+
#include <linux/export.h>
18+
19+
#include <lib/libgcc.h>
20+
21+
long long notrace __ashrdi3(long long u, word_type b)
22+
{
23+
DWunion uu, w;
24+
word_type bm;
25+
26+
if (b == 0)
27+
return u;
28+
29+
uu.ll = u;
30+
bm = 32 - b;
31+
32+
if (bm <= 0) {
33+
/* w.s.high = 1..1 or 0..0 */
34+
w.s.high =
35+
uu.s.high >> 31;
36+
w.s.low = uu.s.high >> -bm;
37+
} else {
38+
const unsigned int carries = (unsigned int) uu.s.high << bm;
39+
40+
w.s.high = uu.s.high >> b;
41+
w.s.low = ((unsigned int) uu.s.low >> b) | carries;
42+
}
43+
44+
return w.ll;
45+
}
46+
EXPORT_SYMBOL(__ashrdi3);

lib/cmpdi2.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, see the file COPYING, or write
14+
* to the Free Software Foundation, Inc.
15+
*/
16+
17+
#include <linux/export.h>
18+
19+
#include <lib/libgcc.h>
20+
21+
word_type notrace __cmpdi2(long long a, long long b)
22+
{
23+
const DWunion au = {
24+
.ll = a
25+
};
26+
const DWunion bu = {
27+
.ll = b
28+
};
29+
30+
if (au.s.high < bu.s.high)
31+
return 0;
32+
else if (au.s.high > bu.s.high)
33+
return 2;
34+
35+
if ((unsigned int) au.s.low < (unsigned int) bu.s.low)
36+
return 0;
37+
else if ((unsigned int) au.s.low > (unsigned int) bu.s.low)
38+
return 2;
39+
40+
return 1;
41+
}
42+
EXPORT_SYMBOL(__cmpdi2);

lib/lshrdi3.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* lib/lshrdi3.c
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, see the file COPYING, or write
16+
* to the Free Software Foundation, Inc.
17+
*/
18+
19+
#include <linux/module.h>
20+
#include <lib/libgcc.h>
21+
22+
long long notrace __lshrdi3(long long u, word_type b)
23+
{
24+
DWunion uu, w;
25+
word_type bm;
26+
27+
if (b == 0)
28+
return u;
29+
30+
uu.ll = u;
31+
bm = 32 - b;
32+
33+
if (bm <= 0) {
34+
w.s.high = 0;
35+
w.s.low = (unsigned int) uu.s.high >> -bm;
36+
} else {
37+
const unsigned int carries = (unsigned int) uu.s.high << bm;
38+
39+
w.s.high = (unsigned int) uu.s.high >> b;
40+
w.s.low = ((unsigned int) uu.s.low >> b) | carries;
41+
}
42+
43+
return w.ll;
44+
}
45+
EXPORT_SYMBOL(__lshrdi3);

lib/muldi3.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, see the file COPYING, or write
14+
* to the Free Software Foundation, Inc.
15+
*/
16+
17+
#include <linux/export.h>
18+
#include <lib/libgcc.h>
19+
20+
#define W_TYPE_SIZE 32
21+
22+
#define __ll_B ((unsigned long) 1 << (W_TYPE_SIZE / 2))
23+
#define __ll_lowpart(t) ((unsigned long) (t) & (__ll_B - 1))
24+
#define __ll_highpart(t) ((unsigned long) (t) >> (W_TYPE_SIZE / 2))
25+
26+
/* If we still don't have umul_ppmm, define it using plain C. */
27+
#if !defined(umul_ppmm)
28+
#define umul_ppmm(w1, w0, u, v) \
29+
do { \
30+
unsigned long __x0, __x1, __x2, __x3; \
31+
unsigned short __ul, __vl, __uh, __vh; \
32+
\
33+
__ul = __ll_lowpart(u); \
34+
__uh = __ll_highpart(u); \
35+
__vl = __ll_lowpart(v); \
36+
__vh = __ll_highpart(v); \
37+
\
38+
__x0 = (unsigned long) __ul * __vl; \
39+
__x1 = (unsigned long) __ul * __vh; \
40+
__x2 = (unsigned long) __uh * __vl; \
41+
__x3 = (unsigned long) __uh * __vh; \
42+
\
43+
__x1 += __ll_highpart(__x0); /* this can't give carry */\
44+
__x1 += __x2; /* but this indeed can */ \
45+
if (__x1 < __x2) /* did we get it? */ \
46+
__x3 += __ll_B; /* yes, add it in the proper pos */ \
47+
\
48+
(w1) = __x3 + __ll_highpart(__x1); \
49+
(w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
50+
} while (0)
51+
#endif
52+
53+
#if !defined(__umulsidi3)
54+
#define __umulsidi3(u, v) ({ \
55+
DWunion __w; \
56+
umul_ppmm(__w.s.high, __w.s.low, u, v); \
57+
__w.ll; \
58+
})
59+
#endif
60+
61+
long long notrace __muldi3(long long u, long long v)
62+
{
63+
const DWunion uu = {.ll = u};
64+
const DWunion vv = {.ll = v};
65+
DWunion w = {.ll = __umulsidi3(uu.s.low, vv.s.low)};
66+
67+
w.s.high += ((unsigned long) uu.s.low * (unsigned long) vv.s.high
68+
+ (unsigned long) uu.s.high * (unsigned long) vv.s.low);
69+
70+
return w.ll;
71+
}
72+
EXPORT_SYMBOL(__muldi3);

lib/ucmpdi2.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, see the file COPYING, or write
14+
* to the Free Software Foundation, Inc.
15+
*/
16+
17+
#include <linux/module.h>
18+
#include <lib/libgcc.h>
19+
20+
word_type __ucmpdi2(unsigned long long a, unsigned long long b)
21+
{
22+
const DWunion au = {.ll = a};
23+
const DWunion bu = {.ll = b};
24+
25+
if ((unsigned int) au.s.high < (unsigned int) bu.s.high)
26+
return 0;
27+
else if ((unsigned int) au.s.high > (unsigned int) bu.s.high)
28+
return 2;
29+
if ((unsigned int) au.s.low < (unsigned int) bu.s.low)
30+
return 0;
31+
else if ((unsigned int) au.s.low > (unsigned int) bu.s.low)
32+
return 2;
33+
return 1;
34+
}
35+
EXPORT_SYMBOL(__ucmpdi2);

0 commit comments

Comments
 (0)