os/kernelhwsrv/kernel/eka/euser/epoc/arm/uc_i64.cia
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\euser\epoc\arm\uc_i64.cia
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <u32std.h>
sl@0
    19
#include <e32math.h>
sl@0
    20
sl@0
    21
#if defined(__GCC32__)
sl@0
    22
extern "C" void __division_by_zero();
sl@0
    23
#define DIV_BY_ZERO " __division_by_zero "
sl@0
    24
#elif defined(__ARMCC__)
sl@0
    25
extern "C" void __rt_div0 (void);
sl@0
    26
#define DIV_BY_ZERO " __cpp(__rt_div0) "
sl@0
    27
#endif
sl@0
    28
sl@0
    29
sl@0
    30
sl@0
    31
sl@0
    32
EXPORT_C __NAKED__ void Math::Mul64(Int64 /*aX*/, Int64 /*aY*/, Int64& /*aOutH*/, Uint64& /*aOutL*/)
sl@0
    33
/**
sl@0
    34
Multiply aX by aY to generate a 128 bit result.
sl@0
    35
sl@0
    36
The high order 64 bits of this calculation are stored in aOutH,
sl@0
    37
and the low order 64 bits are stored in aOutL.
sl@0
    38
sl@0
    39
@param aX     The first 64-bit operand.
sl@0
    40
@param aY     The second 64-bit operand.
sl@0
    41
@param aOutH  The high order 64 bits of the result.
sl@0
    42
@param aOutL  The low order  64 bits of the result.
sl@0
    43
*/
sl@0
    44
	{
sl@0
    45
	// Enter with r1:r0=aX, r3:r2=aY, [sp]=&aOutH, [sp+4]=&aOutL
sl@0
    46
	asm("stmfd sp!, {r4-r5,lr} ");
sl@0
    47
	asm("umull r4, r5, r0, r2 ");	// r5:r4 = x0 * y0
sl@0
    48
	asm("mov r12, #0 ");			// clear r12 initially
sl@0
    49
	asm("umlal r5, r12, r0, r3 ");	// r12:r5:r4 = x0 * y
sl@0
    50
	asm("mov r14, #0 ");			// clear r14 initially
sl@0
    51
	asm("smlal r12, r14, r1, r3 ");	// r14:r12:r5:r4 = x0 * y + (x1*y1)<<64
sl@0
    52
	asm("and r3, r0, r3, asr #32 ");	// if aY<0, r3=x0 else r3=0
sl@0
    53
	asm("and r0, r2, r1, asr #32 ");	// if aX<0, r0=y0 else r0=0
sl@0
    54
	asm("subs r12, r12, r3 ");
sl@0
    55
	asm("sbcs r14, r14, #0 ");
sl@0
    56
	asm("subs r12, r12, r0 ");
sl@0
    57
	asm("sbcs r14, r14, #0 ");
sl@0
    58
	asm("umull r0, r3, r1, r2 ");	// r3:r0 = x1 * y0
sl@0
    59
	asm("ldr r1, [sp, #12] ");		// r1=&aOutH
sl@0
    60
	asm("ldr r2, [sp, #16] ");		// r1=&aOutL
sl@0
    61
	asm("adds r5, r5, r0 ");		// shift left by 32 and add to give final result
sl@0
    62
	asm("adcs r12, r12, r3 ");
sl@0
    63
	asm("adcs r14, r14, #0 ");		// final result now in r14:r12:r5:r4
sl@0
    64
	asm("stmia r2, {r4,r5} ");		// store low 64
sl@0
    65
	asm("stmia r1, {r12,r14} ");	// store high 64
sl@0
    66
	__POPRET("r4-r5,");
sl@0
    67
	}
sl@0
    68
sl@0
    69
sl@0
    70
sl@0
    71
sl@0
    72
EXPORT_C __NAKED__ void Math::UMul64(Uint64 /*aX*/, Uint64 /*aY*/, Uint64& /*aOutH*/, Uint64& /*aOutL*/)
sl@0
    73
