1353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2353358Sdim// See https://llvm.org/LICENSE.txt for license information.
3353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4276789Sdim
5276789Sdim#include "../assembly.h"
6276789Sdim
7276789Sdim// di_int __lshrdi3(di_int input, int count);
8276789Sdim
9276789Sdim// This routine has some extra memory traffic, loading the 64-bit input via two
10276789Sdim// 32-bit loads, then immediately storing it back to the stack via a single 64-bit
11276789Sdim// store.  This is to avoid a write-small, read-large stall.
12276789Sdim// However, if callers of this routine can be safely assumed to store the argument
13276789Sdim// via a 64-bt store, this is unnecessary memory traffic, and should be avoided.
14276789Sdim// It can be turned off by defining the TRUST_CALLERS_USE_64_BIT_STORES macro.
15276789Sdim
16276789Sdim#ifdef __i386__
17276789Sdim#ifdef __SSE2__
18276789Sdim
19276789Sdim.text
20276789Sdim.balign 4
21276789SdimDEFINE_COMPILERRT_FUNCTION(__lshrdi3)
22276789Sdim	movd	  12(%esp),		%xmm2	// Load count
23276789Sdim#ifndef TRUST_CALLERS_USE_64_BIT_STORES
24276789Sdim	movd	   4(%esp),		%xmm0
25276789Sdim	movd	   8(%esp),		%xmm1
26276789Sdim	punpckldq	%xmm1,		%xmm0	// Load input
27276789Sdim#else
28276789Sdim	movq	   4(%esp),		%xmm0	// Load input
29276789Sdim#endif
30276789Sdim	psrlq		%xmm2,		%xmm0	// shift input by count
31276789Sdim	movd		%xmm0,		%eax
32276789Sdim	psrlq		$32,		%xmm0
33276789Sdim	movd		%xmm0,		%edx
34276789Sdim	ret
35276789SdimEND_COMPILERRT_FUNCTION(__lshrdi3)
36276789Sdim
37276789Sdim#else // Use GPRs instead of SSE2 instructions, if they aren't available.
38276789Sdim
39276789Sdim.text
40276789Sdim.balign 4
41276789SdimDEFINE_COMPILERRT_FUNCTION(__lshrdi3)
42276789Sdim	movl	  12(%esp),		%ecx	// Load count
43276789Sdim	movl	   8(%esp),		%edx	// Load high
44276789Sdim	movl	   4(%esp),		%eax	// Load low
45353358Sdim
46276789Sdim	testl		$0x20,		%ecx	// If count >= 32
47276789Sdim	jnz			1f					//    goto 1
48276789Sdim
49276789Sdim	shrdl		%cl, %edx,	%eax	// right shift low by count
50276789Sdim	shrl		%cl,		%edx	// right shift high by count
51276789Sdim	ret
52353358Sdim
53276789Sdim1:	movl		%edx,		%eax	// Move high to low
54276789Sdim	xorl		%edx,		%edx	// clear high
55276789Sdim	shrl		%cl,		%eax	// shift low by count - 32
56276789Sdim	ret
57276789SdimEND_COMPILERRT_FUNCTION(__lshrdi3)
58276789Sdim
59276789Sdim#endif // __SSE2__
60276789Sdim#endif // __i386__
61309124Sdim
62309124SdimNO_EXEC_STACK_DIRECTIVE
63309124Sdim
64