summaryrefslogtreecommitdiff
path: root/arch/alpha/lib/tishift.c
blob: 8319907d81502a8eec4397fb10f49e4828af8be6 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * 128-bit shift helpers.
 *
 * gcc open-codes a variable 128-bit shift when optimizing for speed, but
 * calls these libgcc routines when optimizing for size.
 */

#include <linux/export.h>
#include <linux/types.h>
#include <asm/asm-prototypes.h>

union ti {
	__int128_t val;
	struct {
		u64 low;
		u64 high;
	};
};

noinstr __int128_t __ashlti3(__int128_t a, int shift)
{
	union ti ti = { .val = a };

	if (!shift)
		return ti.val;
	if (shift < 64) {
		ti.high = (ti.high << shift) | (ti.low >> (64 - shift));
		ti.low <<= shift;
	} else {
		ti.high = ti.low << (shift - 64);
		ti.low = 0;
	}
	return ti.val;
}
EXPORT_SYMBOL(__ashlti3);

noinstr __int128_t __ashrti3(__int128_t a, int shift)
{
	union ti ti = { .val = a };

	if (!shift)
		return ti.val;
	if (shift < 64) {
		ti.low = (ti.low >> shift) | (ti.high << (64 - shift));
		ti.high = (s64)ti.high >> shift;
	} else {
		ti.low = (s64)ti.high >> (shift - 64);
		ti.high = (s64)ti.high >> 63;
	}
	return ti.val;
}
EXPORT_SYMBOL(__ashrti3);

noinstr __int128_t __lshrti3(__int128_t a, int shift)
{
	union ti ti = { .val = a };

	if (!shift)
		return ti.val;
	if (shift < 64) {
		ti.low = (ti.low >> shift) | (ti.high << (64 - shift));
		ti.high >>= shift;
	} else {
		ti.low = ti.high >> (shift - 64);
		ti.high = 0;
	}
	return ti.val;
}
EXPORT_SYMBOL(__lshrti3);