/**
sl@0
    74
Multiply aX by aY to generate a 128 bit result.
sl@0
    75
sl@0
    76
The high order 64 bits of this calculation are stored in aOutH,
sl@0
    77
and the low order 64 bits are stored in aOutL.
sl@0
    78
sl@0
    79
@param aX     The first 64-bit operand.
sl@0
    80
@param aY     The second 64-bit operand.
sl@0
    81
@param aOutH  The high order 64 bits of the result.
sl@0
    82
@param aOutL  The low order  64 bits of the result.
sl@0
    83
*/
sl@0
    84
	{
sl@0
    85
	// Enter with r1:r0=aX, r3:r2=aY, [sp]=&aOutH, [sp+4]=&aOutL
sl@0
    86
	asm("stmfd sp!, {r4-r5,lr} ");
sl@0
    87
	asm("umull r4, r5, r0, r2 ");	// r5:r4 = x0 * y0
sl@0
    88
	asm("mov r12, #0 ");			// clear r12 initially
sl@0
    89
	asm("umlal r5, r12, r0, r3 ");	// r12:r5:r4 = x0 * y
sl@0
    90
	asm("mov r14, #0 ");			// clear r14 initially
sl@0
    91
	asm("umlal r12, r14, r1, r3 ");	// r14:r12:r5:r4 = x0 * y + (x1*y1)<<64
sl@0
    92
									// r0, r3 no longer required
sl@0
    93
	asm("umull r0, r3, r1, r2 ");	// r3:r0 = x1 * y0
sl@0
    94
	asm("ldr r1, [sp, #12] ");		// r1=&aOutH
sl@0
    95
	asm("ldr r2, [sp, #16] ");		// r1=&aOutL
sl@0
    96
	asm("adds r5, r5, r0 ");		// shift left by 32 and add to give final result
sl@0
    97
	asm("adcs r12, r12, r3 ");
sl@0
    98
	asm("adcs r14, r14, #0 ");		// final result now in r14:r12:r5:r4
sl@0
    99
	asm("stmia r2, {r4,r5} ");		// store low 64
sl@0
   100
	asm("stmia r1, {r12,r14} ");	// store high 64
sl@0
   101
	__POPRET("r4-r5,");
sl@0
   102
	}
sl@0
   103
sl@0
   104
sl@0
   105
sl@0
   106
sl@0
   107
EXPORT_C __NAKED__ Int64 Math::DivMod64(Int64 /*aDividend*/, Int64 /*aDivisor*/, Int64& /*aRemainder*/)
sl@0
   108
/**
sl@0
   109
Divides aDividend by aDivisor.
sl@0
   110
sl@0
   111
The quotient is returned, and the remainder is stored in aRemainder.
sl@0
   112
The remainder has same sign as the dividend.
sl@0
   113
sl@0
   114
@param aDividend The 64-bit dividend.
sl@0
   115
@param aDivisor  The 64-bit divisor.
sl@0
   116
@param aRemainder The 64-bit remainder.
sl@0
   117
sl@0
   118
@return The 64-bit quotient.
sl@0
   119
*/
sl@0
   120
	{
sl@0
   121
	// Enter with: r1:r0=dividend, r3:r2=divisor, [sp]=&aRemainder
sl@0
   122
	// Return quotient in r1:r0
sl@0
   123
	asm("stmfd sp!, {r4-r8,lr} ");
sl@0
   124
	__EH_FRAME_PUSH2(r4-r8,lr)
sl@0
   125
	asm("mov r8, r1, asr #1 ");		// r8 bit 31 =  r8 bit 30 = dividend sign
sl@0
   126
	asm("eor r8, r8, r3, lsr #1 ");	// r8 bit 31 = dividend sign, r8 bit 30 = quotient sign
sl@0
   127
	asm("cmp r1, #0 ");
sl@0
   128
	asm("bpl 1f ");
sl@0
   129
	asm("rsbs r0, r0, #0 ");		// r1:r0=ABS(dividend)
sl@0
   130
	asm("rscs r1, r1, #0 ");
sl@0
   131
	asm("1: ");
sl@0
   132
	asm("cmp r3, #0 ");
sl@0
   133
	asm("bpl 2f ");
sl@0
   134
	asm("rsbs r2, r2, #0 ");		// r3:r2=ABS(divisor)
sl@0
   135
	asm("rscs r3, r3, #0 ");
sl@0
   136
	asm("2: ");
sl@0
   137
#ifndef __EABI__
sl@0
   138
	asm(".extern UDiv01 ");
sl@0
   139
	asm("bl UDiv01 ");				// do division, quotient->r5:r4, rem->r6:r3
sl@0
   140
	asm("mov r2, r3");				// move to make regs same as EABI function
sl@0
   141
	asm("mov r0, r4");
sl@0
   142
	asm("mov r1, r5");
sl@0
   143
	asm("mov r3, r6");
sl@0
   144
#else //__EABI__
sl@0
   145
	asm(".extern __aeabi_uldivmod ");
sl@0
   146
	asm("bl __aeabi_uldivmod ");	// do division, quotient->r1:r0, rem->r3:r2
sl@0
   147
#endif //__EABI__
sl@0
   148
	asm("add ip, r8, r8 ");			// ip bit 31 = quotient sign
sl@0
   149
	asm("ldr r6, [sp, #24] ");		// r6 = &aRemainder
sl@0
   150
	asm("eors r4, r0, ip, asr #32 ");	// quotient into r5:r4, inverted if quotient -ve
sl@0
   151
	asm("eors r5, r1, ip, asr #32 ");
sl@0
   152
	asm("adcs r0, r4, #0 ");		// if quotient -ve, add 1 whilst moving back to r1:r0
sl@0
   153
	asm("adcs r1, r5, #0 ");
sl@0
   154
	asm("cmp r8, #0 ");
sl@0
   155
	asm("bpl 3f ");
sl@0
   156
	asm("rsbs r2, r2, #0 ");		// if dividend -ve, negate remainder
sl@0
   157
	asm("rscs r3, r3, #0 ");
sl@0
   158
	asm("3: ");
sl@0
   159
	asm("stmia r6, {r2,r3} ");		// store remainder
sl@0
   160
	__POPRET("r4-r8,");
sl@0
   161
	}
