Home | History | Annotate | Line # | Download | only in quad
__aeabi_ldivmod.S revision 1.4
      1 /*-
      2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <machine/asm.h>
     31 
     32 RCSID("$NetBSD: __aeabi_ldivmod.S,v 1.4 2013/05/05 19:02:22 skrll Exp $")
     33 
     34 ENTRY(__aeabi_ldivmod)
     35 	push	{r4-r5, sl, lr}
     36 #define	NEG	r5
     37 	mov	NEG, #0
     38 
     39 #ifdef __ARMEB__
     40 #define	ALO	r1	/* incoming numerator, outgoing quotient */
     41 #define	AHI	r0	/* incoming numerator, outgoing quotient */
     42 #define	BLO	r3	/* incoming denominator, outgoing remainder */
     43 #define	BHI	r2	/* incoming denominator, outgoing remainder */
     44 #else
     45 #define	ALO	r0	/* incoming numerator, outgoing quotient */
     46 #define	AHI	r1	/* incoming numerator, outgoing quotient */
     47 #define	BLO	r2	/* incoming denominator, outgoing remainder */
     48 #define	BHI	r3	/* incoming denominator, outgoing remainder */
     49 #endif
     50 
     51 	cmp	BHI, #0
     52 	bge	2f
     53 	eor	NEG, NEG, #1	/* flip quotient sign */
     54 	bl	.Lnegate_b
     55 	bcs	.Lmaxdenom
     56 
     57 2:
     58 	cmp	AHI, #0
     59 	/* bge	3f */
     60 	eorlt	NEG, NEG, #3	/* flip quotient sign, flip remainder sign */
     61 	bllt	.Lnegate_a
     62 3:
     63 	/*
     64 	 * Arguments are setup, allocate some stack for the remainder
     65 	 * and call __qdivrem for the heavy lifting.
     66 	 */
     67 	sub	sp, sp, #16
     68 	add	ip, sp, #8
     69 	str	ip, [sp]
     70 	bl	PLT_SYM(__qdivrem)
     71 	add	sp, sp, #8
     72 
     73 	teq	NEG, #0		/* any signs to flip? */
     74 	/*
     75 	 * The quotient is already in the right place and neither value
     76 	 * needs its sign flipped.
     77 	 */
     78 	popeq	{r2-r5, sl, lr}
     79 	RETc(eq)
     80 
     81 	pop	{r2, r3}
     82 	tst	NEG, #2		/* does remainder need to be negative? */
     83 	blne	.Lnegate_b
     84 	tst	NEG, #1		/* does quotient need to be negative? */
     85 	blne	.Lnegate_a
     86 	pop	{r4-r5, sl, lr}
     87 	RET
     88 
     89 .Lnegate_a:
     90         rsbs	ALO, ALO, #0
     91         rsc	AHI, AHI, #0
     92 	RET
     93 
     94 .Lnegate_b:
     95         rsbs	BLO, BLO, #0
     96         rsc	BHI, BHI, #0
     97 	RET
     98 
     99 .Lmaxdenom:
    100 	/*
    101 	 * We had a carry so the denominator must have INT64_MIN
    102 	 * Also BLO and BHI never changed values so we can use
    103 	 * them to see if the numerator has the same value.  We
    104 	 * don't have to worry about sign.
    105 	 */
    106 	teq	BHI, AHI
    107 	teqeq	BLO, ALO
    108 	bne	1f
    109 
    110 	/*
    111 	 * They were equal, so we return a quotient of 1 and remainder of 0.
    112 	 */
    113 	mov	ALO, #1
    114 	mov	AHI, #0
    115 	mov	BLO, #0
    116 	mov	BHI, #0
    117 	pop	{r4-r5, sl, lr}
    118 	RET
    119 
    120 	/*
    121 	 * Our remainder must be the numerator and our quotient is 0.
    122 	 */
    123 1:	mov	BLO, ALO
    124 	mov	BHI, AHI
    125 	mov	ALO, #0
    126 	mov	AHI, #0
    127 	pop	{r4-r5, sl, lr}
    128 	RET
    129 END(__aeabi_ldivmod)
    130