diff -r 000000000000 -r a41df078684a kernel/eka/common/arm/cgcchelp.cia --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kernel/eka/common/arm/cgcchelp.cia Mon Oct 19 15:55:17 2009 +0100 @@ -0,0 +1,639 @@ +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). +// All rights reserved. +// This component and the accompanying materials are made available +// under the terms of the License "Eclipse Public License v1.0" +// which accompanies this distribution, and is available +// at the URL "http://www.eclipse.org/legal/epl-v10.html". +// +// Initial Contributors: +// Nokia Corporation - initial contribution. +// +// Contributors: +// +// Description: +// e32\common\arm\cgcchelp.cia +// +// + +#include "../common.h" +#ifdef __KERNEL_MODE__ +#include "nkern.h" +#endif + +extern "C" { +#ifdef __GCC32__ +EXPORT_C __NAKED__ TInt __divsi3(TInt /*dividend*/,TInt /*divisor*/) +// +// Signed divide of r0 by r1: returns quotient in r0 +// Quotient is truncated (rounded towards zero). +// Destroys r2, r3 and ip +// Negates dividend and divisor, then does an unsigned divide; signs +// get sorted out again at the end. +// +// Have to calculate the sign of the result for the end of the calculation. +// Store this in the LSB of ip which also saves the old lr. +// + { + + asm("STMFD sp!, {lr} "); + asm("ANDS r3, r1, #0x80000000 "); // r3 bit 31=sign of divisor, rest of r3=0 + asm("RSBMI r1, r1, #0 "); // r1=ABS(divisor) + asm("EORS ip, r3, r0, ASR #32 "); // ip bit 31=sign of quotient, all other bits=carry=sign of dividend + asm("RSBCS r0, r0, #0 "); // r0=ABS(dividend) + asm(".EXTERN "); + asm("BL __umodsi3_start "); + asm("MOV r0, r3 "); + asm("CMP ip, #0 "); // test sign of quotient + asm("RSBMI r0, r0, #0 "); // negate if necessary + __POPRET(""); + } + + +EXPORT_C __NAKED__ TInt __modsi3(TInt /*dividend*/,TInt /*divisor*/) +// +// Signed divide of r0 by r1: returns remainder in r0 +// Sign of remainder = sign of dividend. +// Destroys r2, r3 and ip +// Negates dividend and divisor, then does an unsigned divide; signs +// get sorted out again at the end. +// +// Have to save sign of dividend in order to apply sign to remainder +// at the end of the calculation. Store this in the LSB of ip which also +// saves the old lr. +// + { + + asm("STMFD sp!, {lr} "); + asm("MOVS r1, r1 "); + asm("RSBMI r1, r1, #0 "); + asm("MOVS ip, r0 "); + asm("RSBMI r0, r0, #0 "); + asm(".EXTERN "); + asm("BL __umodsi3_start "); + asm("MOVS ip, ip "); + asm("RSBMI r0, r0, #0 "); + __POPRET(""); + } + +EXPORT_C __NAKED__ TUint __udivsi3(TUint /*dividend*/,TUint /*divisor*/) +// +// Unsigned divide of r0 by r1: returns quotient in r0 +// Quotient is truncated (rounded towards zero). +// Destroys r2, r3 and ip +// + { + + asm("MOV ip, lr "); + asm(".EXTERN "); + asm("BL __umodsi3_start "); + asm("MOV r0, r3 "); + __JUMP(,ip); + } + + +EXPORT_C __NAKED__ long long __divdi3(long long /*dividend*/, long long /*divisor*/) +// +// Dividend in r1:r0, divisor in r3:r2, Return quotient in r1:r0 +// + { + asm("stmfd sp!, {r4-r8,lr} "); + asm("eor r8, r1, r3 "); // sign of result into r8 + asm("movs r1, r1 "); + asm("bpl 1f "); + asm("rsbs r0, r0, #0 "); // ABS(dividend) + asm("rsc r1, r1, #0 "); + asm("1: "); + asm("movs r3, r3 "); + asm("bpl 2f "); + asm("rsbs r2, r2, #0 "); // ABS(divisor) + asm("rsc r3, r3, #0 "); + asm("2: "); + asm("bl UDiv01 "); // do the division, result in r4,r5 + asm("eors r0, r4, r8, asr #32 "); // quotient into r1:r0, inverted if quotient -ve + asm("eors r1, r5, r8, asr #32 "); + asm("adcs r0, r0, #0 "); // if quotient -ve, add 1 + asm("adcs r1, r1, #0 "); + __POPRET("r4-r8,"); + } + +EXPORT_C __NAKED__ long long __moddi3(long long /*dividend*/, long long /*divisor*/) /* signed */ + { + asm("stmfd sp!, {r4-r8,lr} "); + asm("movs r8, r1 "); // sign of remainder (=sign of dividend) into r8 + asm("bpl 1f "); + asm("rsbs r0, r0, #0 "); // ABS(dividend) + asm("rsc r1, r1, #0 "); + asm("1: "); + asm("movs r3, r3 "); + asm("bpl 2f "); + asm("rsbs r2, r2, #0 "); // ABS(divisor) + asm("rsc r3, r3, #0 "); + asm("2: "); + asm("bl UDiv01 "); // do the division, remainder in r3,r6 + asm("eors r0, r3, r8, asr #32 "); // remainder into r1:r0, inverted if dividend -ve + asm("eors r1, r6, r8, asr #32 "); + asm("adcs r0, r0, #0 "); // if dividend -ve, add 1 + asm("adcs r1, r1, #0 "); + __POPRET("r4-r8,"); + } + +EXPORT_C __NAKED__ long long __umoddi3(unsigned long long /*dividend*/, unsigned long long /*divisor*/) /* unsigned */ + { + asm("stmfd sp!, {r4-r7,lr} "); + asm("bl UDiv01 "); // do the division, remainder in r6:r3 + asm("mov r0, r3 "); + asm("mov r1, r6 "); + __POPRET("r4-r7,"); + } + +EXPORT_C __NAKED__ long long __ashrdi3(long long /*value*/, unsigned int /*count*/) + { + asm("cmp r2, #63 "); + asm("movhi r2, #63 "); // count>63 same as count=63 + asm("cmp r2, #32 "); + asm("bcs Asr01 "); // jump if shift count >=32 + asm("rsb r12, r2, #32 "); // r12=32-shift count + asm("mov r0, r0, lsr r2 "); // shift ls word right + asm("orr r0, r0, r1, lsl r12 "); // or in bits shifted out of ms word + asm("mov r1, r1, asr r2 "); // shift ms word right + __JUMP(,lr); + asm("Asr01: "); + asm("sub r2, r2, #32 "); // r2=shift count-32 + asm("mov r0, r1, asr r2 "); // ls word = ms word >> (count-32) + asm("mov r1, r1, asr #32 "); // ms word of result=sign extension of r1 + __JUMP(,lr); + } + +EXPORT_C __NAKED__ long long __ashldi3(long long /*value*/, unsigned int /*count*/) + { + asm("cmp r2, #63 "); + asm("movhi r2, #64 "); // count>63 same as count=64 + asm("cmp r2, #32 "); + asm("bcs Asl01 "); // jump if shift count >=32 + asm("rsb r12, r2, #32 "); // r12=32-shift count + asm("mov r1, r1, asl r2 "); // shift ms word left + asm("orr r1, r1, r0, lsr r12 "); // or in bits shifted out of ls word + asm("mov r0, r0, asl r2 "); // shift ls word left + __JUMP(,lr); + asm("Asl01: "); + asm("sub r2, r2, #32 "); // r2=shift count-32 + asm("mov r1, r0, asl r2 "); // result ms word = ls word << (count-32) + asm("mov r0, #0 "); // ls word of result is zero + __JUMP(,lr); + } + +EXPORT_C __NAKED__ unsigned long long __lshrdi3(unsigned long long /*value*/, unsigned int /*count*/) + { + asm("cmp r2, #63 "); + asm("movhi r2, #64 "); // count>63 same as count=64 + asm("cmp r2, #32 "); + asm("bcs Lsr01 "); // jump if shift count >=32 + asm("rsb r12, r2, #32 "); // r12=32-shift count + asm("mov r0, r0, lsr r2 "); // shift ls word right + asm("orr r0, r0, r1, lsl r12 "); // or in bits shifted out of ms word + asm("mov r1, r1, lsr r2 "); // shift ms word right + __JUMP(,lr); + asm("Lsr01: "); + asm("sub r2, r2, #32 "); // r2=shift count-32 + asm("mov r0, r1, lsr r2 "); // ls word = ms word >> (count-32) + asm("mov r1, #0 "); // ms word of result = 0 + __JUMP(,lr); + } + +EXPORT_C __NAKED__ long long __muldi3(long long /*multiplicand*/, long long /*multiplier*/) + { + asm("mul r1, r2, r1 "); // r1=low2*high1 + asm("mov ip, r0 "); // ip=low1 + asm("mla r1, r0, r3, r1 "); // r1+=low1*high2 + asm("mov r0, #0 "); + asm("umlal r0, r1, r2, ip "); // r1:r0 += high1*low1 + __JUMP(,lr); + } + +EXPORT_C __NAKED__ long long __negdi2(long long /*argument*/) + { + asm("rsbs r0, r0, #0 "); // r0=0-r0, set carry + asm("rscs r1, r1, #0 "); // r1=0-r1-(1-C) + __JUMP(,lr); + } + +EXPORT_C __NAKED__ unsigned long long __udivmoddi4 (unsigned long long /*dividend*/, + unsigned long long /*divisor*/, + unsigned long long* /*p_remainder*/) + { + asm("stmfd sp!, {r4-r7,lr} "); + asm("bl UDiv01 "); // do the division, quotient in r5:r4 remainder in r6:r3 + asm("ldr r7, [sp, #20] "); // r7=p_remainder + asm("mov r0, r4 "); // r0=quotient low + asm("stmia r7, {r3,r6} "); // store remainder + asm("mov r1, r5 "); // r0=quotient high + __POPRET("r4-r7,"); + } + +EXPORT_C __NAKED__ int __cmpdi2(long long /*a*/, long long /*b*/) + { + // return 0 if ab + asm("subs r0, r2, r0 "); + asm("sbcs r1, r3, r1 "); // r1:r0 = b-a, set flags + asm("movlt r0, #2 "); // if bb + asm("cmp r1, r3 "); + asm("cmpeq r0, r2 "); // compare r1:r0 - r3:r2 + asm("movhi r0, #2 "); // r0=2 if a>b + asm("moveq r0, #1 "); // r0=1 if a=b + asm("movlo r0, #0 "); // r0=0 if a= 2^32, so quotient < 2^32 + // Use 64 bit accumulator, 32 bit quotient + asm("udiv64a: "); + asm("mov r4, #0 "); // quotient in r4, use r1, r6 as accumulator + asm("mov r6, #0 "); + asm("mov r5, #8 "); // do 32 iterations + asm("udiv64d: "); + asm("adds r0, r0, r0 "); // shift dividend left into acc + asm("adcs r1, r1, r1 "); + asm("adcs r6, r6, r6 "); + asm("subs r7, r1, r2 "); // subtract divisor from acc, result into r7,r12 + asm("sbcs r12, r6, r3 "); + asm("adc r4, r4, r4 "); // shift result bit left into quotient + asm("movcs r1, r7 "); // if no borrow, update acc + asm("movcs r6, r12 "); + asm("adds r0, r0, r0 "); // shift dividend left into acc + asm("adcs r1, r1, r1 "); + asm("adcs r6, r6, r6 "); + asm("subs r7, r1, r2 "); // subtract divisor from acc, result into r7,r12 + asm("sbcs r12, r6, r3 "); + asm("adc r4, r4, r4 "); // shift result bit left into quotient + asm("movcs r1, r7 "); // if no borrow, update acc + asm("movcs r6, r12 "); + asm("adds r0, r0, r0 "); // shift dividend left into acc + asm("adcs r1, r1, r1 "); + asm("adcs r6, r6, r6 "); + asm("subs r7, r1, r2 "); // subtract divisor from acc, result into r7,r12 + asm("sbcs r12, r6, r3 "); + asm("adc r4, r4, r4 "); // shift result bit left into quotient + asm("movcs r1, r7 "); // if no borrow, update acc + asm("movcs r6, r12 "); + asm("adds r0, r0, r0 "); // shift dividend left into acc + asm("adcs r1, r1, r1 "); + asm("adcs r6, r6, r6 "); + asm("subs r7, r1, r2 "); // subtract divisor from acc, result into r7,r12 + asm("sbcs r12, r6, r3 "); + asm("adc r4, r4, r4 "); // shift result bit left into quotient + asm("movcs r1, r7 "); // if no borrow, update acc + asm("movcs r6, r12 "); + asm("subs r5, r5, #1 "); // loop + asm("bne udiv64d "); + asm("mov r3, r1 "); // remainder in r3,r6 + __JUMP(,lr); + + asm("udiv64_divby0: "); + asm("stmfd sp!, {r11,lr} "); + __EH_FRAME_PUSH2(r11,lr) + asm("mov r11, sp "); + asm("bic sp, sp, #4 "); + asm("bl " DIV_BY_ZERO); + asm("mov sp, r11 "); + __POPRET("r11,"); + } + +} +