crypto/weakcrypto/source/bigint/gcchelp.cpp
branchRCL_3
changeset 61 641f389e9157
equal deleted inserted replaced
60:f18401adf8e1 61:641f389e9157
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 
       
    24 #include <kernel\u32std.h>	// need this for __NAKED__, __JUMP and __POPRET
       
    25 
       
    26 extern "C" {
       
    27 #ifdef __GCC32__
       
    28 __NAKED__ long long __divdi3(long long /*dividend*/, long long /*divisor*/)
       
    29 //
       
    30 // Dividend in r1:r0, divisor in r3:r2, Return quotient in r1:r0
       
    31 //
       
    32 	{
       
    33 	asm("stmfd sp!, {r4-r8,lr} ");
       
    34 	asm("eor r8, r1, r3 ");				// sign of result into r8
       
    35 	asm("movs r1, r1 ");
       
    36 	asm("bpl 1f ");
       
    37 	asm("rsbs r0, r0, #0 ");			// ABS(dividend)
       
    38 	asm("rsc r1, r1, #0 ");
       
    39 	asm("1: ");
       
    40 	asm("movs r3, r3 ");
       
    41 	asm("bpl 2f ");
       
    42 	asm("rsbs r2, r2, #0 ");			// ABS(divisor)
       
    43 	asm("rsc r3, r3, #0 ");
       
    44 	asm("2: ");
       
    45 	asm("bl UDiv01 ");					// do the division, result in r4,r5
       
    46 	asm("eors r0, r4, r8, asr #32 ");	// quotient into r1:r0, inverted if quotient -ve
       
    47 	asm("eors r1, r5, r8, asr #32 ");
       
    48 	asm("adcs r0, r0, #0 ");		// if quotient -ve, add 1
       
    49 	asm("adcs r1, r1, #0 ");
       
    50 	__POPRET("r4-r8,");
       
    51 	}
       
    52 
       
    53 __NAKED__ long long __moddi3(long long /*dividend*/, long long /*divisor*/)	/* signed */
       
    54 	{
       
    55 	asm("stmfd sp!, {r4-r8,lr} ");
       
    56 	asm("movs r8, r1 ");				// sign of remainder (=sign of dividend) into r8
       
    57 	asm("bpl 1f ");
       
    58 	asm("rsbs r0, r0, #0 ");			// ABS(dividend)
       
    59 	asm("rsc r1, r1, #0 ");
       
    60 	asm("1: ");
       
    61 	asm("movs r3, r3 ");
       
    62 	asm("bpl 2f ");
       
    63 	asm("rsbs r2, r2, #0 ");			// ABS(divisor)
       
    64 	asm("rsc r3, r3, #0 ");
       
    65 	asm("2: ");
       
    66 	asm("bl UDiv01 ");					// do the division, remainder in r3,r6
       
    67 	asm("eors r0, r3, r8, asr #32 ");	// remainder into r1:r0, inverted if dividend -ve
       
    68 	asm("eors r1, r6, r8, asr #32 ");
       
    69 	asm("adcs r0, r0, #0 ");			// if dividend -ve, add 1
       
    70 	asm("adcs r1, r1, #0 ");
       
    71 	__POPRET("r4-r8,");
       
    72 	}
       
    73 
       
    74 __NAKED__ long long __umoddi3(unsigned long long /*dividend*/, unsigned long long /*divisor*/)	/* unsigned */
       
    75 	{
       
    76 	asm("stmfd sp!, {r4-r7,lr} ");
       
    77 	asm("bl UDiv01 ");					// do the division, remainder in r6:r3
       
    78 	asm("mov r0, r3 ");
       
    79 	asm("mov r1, r6 ");
       
    80 	__POPRET("r4-r7,");
       
    81 	}
       
    82 
       
    83 __NAKED__ long long __ashrdi3(long long /*value*/, unsigned int /*count*/)
       
    84 	{
       
    85 	asm("cmp r2, #63 ");
       
    86 	asm("movhi r2, #63 ");			// count>63 same as count=63
       
    87 	asm("cmp r2, #32 ");
       
    88 	asm("bcs Asr01 ");				// jump if shift count >=32
       
    89 	asm("rsb r12, r2, #32 ");		// r12=32-shift count
       
    90 	asm("mov r0, r0, lsr r2 ");		// shift ls word right
       
    91 	asm("orr r0, r0, r1, lsl r12 ");	// or in bits shifted out of ms word
       
    92 	asm("mov r1, r1, asr r2 ");		// shift ms word right
       
    93 	__JUMP(,lr);
       
    94 	asm("Asr01: ");
       
    95 	asm("sub r2, r2, #32 ");		// r2=shift count-32
       
    96 	asm("mov r0, r1, asr r2 ");		// ls word = ms word >> (count-32)
       
    97 	asm("mov r1, r1, asr #32 ");	// ms word of result=sign extension of r1
       
    98 	__JUMP(,lr);
       
    99 	}
       
   100 
       
   101 __NAKED__ long long __ashldi3(long long /*value*/, unsigned int /*count*/)
       
   102 	{
       
   103 	asm("cmp r2, #63 ");
       
   104 	asm("movhi r2, #64 ");			// count>63 same as count=64
       
   105 	asm("cmp r2, #32 ");
       
   106 	asm("bcs Asl01 ");				// jump if shift count >=32
       
   107 	asm("rsb r12, r2, #32 ");		// r12=32-shift count
       
   108 	asm("mov r1, r1, asl r2 ");		// shift ms word left
       
   109 	asm("orr r1, r1, r0, lsr r12 ");	// or in bits shifted out of ls word
       
   110 	asm("mov r0, r0, asl r2 ");		// shift ls word left
       
   111 	__JUMP(,lr);
       
   112 	asm("Asl01: ");
       
   113 	asm("sub r2, r2, #32 ");		// r2=shift count-32
       
   114 	asm("mov r1, r0, asl r2 ");		// result ms word = ls word << (count-32)
       
   115 	asm("mov r0, #0 ");				// ls word of result is zero
       
   116 	__JUMP(,lr);
       
   117 	}
       
   118 
       
   119 __NAKED__ unsigned long long __lshrdi3(unsigned long long /*value*/, unsigned int /*count*/)
       
   120 	{
       
   121 	asm("cmp r2, #63 ");
       
   122 	asm("movhi r2, #64 ");			// count>63 same as count=64
       
   123 	asm("cmp r2, #32 ");
       
   124 	asm("bcs Lsr01 ");				// jump if shift count >=32
       
   125 	asm("rsb r12, r2, #32 ");		// r12=32-shift count
       
   126 	asm("mov r0, r0, lsr r2 ");		// shift ls word right
       
   127 	asm("orr r0, r0, r1, lsl r12 ");	// or in bits shifted out of ms word
       
   128 	asm("mov r1, r1, lsr r2 ");		// shift ms word right
       
   129 	__JUMP(,lr);
       
   130 	asm("Lsr01: ");
       
   131 	asm("sub r2, r2, #32 ");		// r2=shift count-32
       
   132 	asm("mov r0, r1, lsr r2 ");		// ls word = ms word >> (count-32)
       
   133 	asm("mov r1, #0 ");				// ms word of result = 0
       
   134 	__JUMP(,lr);
       
   135 	}
       
   136 
       
   137 __NAKED__ long long __muldi3(long long /*multiplicand*/, long long /*multiplier*/)
       
   138 	{
       
   139 	asm("mul r1, r2, r1 ");				// r1=low2*high1
       
   140 	asm("mov ip, r0 ");					// ip=low1
       
   141 	asm("mla r1, r0, r3, r1 ");			// r1+=low1*high2
       
   142 	asm("mov r0, #0 ");
       
   143 	asm("umlal r0, r1, r2, ip ");		// r1:r0 += high1*low1
       
   144 	__JUMP(,lr);
       
   145 	}
       
   146 
       
   147 __NAKED__ long long __negdi2(long long /*argument*/)
       
   148 	{
       
   149 	asm("rsbs r0, r0, #0 ");		// r0=0-r0, set carry
       
   150 	asm("rscs r1, r1, #0 ");		// r1=0-r1-(1-C)
       
   151 	__JUMP(,lr);
       
   152 	}
       
   153 
       
   154 __NAKED__ unsigned long long __udivmoddi4 (unsigned long long /*dividend*/,
       
   155 													unsigned long long /*divisor*/,
       
   156 													unsigned long long* /*p_remainder*/)
       
   157 	{
       
   158 	asm("stmfd sp!, {r4-r7,lr} ");
       
   159 	asm("bl UDiv01 ");					// do the division, quotient in r5:r4 remainder in r6:r3
       
   160 	asm("ldr r7, [sp, #20] ");			// r7=p_remainder
       
   161 	asm("mov r0, r4 ");					// r0=quotient low
       
   162 	asm("stmia r7, {r3,r6} ");			// store remainder
       
   163 	asm("mov r1, r5 ");					// r0=quotient high
       
   164 	__POPRET("r4-r7,");
       
   165 	}
       
   166 
       
   167 __NAKED__ int __cmpdi2(long long /*a*/, long long /*b*/)
       
   168 	{
       
   169 	// return 0 if a<b, 1 if a=b, 2 if a>b
       
   170 	asm("subs r0, r2, r0 ");
       
   171 	asm("sbcs r1, r3, r1 ");			// r1:r0 = b-a, set flags
       
   172 	asm("movlt r0, #2 ");				// if b<a r0=2
       
   173 	__JUMP(lt,lr);						// if b<a return
       
   174 	asm("cmpeq r0, #0 ");				// if top word of difference=0, look at bottom
       
   175 	asm("moveq r0, #1 ");				// if a=b, r0=1
       
   176 	asm("movne r0, #0 ");				// else r=0
       
   177 	__JUMP(,lr);
       
   178 	}
       
   179 
       
   180 __NAKED__ int __ucmpdi2(unsigned long long /*a*/, unsigned long long /*b*/)
       
   181 	{
       
   182 	// return 0 if a<b, 1 if a=b, 2 if a>b
       
   183 	asm("cmp r1, r3 ");
       
   184 	asm("cmpeq r0, r2 ");				// compare r1:r0 - r3:r2
       
   185 	asm("movhi r0, #2 ");				// r0=2 if a>b
       
   186 	asm("moveq r0, #1 ");				// r0=1 if a=b
       
   187 	asm("movlo r0, #0 ");				// r0=0 if a<b
       
   188 	__JUMP(,lr);
       
   189 	}
       
   190 #endif
       
   191 
       
   192 #if defined(__GCC32__)
       
   193 void __division_by_zero();
       
   194 #define DIV_BY_ZERO " __division_by_zero "
       
   195 #elif defined(__ARMCC__)
       
   196 void __rt_div0 (void);
       
   197 #define DIV_BY_ZERO " __cpp(__rt_div0) "
       
   198 #endif
       
   199 
       
   200 void __division_by_zero()
       
   201 	{
       
   202 	RThread().RaiseException(EExcIntegerDivideByZero);
       
   203     }
       
   204 
       
   205 __NAKED__ unsigned long long __udivdi3(unsigned long long /*dividend*/, unsigned long long /*divisor*/)
       
   206 //
       
   207 // Dividend in r1:r0, divisor in r3:r2, Return quotient in r1:r0
       
   208 //
       
   209 	{
       
   210 	asm("stmfd sp!, {r4-r7,lr} ");
       
   211 	asm("bl UDiv01 ");					// do the division, result in r4,r5
       
   212 	asm("mov r0, r4 ");
       
   213 	asm("mov r1, r5 ");
       
   214 	__POPRET("r4-r7,");
       
   215 
       
   216 	// Unsigned 64-bit division. Dividend in r0,r1, divisor in r2,r3
       
   217 	// Quotient returned in r4,r5, Remainder in r3,r6
       
   218 	// Registers r0-r7,r12 used, r8-r11 unmodified
       
   219 	asm(".global UDiv01 ");
       
   220 	asm("UDiv01: ");
       
   221 	asm("movs r3, r3 ");				// check if divisor fits in 32 bits
       
   222 	asm("bne udiv64a ");				// branch if not
       
   223 	asm("movs r2, r2 ");				// check if divisor fits in 31 bits
       
   224 	asm("bmi udiv64e ");				// branch if not
       
   225 	asm("beq udiv64_divby0 ");			// if divisor=0, branch to error routine
       
   226 
       
   227 	// Divisor is <0x80000000
       
   228 	// This means that a 32-bit accumulator is sufficient
       
   229 	asm("mov r4, #0 ");					// use r3 as acc, result in r4, r5
       
   230 	asm("mov r5, #0 ");
       
   231 	asm("mov r6, #8 ");					// do 2 set of 32 iterations
       
   232 	asm("udiv64b: ");
       
   233 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   234 	asm("adcs r3, r3, r3 ");
       
   235 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   236 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   237 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   238 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   239 	asm("adcs r3, r3, r3 ");
       
   240 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   241 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   242 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   243 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   244 	asm("adcs r3, r3, r3 ");
       
   245 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   246 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   247 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   248 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   249 	asm("adcs r3, r3, r3 ");
       
   250 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   251 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   252 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   253 	asm("subs r6, r6, #1 ");			// loop
       
   254 	asm("bne udiv64b ");
       
   255 	asm("mov r6, #8 ");					// 2nd set of 32 iterations
       
   256 	asm("udiv64c: ");
       
   257 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   258 	asm("adcs r3, r3, r3 ");
       
   259 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   260 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   261 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   262 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   263 	asm("adcs r3, r3, r3 ");
       
   264 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   265 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   266 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   267 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   268 	asm("adcs r3, r3, r3 ");
       
   269 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   270 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   271 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   272 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   273 	asm("adcs r3, r3, r3 ");
       
   274 	asm("subs r3, r3, r2 ");			// subtract divisor from acc
       
   275 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   276 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   277 	asm("subs r6, r6, #1 ");			// loop
       
   278 	asm("bne udiv64c ");
       
   279 	__JUMP(,lr);
       
   280 
       
   281 	// 2^31 <= Divisor < 2^32
       
   282 	// Need 33-bit accumulator - use carry flag as 33rd bit
       
   283 	asm("udiv64e: ");
       
   284 	asm("mov r4, #0 ");					// use r3 as acc, result in r4, r5
       
   285 	asm("mov r5, #0 ");
       
   286 	asm("mov r6, #8 ");					// do 2 set of 32 iterations
       
   287 	asm("udiv64f: ");
       
   288 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   289 	asm("adcs r3, r3, r3 ");
       
   290 	asm("subcs r3, r3, r2 ");
       
   291 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   292 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   293 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   294 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   295 	asm("adcs r3, r3, r3 ");
       
   296 	asm("subcs r3, r3, r2 ");
       
   297 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   298 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   299 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   300 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   301 	asm("adcs r3, r3, r3 ");
       
   302 	asm("subcs r3, r3, r2 ");
       
   303 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   304 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   305 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   306 	asm("adds r1, r1, r1 ");			// shift dividend left into acc
       
   307 	asm("adcs r3, r3, r3 ");
       
   308 	asm("subcs r3, r3, r2 ");
       
   309 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   310 	asm("adc r5, r5, r5 ");				// shift result bit left into quotient
       
   311 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   312 	asm("subs r6, r6, #1 ");			// loop
       
   313 	asm("bne udiv64f ");
       
   314 	asm("mov r6, #8 ");					// 2nd set of 32 iterations
       
   315 	asm("udiv64g: ");
       
   316 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   317 	asm("adcs r3, r3, r3 ");
       
   318 	asm("subcs r3, r3, r2 ");
       
   319 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   320 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   321 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   322 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   323 	asm("adcs r3, r3, r3 ");
       
   324 	asm("subcs r3, r3, r2 ");
       
   325 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   326 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   327 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   328 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   329 	asm("adcs r3, r3, r3 ");
       
   330 	asm("subcs r3, r3, r2 ");
       
   331 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   332 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   333 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   334 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   335 	asm("adcs r3, r3, r3 ");
       
   336 	asm("subcs r3, r3, r2 ");
       
   337 	asm("subccs r3, r3, r2 ");			// subtract divisor from acc
       
   338 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   339 	asm("addcc r3, r3, r2 ");			// if borrow, add back
       
   340 	asm("subs r6, r6, #1 ");			// loop
       
   341 	asm("bne udiv64g ");
       
   342 	__JUMP(,lr);
       
   343 	
       
   344 	// Divisor >= 2^32, so quotient < 2^32
       
   345 	// Use 64 bit accumulator, 32 bit quotient
       
   346 	asm("udiv64a: ");
       
   347 	asm("mov r4, #0 ");					// quotient in r4, use r1, r6 as accumulator
       
   348 	asm("mov r6, #0 ");
       
   349 	asm("mov r5, #8 ");					// do 32 iterations
       
   350 	asm("udiv64d: ");
       
   351 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   352 	asm("adcs r1, r1, r1 ");
       
   353 	asm("adcs r6, r6, r6 ");
       
   354 	asm("subs r7, r1, r2 ");			// subtract divisor from acc, result into r7,r12
       
   355 	asm("sbcs r12, r6, r3 ");
       
   356 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   357 	asm("movcs r1, r7 ");				// if no borrow, update acc
       
   358 	asm("movcs r6, r12 ");
       
   359 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   360 	asm("adcs r1, r1, r1 ");
       
   361 	asm("adcs r6, r6, r6 ");
       
   362 	asm("subs r7, r1, r2 ");			// subtract divisor from acc, result into r7,r12
       
   363 	asm("sbcs r12, r6, r3 ");
       
   364 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   365 	asm("movcs r1, r7 ");				// if no borrow, update acc
       
   366 	asm("movcs r6, r12 ");
       
   367 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   368 	asm("adcs r1, r1, r1 ");
       
   369 	asm("adcs r6, r6, r6 ");
       
   370 	asm("subs r7, r1, r2 ");			// subtract divisor from acc, result into r7,r12
       
   371 	asm("sbcs r12, r6, r3 ");
       
   372 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   373 	asm("movcs r1, r7 ");				// if no borrow, update acc
       
   374 	asm("movcs r6, r12 ");
       
   375 	asm("adds r0, r0, r0 ");			// shift dividend left into acc
       
   376 	asm("adcs r1, r1, r1 ");
       
   377 	asm("adcs r6, r6, r6 ");
       
   378 	asm("subs r7, r1, r2 ");			// subtract divisor from acc, result into r7,r12
       
   379 	asm("sbcs r12, r6, r3 ");
       
   380 	asm("adc r4, r4, r4 ");				// shift result bit left into quotient
       
   381 	asm("movcs r1, r7 ");				// if no borrow, update acc
       
   382 	asm("movcs r6, r12 ");
       
   383 	asm("subs r5, r5, #1 ");			// loop
       
   384 	asm("bne udiv64d ");
       
   385 	asm("mov r3, r1 ");					// remainder in r3,r6
       
   386 	__JUMP(,lr);
       
   387 
       
   388 	asm("udiv64_divby0: ");
       
   389 	asm("str lr, [sp, #-4]! ");
       
   390 	asm("bl " DIV_BY_ZERO);
       
   391 	__POPRET("");
       
   392 	}
       
   393 
       
   394 }
       
   395