1 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 // See https://llvm.org/LICENSE.txt for license information.
3 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 #include "../assembly.h"
7 // di_int __ashrdi3(di_int input, int count);
14 DEFINE_COMPILERRT_FUNCTION(__ashrdi3)
15 movd 12(%esp), %xmm2 // Load count
17 #ifndef TRUST_CALLERS_USE_64_BIT_STORES
20 punpckldq %xmm1, %xmm0 // Load input
22 movq 4(%esp), %xmm0 // Load input
25 psrlq %xmm2, %xmm0 // unsigned shift input by count
27 testl %eax, %eax // check the sign-bit of the input
28 jns 1f // early out for positive inputs
30 // If the input is negative, we need to construct the shifted sign bit
31 // to or into the result, as xmm does not have a signed right shift.
32 pcmpeqb %xmm1, %xmm1 // -1ULL
33 psrlq $58, %xmm1 // 0x3f
34 pandn %xmm1, %xmm2 // 63 - count
35 pcmpeqb %xmm1, %xmm1 // -1ULL
36 psubq %xmm1, %xmm2 // 64 - count
37 psllq %xmm2, %xmm1 // -1 << (64 - count) = leading sign bits
40 // Move the result back to the general purpose registers and return
45 END_COMPILERRT_FUNCTION(__ashrdi3)
47 #else // Use GPRs instead of SSE2 instructions, if they aren't available.
51 DEFINE_COMPILERRT_FUNCTION(__ashrdi3)
52 movl 12(%esp), %ecx // Load count
53 movl 8(%esp), %edx // Load high
54 movl 4(%esp), %eax // Load low
56 testl $0x20, %ecx // If count >= 32
59 shrdl %cl, %edx, %eax // right shift low by count
60 sarl %cl, %edx // right shift high by count
63 1: movl %edx, %eax // Move high to low
64 sarl $31, %edx // clear high
65 sarl %cl, %eax // shift low by count - 32
67 END_COMPILERRT_FUNCTION(__ashrdi3)
72 NO_EXEC_STACK_DIRECTIVE