]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/CompilerIntrinsicsLib/Arm/divsi3.c
ArmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / ArmPkg / Library / CompilerIntrinsicsLib / Arm / divsi3.c
1 /** @file
2 Compiler intrinsic for 32--bit unsigned division, ported from LLVM code.
3
4
5 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10 /**
11 University of Illinois/NCSA
12 Open Source License
13
14 Copyright (c) 2003-2008 University of Illinois at Urbana-Champaign.
15 All rights reserved.
16
17 Developed by:
18
19 LLVM Team
20
21 University of Illinois at Urbana-Champaign
22
23 http://llvm.org
24
25 Permission is hereby granted, free of charge, to any person obtaining a copy of
26 this software and associated documentation files (the "Software"), to deal with
27 the Software without restriction, including without limitation the rights to
28 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 of the Software, and to permit persons to whom the Software is furnished to do
30 so, subject to the following conditions:
31
32 * Redistributions of source code must retain the above copyright notice,
33 this list of conditions and the following disclaimers.
34
35 * Redistributions in binary form must reproduce the above copyright notice,
36 this list of conditions and the following disclaimers in the
37 documentation and/or other materials provided with the distribution.
38
39 * Neither the names of the LLVM Team, University of Illinois at
40 Urbana-Champaign, nor the names of its contributors may be used to
41 endorse or promote products derived from this Software without specific
42 prior written permission.
43
44 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
46 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47 CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
50 SOFTWARE.
51 **/
52
53
54 #include "Llvm_int_lib.h"
55
56 UINT32 __udivsi3(UINT32 n, UINT32 d);
57
58 // Returns: a / b
59
60 INT32
61 __divsi3(INT32 a, INT32 b)
62 {
63 const int bits_in_word_m1 = (int)(sizeof(INT32) * CHAR_BIT) - 1;
64 INT32 s_a = a >> bits_in_word_m1; // s_a = a < 0 ? -1 : 0
65 INT32 s_b = b >> bits_in_word_m1; // s_b = b < 0 ? -1 : 0
66 a = (a ^ s_a) - s_a; // negate if s_a == -1
67 b = (b ^ s_b) - s_b; // negate if s_b == -1
68 s_a ^= s_b; // sign of quotient
69 return (__udivsi3(a, b) ^ s_a) - s_a; // negate if s_a == -1
70 }
71
72