os/kernelhwsrv/kernel/eka/include/x86hlp.inl
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-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\common\x86\x86hlp.inl
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#ifdef __GCC32__
sl@0
    19
#include "x86hlp_gcc.inl"
sl@0
    20
#else
sl@0
    21
sl@0
    22
/**** MSVC helpers ****/
sl@0
    23
sl@0
    24
/*static void DivisionByZero()
sl@0
    25
	{
sl@0
    26
	_asm int 0;
sl@0
    27
	}*/
sl@0
    28
sl@0
    29
#pragma warning ( disable : 4414 )  // short jump to function converted to near
sl@0
    30
sl@0
    31
extern "C" {
sl@0
    32
__NAKED__ void _allmul()
sl@0
    33
//
sl@0
    34
// Multiply two 64 bit integers returning a 64 bit result
sl@0
    35
// On entry:
sl@0
    36
//		[esp+4], [esp+8] = arg 1
sl@0
    37
//		[esp+12], [esp+16] = arg 1
sl@0
    38
// Return result in edx:eax
sl@0
    39
// Remove arguments from stack
sl@0
    40
//
sl@0
    41
	{
sl@0
    42
	_asm mov eax, [esp+4]			// eax = low1
sl@0
    43
	_asm mul dword ptr [esp+16]		// edx:eax = low1*high2
sl@0
    44
	_asm mov ecx, eax				// keep low 32 bits of product
sl@0
    45
	_asm mov eax, [esp+8]			// eax = high1
sl@0
    46
	_asm mul dword ptr [esp+12]		// edx:eax = high1*low2
sl@0
    47
	_asm add ecx, eax				// accumulate low 32 bits of product
sl@0
    48
	_asm mov eax, [esp+4]			// eax = low1
sl@0
    49
	_asm mul dword ptr [esp+12]		// edx:eax = low1*low2
sl@0
    50
	_asm add edx, ecx				// add cross terms to high 32 bits
sl@0
    51
	_asm ret 16
sl@0
    52
	}
sl@0
    53
sl@0
    54
void udiv64_divby0()
sl@0
    55
	{
sl@0
    56
	_asm int 0						// division by zero exception
sl@0
    57
	_asm ret
sl@0
    58
	}
sl@0
    59
sl@0
    60
__NAKED__ void UDiv64()
sl@0
    61
	{
sl@0
    62
	// unsigned divide edx:eax by edi:esi
sl@0
    63
	// quotient in ebx:eax, remainder in edi:edx
sl@0
    64
	// ecx, ebp, esi also modified
sl@0
    65
	_asm test edi, edi
sl@0
    66
	_asm jnz short UDiv64a				// branch if divisor >= 2^32
sl@0
    67
	_asm test esi, esi
sl@0
    68
//	_ASM_j(z,DivisionByZero)			// if divisor=0, branch to error routine
sl@0
    69
	_asm jz udiv64_divby0
sl@0
    70
	_asm mov ebx, eax					// ebx=dividend low
sl@0
    71
	_asm mov eax, edx					// eax=dividend high
sl@0
    72
	_asm xor edx, edx					// edx=0
sl@0
    73
	_asm div esi						// quotient high now in eax
sl@0
    74
	_asm xchg eax, ebx					// quotient high in ebx, dividend low in eax
sl@0
    75
	_asm div esi						// quotient now in ebx:eax, remainder in edi:edx
sl@0
    76
	_asm ret
sl@0
    77
	UDiv64e:
sl@0
    78
	_asm xor eax, eax					// set result to 0xFFFFFFFF
sl@0
    79
	_asm dec eax
sl@0
    80
	_asm jmp short UDiv64f
sl@0
    81
	UDiv64a:
sl@0
    82
	_asm js short UDiv64b				// skip if divisor msb set
sl@0
    83
	_asm bsr ecx, edi					// ecx=bit number of divisor msb - 32
sl@0
    84
	_asm inc cl
sl@0
    85
	_asm push edi						// save divisor high
sl@0
    86
	_asm push esi						// save divisor low
sl@0
    87
	_asm shrd esi, edi, cl				// shift divisor right so that msb is bit 31
sl@0
    88
	_asm mov ebx, edx					// dividend into ebx:ebp
sl@0
    89
	_asm mov ebp, eax
sl@0
    90
	_asm shrd eax, edx, cl				// shift dividend right same number of bits
sl@0
    91
	_asm shr edx, cl
sl@0
    92
	_asm cmp edx, esi					// check if approx quotient will be 2^32
sl@0
    93
	_asm jae short UDiv64e				// if so, true result must be 0xFFFFFFFF
sl@0
    94
	_asm div esi						// approximate quotient now in eax
sl@0
    95
	UDiv64f:
sl@0
    96
	_asm mov ecx, eax					// into ecx
sl@0
    97
	_asm mul edi						// multiply approx. quotient by divisor high
sl@0
    98
	_asm mov esi, eax					// ls dword into esi, ms into edi
sl@0
    99
	_asm mov edi, edx
sl@0
   100
	_asm mov eax, ecx					// approx. quotient into eax
sl@0
   101
	_asm mul dword ptr [esp]			// multiply approx. quotient by divisor low
sl@0
   102
	_asm add edx, esi					// edi:edx:eax now equals approx. quotient * divisor
sl@0
   103
	_asm adc edi, 0
sl@0
   104
	_asm xor esi, esi
sl@0
   105
	_asm sub ebp, eax					// subtract dividend - approx. quotient *divisor
sl@0
   106
	_asm sbb ebx, edx
sl@0
   107
	_asm sbb esi, edi
sl@0
   108
	_asm jnc short UDiv64c				// if no borrow, result OK
sl@0
   109
	_asm dec ecx						// else result is one too big
sl@0
   110
	_asm add ebp, [esp]					// and add divisor to get correct remainder
sl@0
   111
	_asm adc ebx, [esp+4]
sl@0
   112
	UDiv64c:
sl@0
   113
	_asm mov eax, ecx					// result into ebx:eax, remainder into edi:edx
sl@0
   114
	_asm mov edi, ebx
sl@0
   115
	_asm mov edx, ebp
sl@0
   116
	_asm xor ebx, ebx
sl@0
   117
	_asm add esp, 8						// remove temporary values from stack
sl@0
   118
	_asm ret
sl@0
   119
	UDiv64b:
sl@0
   120
	_asm mov ebx, 1
sl@0
   121
	_asm sub eax, esi					// subtract divisor from dividend
sl@0
   122
	_asm sbb edx, edi
sl@0
   123
	_asm jnc short UDiv64d				// if no borrow, result=1, remainder in edx:eax
sl@0
   124
	_asm add eax, esi					// else add back
sl@0
   125
	_asm adc edx, edi
sl@0
   126
	_asm dec ebx						// and decrement quotient
sl@0
   127
	UDiv64d:
sl@0
   128
	_asm mov edi, edx					// remainder into edi:edx
sl@0
   129
	_asm mov edx, eax
sl@0
   130
	_asm mov eax, ebx					// result in ebx:eax
sl@0
   131
	_asm xor ebx, ebx
sl@0
   132
	_asm ret
sl@0
   133
	}
sl@0
   134
sl@0
   135
__NAKED__ void _aulldvrm()
sl@0
   136
//
sl@0
   137
// Divide two 64 bit unsigned integers, returning a 64 bit result
sl@0
   138
// and a 64 bit remainder
sl@0
   139
//
sl@0
   140
// On entry:
sl@0
   141
//		[esp+4], [esp+8] = dividend
sl@0
   142
//		[esp+12], [esp+16] = divisor
sl@0
   143
//
sl@0
   144
// Return (dividend / divisor) in edx:eax
sl@0
   145
// Return (dividend % divisor) in ebx:ecx
sl@0
   146
//
sl@0
   147
// Remove arguments from stack
sl@0
   148
//
sl@0
   149
	{
sl@0
   150
	_asm push ebp
sl@0
   151
	_asm push edi
sl@0
   152
	_asm push esi
sl@0
   153
	_asm mov eax, [esp+16]
sl@0
   154
	_asm mov edx, [esp+20]
sl@0
   155
	_asm mov esi, [esp+24]
sl@0
   156
	_asm mov edi, [esp+28]
sl@0
   157
	_asm call UDiv64
sl@0
   158
	_asm mov ecx, edx
sl@0
   159
	_asm mov edx, ebx
sl@0
   160
	_asm mov ebx, edi
sl@0
   161
	_asm pop esi
sl@0
   162
	_asm pop edi
sl@0
   163
	_asm pop ebp
sl@0
   164
	_asm ret 16
sl@0
   165
	}
sl@0
   166
sl@0
   167
__NAKED__ void _alldvrm()
sl@0
   168
//
sl@0
   169
// Divide two 64 bit signed integers, returning a 64 bit result
sl@0
   170
// and a 64 bit remainder
sl@0
   171
//
sl@0
   172
// On entry:
sl@0
   173
//		[esp+4], [esp+8] = dividend
sl@0
   174
//		[esp+12], [esp+16] = divisor
sl@0
   175
//
sl@0
   176
// Return (dividend / divisor) in edx:eax
sl@0
   177
// Return (dividend % divisor) in ebx:ecx
sl@0
   178
//
sl@0
   179
// Remove arguments from stack
sl@0
   180
//
sl@0
   181
	{
sl@0
   182
	_asm push ebp
sl@0
   183
	_asm push edi
sl@0
   184
	_asm push esi
sl@0
   185
	_asm mov eax, [esp+16]
sl@0
   186
	_asm mov edx, [esp+20]
sl@0
   187
	_asm mov esi, [esp+24]
sl@0
   188
	_asm mov edi, [esp+28]
sl@0
   189
	_asm test edx, edx
sl@0
   190
	_asm jns dividend_nonnegative
sl@0
   191
	_asm neg edx
sl@0
   192
	_asm neg eax
sl@0
   193
	_asm sbb edx, 0
sl@0
   194
	dividend_nonnegative:
sl@0
   195
	_asm test edi, edi
sl@0
   196
	_asm jns divisor_nonnegative
sl@0
   197
	_asm neg edi
sl@0
   198
	_asm neg esi
sl@0
   199
	_asm sbb edi, 0
sl@0
   200
	divisor_nonnegative:
sl@0
   201
	_asm call UDiv64
sl@0
   202
	_asm mov ebp, [esp+20]
sl@0
   203
	_asm mov ecx, edx
sl@0
   204
	_asm xor ebp, [esp+28]
sl@0
   205
	_asm mov edx, ebx
sl@0
   206
	_asm mov ebx, edi
sl@0
   207
	_asm jns quotient_nonnegative
sl@0
   208
	_asm neg edx
sl@0
   209
	_asm neg eax
sl@0
   210
	_asm sbb edx, 0
sl@0
   211
	quotient_nonnegative:
sl@0
   212
	_asm cmp dword ptr [esp+20], 0
sl@0
   213
	_asm jns rem_nonnegative
sl@0
   214
	_asm neg ebx
sl@0
   215
	_asm neg ecx
sl@0
   216
	_asm sbb ebx, 0
sl@0
   217
	rem_nonnegative:
sl@0
   218
	_asm pop esi
sl@0
   219
	_asm pop edi
sl@0
   220
	_asm pop ebp
sl@0
   221
	_asm ret 16
sl@0
   222
	}
sl@0
   223
sl@0
   224
__NAKED__ void _aulldiv()
sl@0
   225
//
sl@0
   226
// Divide two 64 bit unsigned integers returning a 64 bit result
sl@0
   227
// On entry:
sl@0
   228
//		[esp+4], [esp+8] = dividend
sl@0
   229
//		[esp+12], [esp+16] = divisor
sl@0
   230
// Return result in edx:eax
sl@0
   231
// Remove arguments from stack
sl@0
   232
//
sl@0
   233
	{
sl@0
   234
	_asm push ebp
sl@0
   235
	_asm push edi
sl@0
   236
	_asm push esi
sl@0
   237
	_asm push ebx
sl@0
   238
	_asm mov eax, [esp+20]
sl@0
   239
	_asm mov edx, [esp+24]
sl@0
   240
	_asm mov esi, [esp+28]
sl@0
   241
	_asm mov edi, [esp+32]
sl@0
   242
	_asm call UDiv64
sl@0
   243
	_asm mov edx, ebx
sl@0
   244
	_asm pop ebx
sl@0
   245
	_asm pop esi
sl@0
   246
	_asm pop edi
sl@0
   247
	_asm pop ebp
sl@0
   248
	_asm ret 16
sl@0
   249
	}
sl@0
   250
sl@0
   251
__NAKED__ void _alldiv()
sl@0
   252
//
sl@0
   253
// Divide two 64 bit signed integers returning a 64 bit result
sl@0
   254
// On entry:
sl@0
   255
//		[esp+4], [esp+8] = dividend
sl@0
   256
//		[esp+12], [esp+16] = divisor
sl@0
   257
// Return result in edx:eax
sl@0
   258
// Remove arguments from stack
sl@0
   259
//
sl@0
   260
	{
sl@0
   261
	_asm push ebp
sl@0
   262
	_asm push edi
sl@0
   263
	_asm push esi
sl@0
   264
	_asm push ebx
sl@0
   265
	_asm mov eax, [esp+20]
sl@0
   266
	_asm mov edx, [esp+24]
sl@0
   267
	_asm mov esi, [esp+28]
sl@0
   268
	_asm mov edi, [esp+32]
sl@0
   269
	_asm test edx, edx
sl@0
   270
	_asm jns dividend_nonnegative
sl@0
   271
	_asm neg edx
sl@0
   272
	_asm neg eax
sl@0
   273
	_asm sbb edx, 0
sl@0
   274
	dividend_nonnegative:
sl@0
   275
	_asm test edi, edi
sl@0
   276
	_asm jns divisor_nonnegative
sl@0
   277
	_asm neg edi
sl@0
   278
	_asm neg esi
sl@0
   279
	_asm sbb edi, 0
sl@0
   280
	divisor_nonnegative:
sl@0
   281
	_asm call UDiv64
sl@0
   282
	_asm mov ecx, [esp+24]
sl@0
   283
	_asm mov edx, ebx
sl@0
   284
	_asm xor ecx, [esp+32]
sl@0
   285
	_asm jns quotient_nonnegative
sl@0
   286
	_asm neg edx
sl@0
   287
	_asm neg eax
sl@0
   288
	_asm sbb edx, 0
sl@0
   289
	quotient_nonnegative:
sl@0
   290
	_asm pop ebx
sl@0
   291
	_asm pop esi
sl@0
   292
	_asm pop edi
sl@0
   293
	_asm pop ebp
sl@0
   294
	_asm ret 16
sl@0
   295
	}
sl@0
   296
sl@0
   297
__NAKED__ void _aullrem()
sl@0
   298
//
sl@0
   299
// Divide two 64 bit unsigned integers and return 64 bit remainder
sl@0
   300
// On entry:
sl@0
   301
//		[esp+4], [esp+8] = dividend
sl@0
   302
//		[esp+12], [esp+16] = divisor
sl@0
   303
// Return result in edx:eax
sl@0
   304
// Remove arguments from stack
sl@0
   305
//
sl@0
   306
	{
sl@0
   307
	_asm push ebp
sl@0
   308
	_asm push edi
sl@0
   309
	_asm push esi
sl@0
   310
	_asm push ebx
sl@0
   311
	_asm mov eax, [esp+20]
sl@0
   312
	_asm mov edx, [esp+24]
sl@0
   313
	_asm mov esi, [esp+28]
sl@0
   314
	_asm mov edi, [esp+32]
sl@0
   315
	_asm call UDiv64
sl@0
   316
	_asm mov eax, edx
sl@0
   317
	_asm mov edx, edi
sl@0
   318
	_asm pop ebx
sl@0
   319
	_asm pop esi
sl@0
   320
	_asm pop edi
sl@0
   321
	_asm pop ebp
sl@0
   322
	_asm ret 16
sl@0
   323
	}
sl@0
   324
sl@0
   325
__NAKED__ void _allrem()
sl@0
   326
//
sl@0
   327
// Divide two 64 bit signed integers and return 64 bit remainder
sl@0
   328
// On entry:
sl@0
   329
//		[esp+4], [esp+8] = dividend
sl@0
   330
//		[esp+12], [esp+16] = divisor
sl@0
   331
// Return result in edx:eax
sl@0
   332
// Remove arguments from stack
sl@0
   333
//
sl@0
   334
	{
sl@0
   335
	_asm push ebp
sl@0
   336
	_asm push edi
sl@0
   337
	_asm push esi
sl@0
   338
	_asm push ebx
sl@0
   339
	_asm mov eax, [esp+20]
sl@0
   340
	_asm mov edx, [esp+24]
sl@0
   341
	_asm mov esi, [esp+28]
sl@0
   342
	_asm mov edi, [esp+32]
sl@0
   343
	_asm test edx, edx
sl@0
   344
	_asm jns dividend_nonnegative
sl@0
   345
	_asm neg edx
sl@0
   346
	_asm neg eax
sl@0
   347
	_asm sbb edx, 0
sl@0
   348
	dividend_nonnegative:
sl@0
   349
	_asm test edi, edi
sl@0
   350
	_asm jns divisor_nonnegative
sl@0
   351
	_asm neg edi
sl@0
   352
	_asm neg esi
sl@0
   353
	_asm sbb edi, 0
sl@0
   354
	divisor_nonnegative:
sl@0
   355
	_asm call UDiv64
sl@0
   356
	_asm mov eax, edx
sl@0
   357
	_asm mov edx, edi
sl@0
   358
	_asm cmp dword ptr [esp+24], 0
sl@0
   359
	_asm jns rem_nonnegative
sl@0
   360
	_asm neg edx
sl@0
   361
	_asm neg eax
sl@0
   362
	_asm sbb edx, 0
sl@0
   363
	rem_nonnegative:
sl@0
   364
	_asm pop ebx
sl@0
   365
	_asm pop esi
sl@0
   366
	_asm pop edi
sl@0
   367
	_asm pop ebp
sl@0
   368
	_asm ret 16
sl@0
   369
	}
sl@0
   370
sl@0
   371
__NAKED__ void _allshr()
sl@0
   372
//
sl@0
   373
// Arithmetic shift right EDX:EAX by CL
sl@0
   374
//
sl@0
   375
	{
sl@0
   376
	_asm cmp cl, 64
sl@0
   377
	_asm jae asr_count_ge_64
sl@0
   378
	_asm cmp cl, 32
sl@0
   379
	_asm jae asr_count_ge_32
sl@0
   380
	_asm shrd eax, edx, cl
sl@0
   381
	_asm sar edx, cl
sl@0
   382
	_asm ret
sl@0
   383
	asr_count_ge_32:
sl@0
   384
	_asm sub cl, 32
sl@0
   385
	_asm mov eax, edx
sl@0
   386
	_asm cdq
sl@0
   387
	_asm sar eax, cl
sl@0
   388
	_asm ret
sl@0
   389
	asr_count_ge_64:
sl@0
   390
	_asm sar edx, 32
sl@0
   391
	_asm mov eax, edx
sl@0
   392
	_asm ret
sl@0
   393
	}
sl@0
   394
sl@0
   395
__NAKED__ void _allshl()
sl@0
   396
//
sl@0
   397
// shift left EDX:EAX by CL
sl@0
   398
//
sl@0
   399
	{
sl@0
   400
	_asm cmp cl, 64
sl@0
   401
	_asm jae lsl_count_ge_64
sl@0
   402
	_asm cmp cl, 32
sl@0
   403
	_asm jae lsl_count_ge_32
sl@0
   404
	_asm shld edx, eax, cl
sl@0
   405
	_asm shl eax, cl
sl@0
   406
	_asm ret
sl@0
   407
	lsl_count_ge_32:
sl@0
   408
	_asm sub cl, 32
sl@0
   409
	_asm mov edx, eax
sl@0
   410
	_asm xor eax, eax
sl@0
   411
	_asm shl edx, cl
sl@0
   412
	_asm ret
sl@0
   413
	lsl_count_ge_64:
sl@0
   414
	_asm xor edx, edx
sl@0
   415
	_asm xor eax, eax
sl@0
   416
	_asm ret
sl@0
   417
	}
sl@0
   418
sl@0
   419
__NAKED__ void _aullshr()
sl@0
   420
//
sl@0
   421
// Logical shift right EDX:EAX by CL
sl@0
   422
//
sl@0
   423
	{
sl@0
   424
	_asm cmp cl, 64
sl@0
   425
	_asm jae lsr_count_ge_64
sl@0
   426
	_asm cmp cl, 32
sl@0
   427
	_asm jae lsr_count_ge_32
sl@0
   428
	_asm shrd eax, edx, cl
sl@0
   429
	_asm shr edx, cl
sl@0
   430
	_asm ret
sl@0
   431
	lsr_count_ge_32:
sl@0
   432
	_asm sub cl, 32
sl@0
   433
	_asm mov eax, edx
sl@0
   434
	_asm xor edx, edx
sl@0
   435
	_asm shr eax, cl
sl@0
   436
	_asm ret
sl@0
   437
	lsr_count_ge_64:
sl@0
   438
	_asm xor edx, edx
sl@0
   439
	_asm xor eax, eax
sl@0
   440
	_asm ret
sl@0
   441
	}
sl@0
   442
sl@0
   443
sl@0
   444
}
sl@0
   445
sl@0
   446
sl@0
   447
#endif