]> git.saurik.com Git - apple/libc.git/blame_incremental - gen.subproj/ppc.subproj/abs.s
Libc-166.tar.gz
[apple/libc.git] / gen.subproj / ppc.subproj / abs.s
... / ...
CommitLineData
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1992,1997 NeXT Software, Inc. All rights reserved.
23 *
24 * File: libc/gen/ppc/abs.s
25 * Author: Derek B Clegg, NeXT Software, Inc.
26 *
27 * HISTORY
28 * 24-Jan-1997 Umesh Vaishampayan (umeshv@NeXT.com)
29 * Ported to PPC.
30 * 10-Nov-92 Derek B Clegg (dclegg@next.com)
31 * Created.
32 * 13-Jan-93 Derek B Clegg (dclegg@next.com)
33 * Optimized.
34 *
35 * ANSI X3.159-1989:
36 * int abs(int j);
37 *
38 * Description:
39 * The `abs' function computes the absolute value of an integer `j'.
40 * If the result cannot be represented, the behavior is undefined.
41 * Returns:
42 * The `abs' function returns the absolute value.
43 */
44#include <architecture/ppc/asm_help.h>
45#include <architecture/ppc/pseudo_inst.h>
46
47/* We calculate abs(x) as
48 * s = x >> 31;
49 * y = x + s;
50 * return y ^ s;
51 *
52 * If x >= 0, then s = 0, so clearly we return x. On the other hand, if
53 * x < 0, then we may write x as ~z + 1, where z = -x. In this case,
54 * s = -1, so y = x - 1 = ~z, and hence we return -1 ^ (x - 1) = -1 ^ ~z
55 * = z = -x.
56 */
57LEAF(_abs)
58 srawi a1,a0,31
59 add a2,a1,a0
60 xor a0,a2,a1
61 blr
62END(_abs)