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