xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/absvsi2.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- absvsi2.c - Implement __absvsi2 -----------------------------------===//
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 __absvsi2 for the compiler_rt library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "int_lib.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric // Returns: absolute value
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric // Effects: aborts if abs(x) < 0
180b57cec5SDimitry Andric 
__absvsi2(si_int a)190b57cec5SDimitry Andric COMPILER_RT_ABI si_int __absvsi2(si_int a) {
200b57cec5SDimitry Andric   const int N = (int)(sizeof(si_int) * CHAR_BIT);
21*06c3fb27SDimitry Andric   if (a == ((si_int)((su_int)1 << (N - 1))))
220b57cec5SDimitry Andric     compilerrt_abort();
230b57cec5SDimitry Andric   const si_int t = a >> (N - 1);
240b57cec5SDimitry Andric   return (a ^ t) - t;
250b57cec5SDimitry Andric }
26