xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/lshrti3.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- lshrti3.c - Implement __lshrti3 -----------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements __lshrti3 for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "int_lib.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifdef CRT_HAS_128BIT
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric // Returns: logical a >> b
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric // Precondition:  0 <= b < bits_in_tword
200b57cec5SDimitry Andric 
__lshrti3(ti_int a,int b)21*06c3fb27SDimitry Andric COMPILER_RT_ABI ti_int __lshrti3(ti_int a, int b) {
220b57cec5SDimitry Andric   const int bits_in_dword = (int)(sizeof(di_int) * CHAR_BIT);
230b57cec5SDimitry Andric   utwords input;
240b57cec5SDimitry Andric   utwords result;
250b57cec5SDimitry Andric   input.all = a;
260b57cec5SDimitry Andric   if (b & bits_in_dword) /* bits_in_dword <= b < bits_in_tword */ {
270b57cec5SDimitry Andric     result.s.high = 0;
280b57cec5SDimitry Andric     result.s.low = input.s.high >> (b - bits_in_dword);
290b57cec5SDimitry Andric   } else /* 0 <= b < bits_in_dword */ {
300b57cec5SDimitry Andric     if (b == 0)
310b57cec5SDimitry Andric       return a;
320b57cec5SDimitry Andric     result.s.high = input.s.high >> b;
330b57cec5SDimitry Andric     result.s.low = (input.s.high << (bits_in_dword - b)) | (input.s.low >> b);
340b57cec5SDimitry Andric   }
350b57cec5SDimitry Andric   return result.all;
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric #endif // CRT_HAS_128BIT
39