Path: blob/main/contrib/llvm-project/libcxx/src/filesystem/int128_builtins.cpp
35231 views
/*===-- int128_builtins.cpp - Implement __muloti4 --------------------------===1*2* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3* See https://llvm.org/LICENSE.txt for license information.4* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5*6* ===----------------------------------------------------------------------===7*8* This file implements __muloti4, and is stolen from the compiler_rt library.9*10* FIXME: we steal and re-compile it into filesystem, which uses __int128_t,11* and requires this builtin when sanitized. See llvm.org/PR3064312*13* ===----------------------------------------------------------------------===14*/15#include <__config>16#include <climits>1718#if !defined(_LIBCPP_HAS_NO_INT128)1920extern "C" __attribute__((no_sanitize("undefined"))) _LIBCPP_EXPORTED_FROM_ABI __int128_t21__muloti4(__int128_t a, __int128_t b, int* overflow) {22const int N = (int)(sizeof(__int128_t) * CHAR_BIT);23const __int128_t MIN = (__int128_t)1 << (N - 1);24const __int128_t MAX = ~MIN;25*overflow = 0;26__int128_t result = a * b;27if (a == MIN) {28if (b != 0 && b != 1)29*overflow = 1;30return result;31}32if (b == MIN) {33if (a != 0 && a != 1)34*overflow = 1;35return result;36}37__int128_t sa = a >> (N - 1);38__int128_t abs_a = (a ^ sa) - sa;39__int128_t sb = b >> (N - 1);40__int128_t abs_b = (b ^ sb) - sb;41if (abs_a < 2 || abs_b < 2)42return result;43if (sa == sb) {44if (abs_a > MAX / abs_b)45*overflow = 1;46} else {47if (abs_a > MIN / -abs_b)48*overflow = 1;49}50return result;51}5253#endif545556