]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/CompilerIntrinsicsLib/Arm/moddi3.c
Add some missing 64-bit math functions for gcc
[mirror_edk2.git] / ArmPkg / Library / CompilerIntrinsicsLib / Arm / moddi3.c
CommitLineData
2ef2b01e
A
1/** @file
2 Compiler intrinsic for 64-bit mod, ported from LLVM code.
3
4 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
5
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14**/
15/**
16 University of Illinois/NCSA
17 Open Source License
18
19 Copyright (c) 2003-2008 University of Illinois at Urbana-Champaign.
20 All rights reserved.
21
22 Developed by:
23
24 LLVM Team
25
26 University of Illinois at Urbana-Champaign
27
28 http://llvm.org
29
30 Permission is hereby granted, free of charge, to any person obtaining a copy of
31 this software and associated documentation files (the "Software"), to deal with
32 the Software without restriction, including without limitation the rights to
33 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
34 of the Software, and to permit persons to whom the Software is furnished to do
35 so, subject to the following conditions:
36
37 * Redistributions of source code must retain the above copyright notice,
38 this list of conditions and the following disclaimers.
39
40 * Redistributions in binary form must reproduce the above copyright notice,
41 this list of conditions and the following disclaimers in the
42 documentation and/or other materials provided with the distribution.
43
44 * Neither the names of the LLVM Team, University of Illinois at
45 Urbana-Champaign, nor the names of its contributors may be used to
46 endorse or promote products derived from this Software without specific
47 prior written permission.
48
49 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
51 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52 CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
55 SOFTWARE.
56**/
57
58
59#include "Llvm_int_lib.h"
60
61UINT64 __udivmoddi4(UINT64 a, UINT64 b, UINT64* rem);
62
63// Returns: a % b
64
65INT64
66__moddi3(INT64 a, INT64 b)
67{
68 const int bits_in_dword_m1 = (int)(sizeof(INT64) * CHAR_BIT) - 1;
69 INT64 s = b >> bits_in_dword_m1; // s = b < 0 ? -1 : 0
70 b = (b ^ s) - s; // negate if s == -1
71 s = a >> bits_in_dword_m1; // s = a < 0 ? -1 : 0
72 a = (a ^ s) - s; // negate if s == -1
73 INT64 r;
74 __udivmoddi4(a, b, (UINT64*)&r);
75 return (r ^ s) - s; // negate if s == -1
76}
77