sl@0
   162
sl@0
   163
sl@0
   164
sl@0
   165
sl@0
   166
EXPORT_C __NAKED__ Uint64 Math::UDivMod64(Uint64 /*aDividend*/, Uint64 /*aDivisor*/, Uint64& /*aRemainder*/)
sl@0
   167
/**
sl@0
   168
Divides aDividend by aDivisor.
sl@0
   169
sl@0
   170
The quotient is returned, and the remainder is stored in aRemainder.
sl@0
   171
sl@0
   172
@param aDividend The 64-bit dividend.
sl@0
   173
@param aDivisor  The 64-bit divisor.
sl@0
   174
@param aRemainder The 64-bit remainder.
sl@0
   175
sl@0
   176
@return The 64-bit quotient.
sl@0
   177
*/
sl@0
   178
	{
sl@0
   179
	// Enter with: r1:r0=dividend, r3:r2=divisor, [sp]=&aRemainder
sl@0
   180
	// Return quotient in r1:r0
sl@0
   181
#ifdef __EABI__
sl@0
   182
	// need to keep sp 8-byte aligned
sl@0
   183
	asm("stmfd sp!, {r4-r8,lr} ");
sl@0
   184
	__EH_FRAME_PUSH2(r4-r8,lr)
sl@0
   185
#else
sl@0
   186
	asm("stmfd sp!, {r4-r7,lr} ");
sl@0
   187
#endif
sl@0
   188
sl@0
   189
#ifndef __EABI__
sl@0
   190
	asm(".extern UDiv01 ");
sl@0
   191
	asm("bl UDiv01 ");				// do division, quotient->r5:r4, rem->r6:r3
sl@0
   192
	asm("mov r2, r3");				// move to make regs same as EABI function
sl@0
   193
	asm("mov r0, r4");
sl@0
   194
	asm("mov r1, r5");
sl@0
   195
	asm("mov r3, r6");
sl@0
   196
#else //__EABI__
sl@0
   197
	asm("bl __aeabi_uldivmod ");	// do division, quotient->r1:r0, rem->r3:r2
sl@0
   198
#endif //__EABI__
sl@0
   199
sl@0
   200
#ifdef __EABI__
sl@0
   201
	asm("ldr r6, [sp, #24] ");		// r6 = &aRemainder
sl@0
   202
#else 
sl@0
   203
	asm("ldr r6, [sp, #20] ");		// r6 = &aRemainder
sl@0
   204
#endif 
sl@0
   205
sl@0
   206
	asm("stmia r6, {r2,r3} ");		// store remainder
sl@0
   207
sl@0
   208
#ifdef __EABI__
sl@0
   209
	__POPRET("r4-r8,");
sl@0
   210
#else
sl@0
   211
	__POPRET("r4-r7,");
sl@0
   212
#endif
sl@0
   213
	}