11ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher/*===-- muloti4.c - Implement __muloti4 -----------------------------------=== 21ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * 31ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * The LLVM Compiler Infrastructure 41ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * 51ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * This file is dual licensed under the MIT and the University of Illinois Open 61ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * Source Licenses. See LICENSE.TXT for details. 71ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * 81ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * ===----------------------------------------------------------------------=== 91ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * 101ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * This file implements __muloti4 for the compiler_rt library. 111ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * 121ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher * ===----------------------------------------------------------------------=== 131ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher */ 141ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher 151ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher#include "int_lib.h" 161ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher 177f2d7c75e713d778106d01a54e7aef40227bbf2dChandler Carruth#if __x86_64 187f2d7c75e713d778106d01a54e7aef40227bbf2dChandler Carruth 191ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher/* Returns: a * b */ 201ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher 211ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher/* Effects: sets *overflow to 1 if a * b overflows */ 221ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher 231ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopherti_int 241ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher__muloti4(ti_int a, ti_int b, int* overflow) 251ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher{ 261ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher const int N = (int)(sizeof(ti_int) * CHAR_BIT); 271ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher const ti_int MIN = (ti_int)1 << (N-1); 281ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher const ti_int MAX = ~MIN; 291ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher *overflow = 0; 301ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher ti_int result = a * b; 311ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (a == MIN) 321ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher { 331ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (b != 0 && b != 1) 341ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher *overflow = 1; 351ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher return result; 361ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher } 371ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (b == MIN) 381ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher { 391ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (a != 0 && a != 1) 401ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher *overflow = 1; 411ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher return result; 421ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher } 431ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher ti_int sa = a >> (N - 1); 441ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher ti_int abs_a = (a ^ sa) - sa; 451ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher ti_int sb = b >> (N - 1); 461ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher ti_int abs_b = (b ^ sb) - sb; 471ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (abs_a < 2 || abs_b < 2) 481ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher return result; 491ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (sa == sb) 501ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher { 511ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (abs_a > MAX / abs_b) 521ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher *overflow = 1; 531ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher } 541ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher else 551ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher { 561ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher if (abs_a > MIN / -abs_b) 571ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher *overflow = 1; 581ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher } 591ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher return result; 601ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher} 611ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher 621ace4055f79f304750839d73c46bbcaeb994f1b5Eric Christopher#endif 63