xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/ashrti3.c (revision f7f78b3373aafafa211473d7733a8806c0e402c0)
1156cd587Sjoerg /* ===-- ashrti3.c - Implement __ashrti3 -----------------------------------===
2156cd587Sjoerg  *
3156cd587Sjoerg  *                     The LLVM Compiler Infrastructure
4156cd587Sjoerg  *
5156cd587Sjoerg  * This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg  * Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg  *
8156cd587Sjoerg  * ===----------------------------------------------------------------------===
9156cd587Sjoerg  *
10156cd587Sjoerg  * This file implements __ashrti3 for the compiler_rt library.
11156cd587Sjoerg  *
12156cd587Sjoerg  * ===----------------------------------------------------------------------===
13156cd587Sjoerg  */
14156cd587Sjoerg 
15156cd587Sjoerg #include "int_lib.h"
16156cd587Sjoerg 
17156cd587Sjoerg #ifdef CRT_HAS_128BIT
18156cd587Sjoerg 
19156cd587Sjoerg /* Returns: arithmetic a >> b */
20156cd587Sjoerg 
21156cd587Sjoerg /* Precondition:  0 <= b < bits_in_tword */
22156cd587Sjoerg 
23*f7f78b33Sjoerg COMPILER_RT_ABI ti_int
__ashrti3(ti_int a,si_int b)24156cd587Sjoerg __ashrti3(ti_int a, si_int b)
25156cd587Sjoerg {
26156cd587Sjoerg     const int bits_in_dword = (int)(sizeof(di_int) * CHAR_BIT);
27156cd587Sjoerg     twords input;
28156cd587Sjoerg     twords result;
29156cd587Sjoerg     input.all = a;
30156cd587Sjoerg     if (b & bits_in_dword)  /* bits_in_dword <= b < bits_in_tword */
31156cd587Sjoerg     {
32156cd587Sjoerg         /* result.s.high = input.s.high < 0 ? -1 : 0 */
33156cd587Sjoerg         result.s.high = input.s.high >> (bits_in_dword - 1);
34156cd587Sjoerg         result.s.low = input.s.high >> (b - bits_in_dword);
35156cd587Sjoerg     }
36156cd587Sjoerg     else  /* 0 <= b < bits_in_dword */
37156cd587Sjoerg     {
38156cd587Sjoerg         if (b == 0)
39156cd587Sjoerg             return a;
40156cd587Sjoerg         result.s.high  = input.s.high >> b;
41156cd587Sjoerg         result.s.low = (input.s.high << (bits_in_dword - b)) | (input.s.low >> b);
42156cd587Sjoerg     }
43156cd587Sjoerg     return result.all;
44156cd587Sjoerg }
45156cd587Sjoerg 
46156cd587Sjoerg #endif /* CRT_HAS_128BIT */
47