1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/bigint/vchelp.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,351 @@
1.4 +/*
1.5 +* Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* e32\nklib\x86\vchelp.cpp
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#ifndef __NAKED__
1.24 +#define __NAKED__ __declspec(naked)
1.25 +#endif
1.26 +
1.27 +#include <e32def.h>
1.28 +
1.29 +#pragma warning ( disable : 4414 ) // short jump to function converted to near
1.30 +
1.31 +extern "C" {
1.32 +__NAKED__ void _allmul()
1.33 +//
1.34 +// Multiply two 64 bit integers returning a 64 bit result
1.35 +// On entry:
1.36 +// [esp+4], [esp+8] = arg 1
1.37 +// [esp+12], [esp+16] = arg 1
1.38 +// Return result in edx:eax
1.39 +// Remove arguments from stack
1.40 +//
1.41 + {
1.42 + _asm mov eax, [esp+4] // eax = low1
1.43 + _asm mul dword ptr [esp+16] // edx:eax = low1*high2
1.44 + _asm mov ecx, eax // keep low 32 bits of product
1.45 + _asm mov eax, [esp+8] // eax = high1
1.46 + _asm mul dword ptr [esp+12] // edx:eax = high1*low2
1.47 + _asm add ecx, eax // accumulate low 32 bits of product
1.48 + _asm mov eax, [esp+4] // eax = low1
1.49 + _asm mul dword ptr [esp+12] // edx:eax = low1*low2
1.50 + _asm add edx, ecx // add cross terms to high 32 bits
1.51 + _asm ret 16
1.52 + }
1.53 +
1.54 +void udiv64_divby0()
1.55 + {
1.56 + _asm int 0 // division by zero exception
1.57 + _asm ret
1.58 + }
1.59 +
1.60 +__NAKED__ void UDiv64()
1.61 + {
1.62 + // unsigned divide edx:eax by edi:esi
1.63 + // quotient in ebx:eax, remainder in edi:edx
1.64 + // ecx, ebp, esi also modified
1.65 + _asm test edi, edi
1.66 + _asm jnz short UDiv64a // branch if divisor >= 2^32
1.67 + _asm test esi, esi
1.68 +// _ASM_j(z,DivisionByZero) // if divisor=0, branch to error routine
1.69 + _asm jz udiv64_divby0
1.70 + _asm mov ebx, eax // ebx=dividend low
1.71 + _asm mov eax, edx // eax=dividend high
1.72 + _asm xor edx, edx // edx=0
1.73 + _asm div esi // quotient high now in eax
1.74 + _asm xchg eax, ebx // quotient high in ebx, dividend low in eax
1.75 + _asm div esi // quotient now in ebx:eax, remainder in edi:edx
1.76 + _asm ret
1.77 + UDiv64e:
1.78 + _asm xor eax, eax // set result to 0xFFFFFFFF
1.79 + _asm dec eax
1.80 + _asm jmp short UDiv64f
1.81 + UDiv64a:
1.82 + _asm js short UDiv64b // skip if divisor msb set
1.83 + _asm bsr ecx, edi // ecx=bit number of divisor msb - 32
1.84 + _asm inc cl
1.85 + _asm push edi // save divisor high
1.86 + _asm push esi // save divisor low
1.87 + _asm shrd esi, edi, cl // shift divisor right so that msb is bit 31
1.88 + _asm mov ebx, edx // dividend into ebx:ebp
1.89 + _asm mov ebp, eax
1.90 + _asm shrd eax, edx, cl // shift dividend right same number of bits
1.91 + _asm shr edx, cl
1.92 + _asm cmp edx, esi // check if approx quotient will be 2^32
1.93 + _asm jae short UDiv64e // if so, true result must be 0xFFFFFFFF
1.94 + _asm div esi // approximate quotient now in eax
1.95 + UDiv64f:
1.96 + _asm mov ecx, eax // into ecx
1.97 + _asm mul edi // multiply approx. quotient by divisor high
1.98 + _asm mov esi, eax // ls dword into esi, ms into edi
1.99 + _asm mov edi, edx
1.100 + _asm mov eax, ecx // approx. quotient into eax
1.101 + _asm mul dword ptr [esp] // multiply approx. quotient by divisor low
1.102 + _asm add edx, esi // edi:edx:eax now equals approx. quotient * divisor
1.103 + _asm adc edi, 0
1.104 + _asm xor esi, esi
1.105 + _asm sub ebp, eax // subtract dividend - approx. quotient *divisor
1.106 + _asm sbb ebx, edx
1.107 + _asm sbb esi, edi
1.108 + _asm jnc short UDiv64c // if no borrow, result OK
1.109 + _asm dec ecx // else result is one too big
1.110 + _asm add ebp, [esp] // and add divisor to get correct remainder
1.111 + _asm adc ebx, [esp+4]
1.112 + UDiv64c:
1.113 + _asm mov eax, ecx // result into ebx:eax, remainder into edi:edx
1.114 + _asm mov edi, ebx
1.115 + _asm mov edx, ebp
1.116 + _asm xor ebx, ebx
1.117 + _asm add esp, 8 // remove temporary values from stack
1.118 + _asm ret
1.119 + UDiv64b:
1.120 + _asm mov ebx, 1
1.121 + _asm sub eax, esi // subtract divisor from dividend
1.122 + _asm sbb edx, edi
1.123 + _asm jnc short UDiv64d // if no borrow, result=1, remainder in edx:eax
1.124 + _asm add eax, esi // else add back
1.125 + _asm adc edx, edi
1.126 + _asm dec ebx // and decrement quotient
1.127 + UDiv64d:
1.128 + _asm mov edi, edx // remainder into edi:edx
1.129 + _asm mov edx, eax
1.130 + _asm mov eax, ebx // result in ebx:eax
1.131 + _asm xor ebx, ebx
1.132 + _asm ret
1.133 + }
1.134 +
1.135 +__NAKED__ void _aulldiv()
1.136 +//
1.137 +// Divide two 64 bit unsigned integers returning a 64 bit result
1.138 +// On entry:
1.139 +// [esp+4], [esp+8] = dividend
1.140 +// [esp+12], [esp+16] = divisor
1.141 +// Return result in edx:eax
1.142 +// Remove arguments from stack
1.143 +//
1.144 + {
1.145 + _asm push ebp
1.146 + _asm push edi
1.147 + _asm push esi
1.148 + _asm push ebx
1.149 + _asm mov eax, [esp+20]
1.150 + _asm mov edx, [esp+24]
1.151 + _asm mov esi, [esp+28]
1.152 + _asm mov edi, [esp+32]
1.153 + _asm call UDiv64
1.154 + _asm mov edx, ebx
1.155 + _asm pop ebx
1.156 + _asm pop esi
1.157 + _asm pop edi
1.158 + _asm pop ebp
1.159 + _asm ret 16
1.160 + }
1.161 +
1.162 +__NAKED__ void _alldiv()
1.163 +//
1.164 +// Divide two 64 bit signed integers returning a 64 bit result
1.165 +// On entry:
1.166 +// [esp+4], [esp+8] = dividend
1.167 +// [esp+12], [esp+16] = divisor
1.168 +// Return result in edx:eax
1.169 +// Remove arguments from stack
1.170 +//
1.171 + {
1.172 + _asm push ebp
1.173 + _asm push edi
1.174 + _asm push esi
1.175 + _asm push ebx
1.176 + _asm mov eax, [esp+20]
1.177 + _asm mov edx, [esp+24]
1.178 + _asm mov esi, [esp+28]
1.179 + _asm mov edi, [esp+32]
1.180 + _asm test edx, edx
1.181 + _asm jns dividend_nonnegative
1.182 + _asm neg edx
1.183 + _asm neg eax
1.184 + _asm sbb edx, 0
1.185 + dividend_nonnegative:
1.186 + _asm test edi, edi
1.187 + _asm jns divisor_nonnegative
1.188 + _asm neg edi
1.189 + _asm neg esi
1.190 + _asm sbb edi, 0
1.191 + divisor_nonnegative:
1.192 + _asm call UDiv64
1.193 + _asm mov ecx, [esp+24]
1.194 + _asm mov edx, ebx
1.195 + _asm xor ecx, [esp+32]
1.196 + _asm jns quotient_nonnegative
1.197 + _asm neg edx
1.198 + _asm neg eax
1.199 + _asm sbb edx, 0
1.200 + quotient_nonnegative:
1.201 + _asm pop ebx
1.202 + _asm pop esi
1.203 + _asm pop edi
1.204 + _asm pop ebp
1.205 + _asm ret 16
1.206 + }
1.207 +
1.208 +__NAKED__ void _aullrem()
1.209 +//
1.210 +// Divide two 64 bit unsigned integers and return 64 bit remainder
1.211 +// On entry:
1.212 +// [esp+4], [esp+8] = dividend
1.213 +// [esp+12], [esp+16] = divisor
1.214 +// Return result in edx:eax
1.215 +// Remove arguments from stack
1.216 +//
1.217 + {
1.218 + _asm push ebp
1.219 + _asm push edi
1.220 + _asm push esi
1.221 + _asm push ebx
1.222 + _asm mov eax, [esp+20]
1.223 + _asm mov edx, [esp+24]
1.224 + _asm mov esi, [esp+28]
1.225 + _asm mov edi, [esp+32]
1.226 + _asm call UDiv64
1.227 + _asm mov eax, edx
1.228 + _asm mov edx, edi
1.229 + _asm pop ebx
1.230 + _asm pop esi
1.231 + _asm pop edi
1.232 + _asm pop ebp
1.233 + _asm ret 16
1.234 + }
1.235 +
1.236 +__NAKED__ void _allrem()
1.237 +//
1.238 +// Divide two 64 bit signed integers and return 64 bit remainder
1.239 +// On entry:
1.240 +// [esp+4], [esp+8] = dividend
1.241 +// [esp+12], [esp+16] = divisor
1.242 +// Return result in edx:eax
1.243 +// Remove arguments from stack
1.244 +//
1.245 + {
1.246 + _asm push ebp
1.247 + _asm push edi
1.248 + _asm push esi
1.249 + _asm push ebx
1.250 + _asm mov eax, [esp+20]
1.251 + _asm mov edx, [esp+24]
1.252 + _asm mov esi, [esp+28]
1.253 + _asm mov edi, [esp+32]
1.254 + _asm test edx, edx
1.255 + _asm jns dividend_nonnegative
1.256 + _asm neg edx
1.257 + _asm neg eax
1.258 + _asm sbb edx, 0
1.259 + dividend_nonnegative:
1.260 + _asm test edi, edi
1.261 + _asm jns divisor_nonnegative
1.262 + _asm neg edi
1.263 + _asm neg esi
1.264 + _asm sbb edi, 0
1.265 + divisor_nonnegative:
1.266 + _asm call UDiv64
1.267 + _asm mov eax, edx
1.268 + _asm mov edx, edi
1.269 + _asm cmp dword ptr [esp+24], 0
1.270 + _asm jns rem_nonnegative
1.271 + _asm neg edx
1.272 + _asm neg eax
1.273 + _asm sbb edx, 0
1.274 + rem_nonnegative:
1.275 + _asm pop ebx
1.276 + _asm pop esi
1.277 + _asm pop edi
1.278 + _asm pop ebp
1.279 + _asm ret 16
1.280 + }
1.281 +
1.282 +__NAKED__ void _allshr()
1.283 +//
1.284 +// Arithmetic shift right EDX:EAX by ECX
1.285 +//
1.286 + {
1.287 + _asm cmp ecx, 64
1.288 + _asm jae asr_count_ge_64
1.289 + _asm cmp cl, 32
1.290 + _asm jae asr_count_ge_32
1.291 + _asm shrd eax, edx, cl
1.292 + _asm sar edx, cl
1.293 + _asm ret
1.294 + asr_count_ge_32:
1.295 + _asm sub cl, 32
1.296 + _asm mov eax, edx
1.297 + _asm cdq
1.298 + _asm sar eax, cl
1.299 + _asm ret
1.300 + asr_count_ge_64:
1.301 + _asm sar edx, 32
1.302 + _asm mov eax, edx
1.303 + _asm ret
1.304 + }
1.305 +
1.306 +__NAKED__ void _allshl()
1.307 +//
1.308 +// shift left EDX:EAX by ECX
1.309 +//
1.310 + {
1.311 + _asm cmp ecx, 64
1.312 + _asm jae lsl_count_ge_64
1.313 + _asm cmp cl, 32
1.314 + _asm jae lsl_count_ge_32
1.315 + _asm shld edx, eax, cl
1.316 + _asm shl eax, cl
1.317 + _asm ret
1.318 + lsl_count_ge_32:
1.319 + _asm sub cl, 32
1.320 + _asm mov edx, eax
1.321 + _asm xor eax, eax
1.322 + _asm shl edx, cl
1.323 + _asm ret
1.324 + lsl_count_ge_64:
1.325 + _asm xor edx, edx
1.326 + _asm xor eax, eax
1.327 + _asm ret
1.328 + }
1.329 +
1.330 +__NAKED__ void _aullshr()
1.331 +//
1.332 +// Logical shift right EDX:EAX by ECX
1.333 +//
1.334 + {
1.335 + _asm cmp ecx, 64
1.336 + _asm jae lsr_count_ge_64
1.337 + _asm cmp cl, 32
1.338 + _asm jae lsr_count_ge_32
1.339 + _asm shrd eax, edx, cl
1.340 + _asm shr edx, cl
1.341 + _asm ret
1.342 + lsr_count_ge_32:
1.343 + _asm sub cl, 32
1.344 + _asm mov eax, edx
1.345 + _asm xor edx, edx
1.346 + _asm shr eax, cl
1.347 + _asm ret
1.348 + lsr_count_ge_64:
1.349 + _asm xor edx, edx
1.350 + _asm xor eax, eax
1.351 + _asm ret
1.352 + }
1.353 +}
1.354 +