~ubuntu-branches/ubuntu/intrepid/xulrunner-1.9/intrepid-security

« back to all changes in this revision

Viewing changes to mozilla/security/nss/lib/freebl/mpi/mpi_x86_asm.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2009-08-03 13:16:32 UTC
  • mfrom: (1.2.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20090803131632-1pfcgs323tjx7bp1
Tags: 1.9.0.13+nobinonly-0ubuntu0.8.10.1
* New upstream release v1.9.0.13 (FIREFOX_3_0_13_RELEASE)
  - see USN-811-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
#include "mpi-priv.h"
42
42
 
 
43
static int is_sse = -1;
 
44
extern unsigned long s_mpi_is_sse2();
 
45
 
43
46
/*
44
47
 *   ebp - 36:  caller's esi
45
48
 *   ebp - 32:  caller's edi
68
71
s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
69
72
{
70
73
  __asm {
 
74
    mov    eax, is_sse
 
75
    cmp    eax, 0
 
76
    je     s_mpv_mul_d_x86
 
77
    jg     s_mpv_mul_d_sse2
 
78
    call   s_mpi_is_sse2
 
79
    mov    is_sse, eax
 
80
    cmp    eax, 0
 
81
    jg     s_mpv_mul_d_sse2
 
82
s_mpv_mul_d_x86:
71
83
    push   ebp
72
84
    mov    ebp,esp
73
85
    sub    esp,28
101
113
    leave  
102
114
    ret    
103
115
    nop
104
 
  }
105
 
}
106
 
 
107
 
/*
108
 
 *   ebp - 36:  caller's esi
109
 
 *   ebp - 32:  caller's edi
110
 
 *   ebp - 28:  
111
 
 *   ebp - 24:  
112
 
 *   ebp - 20:  
113
 
 *   ebp - 16:  
114
 
 *   ebp - 12:  
115
 
 *   ebp - 8:   
116
 
 *   ebp - 4:   
117
 
 *   ebp + 0:   caller's ebp
118
 
 *   ebp + 4:   return address
119
 
 *   ebp + 8:   a       argument
120
 
 *   ebp + 12:  a_len   argument
121
 
 *   ebp + 16:  b       argument
122
 
 *   ebp + 20:  c       argument
123
 
 *   registers:
124
 
 *      eax:
125
 
 *      ebx:    carry
126
 
 *      ecx:    a_len
127
 
 *      edx:
128
 
 *      esi:    a ptr
129
 
 *      edi:    c ptr
130
 
 */
131
 
__declspec(naked) void
132
 
s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
133
 
{
134
 
  __asm {
135
 
    push   ebp
136
 
    mov    ebp,esp
137
 
    sub    esp,28
138
 
    push   edi
139
 
    push   esi
140
 
    push   ebx
141
 
    mov    ebx,0                ; carry = 0
142
 
    mov    ecx,[ebp+12]         ; ecx = a_len
143
 
    mov    edi,[ebp+20]
144
 
    cmp    ecx,0
145
 
    je     L_4                  ; jmp if a_len == 0
146
 
    mov    esi,[ebp+8]          ; esi = a
147
 
    cld
148
 
L_3:
149
 
    lodsd                       ; eax = [ds:esi]; esi += 4
150
 
    mov    edx,[ebp+16]         ; edx = b
151
 
    mul    edx                  ; edx:eax = Phi:Plo = a_i * b
152
 
 
153
 
    add    eax,ebx              ; add carry (ebx) to edx:eax
154
 
    adc    edx,0
155
 
    mov    ebx,[edi]            ; add in current word from *c
156
 
    add    eax,ebx              
157
 
    adc    edx,0
158
 
    mov    ebx,edx              ; high half of product becomes next carry
159
 
 
160
 
    stosd                       ; [es:edi] = ax; edi += 4;
161
 
    dec    ecx                  ; --a_len
162
 
    jnz    L_3                  ; jmp if a_len != 0
163
 
L_4:
164
 
    mov    [edi],ebx            ; *c = carry
165
 
    pop    ebx
166
 
    pop    esi
167
 
    pop    edi
168
 
    leave  
169
 
    ret    
170
 
    nop
171
 
  }
172
 
}
173
 
 
174
 
/*
175
 
 *   ebp - 36:  caller's esi
176
 
 *   ebp - 32:  caller's edi
177
 
 *   ebp - 28:  
178
 
 *   ebp - 24:  
179
 
 *   ebp - 20:  
180
 
 *   ebp - 16:  
181
 
 *   ebp - 12:  
182
 
 *   ebp - 8:   
183
 
 *   ebp - 4:   
184
 
 *   ebp + 0:   caller's ebp
185
 
 *   ebp + 4:   return address
186
 
 *   ebp + 8:   a       argument
187
 
 *   ebp + 12:  a_len   argument
188
 
 *   ebp + 16:  b       argument
189
 
 *   ebp + 20:  c       argument
190
 
 *   registers:
191
 
 *      eax:
192
 
 *      ebx:    carry
193
 
 *      ecx:    a_len
194
 
 *      edx:
195
 
 *      esi:    a ptr
196
 
 *      edi:    c ptr
197
 
 */
198
 
__declspec(naked) void
199
 
s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
200
 
{
201
 
  __asm {
202
 
    push   ebp
203
 
    mov    ebp,esp
204
 
    sub    esp,28
205
 
    push   edi
206
 
    push   esi
207
 
    push   ebx
208
 
    mov    ebx,0                ; carry = 0
209
 
    mov    ecx,[ebp+12]         ; ecx = a_len
210
 
    mov    edi,[ebp+20]
211
 
    cmp    ecx,0
 
116
s_mpv_mul_d_sse2:
 
117
    push   ebp
 
118
    mov    ebp, esp
 
119
    push   edi
 
120
    push   esi
 
121
    psubq  mm2, mm2             ; carry = 0
 
122
    mov    ecx, [ebp+12]        ; ecx = a_len
 
123
    movd   mm1, [ebp+16]        ; mm1 = b
 
124
    mov    edi, [ebp+20]
 
125
    cmp    ecx, 0
212
126
    je     L_6                  ; jmp if a_len == 0
 
127
    mov    esi, [ebp+8]         ; esi = a
213
128
    cld
214
 
    mov    esi,[ebp+8]          ; esi = a
215
129
L_5:
216
 
    lodsd                       ; eax = [ds:esi]; esi += 4
217
 
    mov    edx,[ebp+16]         ; edx = b
218
 
    mul    edx                  ; edx:eax = Phi:Plo = a_i * b
219
 
 
220
 
    add    eax,ebx              ; add carry (ebx) to edx:eax
221
 
    adc    edx,0
222
 
    mov    ebx,[edi]            ; add in current word from *c
223
 
    add    eax,ebx              
224
 
    adc    edx,0
225
 
    mov    ebx,edx              ; high half of product becomes next carry
226
 
 
227
 
    stosd                       ; [es:edi] = ax; edi += 4;
 
130
    movd   mm0, [esi]           ; mm0 = *a++
 
131
    add    esi, 4
 
132
    pmuludq mm0, mm1            ; mm0 = b * *a++
 
133
    paddq  mm2, mm0             ; add the carry
 
134
    movd   [edi], mm2           ; store the 32bit result
 
135
    add    edi, 4
 
136
    psrlq  mm2, 32              ; save the carry
228
137
    dec    ecx                  ; --a_len
229
138
    jnz    L_5                  ; jmp if a_len != 0
230
139
L_6:
 
140
    movd   [edi], mm2           ; *c = carry
 
141
    emms
 
142
    pop    esi
 
143
    pop    edi
 
144
    leave  
 
145
    ret    
 
146
    nop
 
147
  }
 
148
}
 
149
 
 
150
/*
 
151
 *   ebp - 36:  caller's esi
 
152
 *   ebp - 32:  caller's edi
 
153
 *   ebp - 28:  
 
154
 *   ebp - 24:  
 
155
 *   ebp - 20:  
 
156
 *   ebp - 16:  
 
157
 *   ebp - 12:  
 
158
 *   ebp - 8:   
 
159
 *   ebp - 4:   
 
160
 *   ebp + 0:   caller's ebp
 
161
 *   ebp + 4:   return address
 
162
 *   ebp + 8:   a       argument
 
163
 *   ebp + 12:  a_len   argument
 
164
 *   ebp + 16:  b       argument
 
165
 *   ebp + 20:  c       argument
 
166
 *   registers:
 
167
 *      eax:
 
168
 *      ebx:    carry
 
169
 *      ecx:    a_len
 
170
 *      edx:
 
171
 *      esi:    a ptr
 
172
 *      edi:    c ptr
 
173
 */
 
174
__declspec(naked) void
 
175
s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
 
176
{
 
177
  __asm {
 
178
    mov    eax, is_sse
 
179
    cmp    eax, 0
 
180
    je     s_mpv_mul_d_add_x86
 
181
    jg     s_mpv_mul_d_add_sse2
 
182
    call   s_mpi_is_sse2
 
183
    mov    is_sse, eax
 
184
    cmp    eax, 0
 
185
    jg     s_mpv_mul_d_add_sse2
 
186
s_mpv_mul_d_add_x86:
 
187
    push   ebp
 
188
    mov    ebp,esp
 
189
    sub    esp,28
 
190
    push   edi
 
191
    push   esi
 
192
    push   ebx
 
193
    mov    ebx,0                ; carry = 0
 
194
    mov    ecx,[ebp+12]         ; ecx = a_len
 
195
    mov    edi,[ebp+20]
 
196
    cmp    ecx,0
 
197
    je     L_11                 ; jmp if a_len == 0
 
198
    mov    esi,[ebp+8]          ; esi = a
 
199
    cld
 
200
L_10:
 
201
    lodsd                       ; eax = [ds:esi]; esi += 4
 
202
    mov    edx,[ebp+16]         ; edx = b
 
203
    mul    edx                  ; edx:eax = Phi:Plo = a_i * b
 
204
 
 
205
    add    eax,ebx              ; add carry (ebx) to edx:eax
 
206
    adc    edx,0
 
207
    mov    ebx,[edi]            ; add in current word from *c
 
208
    add    eax,ebx              
 
209
    adc    edx,0
 
210
    mov    ebx,edx              ; high half of product becomes next carry
 
211
 
 
212
    stosd                       ; [es:edi] = ax; edi += 4;
 
213
    dec    ecx                  ; --a_len
 
214
    jnz    L_10                 ; jmp if a_len != 0
 
215
L_11:
 
216
    mov    [edi],ebx            ; *c = carry
 
217
    pop    ebx
 
218
    pop    esi
 
219
    pop    edi
 
220
    leave  
 
221
    ret    
 
222
    nop
 
223
s_mpv_mul_d_add_sse2:
 
224
    push   ebp
 
225
    mov    ebp, esp
 
226
    push   edi
 
227
    push   esi
 
228
    psubq  mm2, mm2             ; carry = 0
 
229
    mov    ecx, [ebp+12]        ; ecx = a_len
 
230
    movd   mm1, [ebp+16]        ; mm1 = b
 
231
    mov    edi, [ebp+20]
 
232
    cmp    ecx, 0
 
233
    je     L_16                 ; jmp if a_len == 0
 
234
    mov    esi, [ebp+8]         ; esi = a
 
235
    cld
 
236
L_15:
 
237
    movd   mm0, [esi]           ; mm0 = *a++
 
238
    add    esi, 4
 
239
    pmuludq mm0, mm1            ; mm0 = b * *a++
 
240
    paddq  mm2, mm0             ; add the carry
 
241
    movd   mm0, [edi]
 
242
    paddq  mm2, mm0             ; add the carry
 
243
    movd   [edi], mm2           ; store the 32bit result
 
244
    add    edi, 4
 
245
    psrlq  mm2, 32              ; save the carry
 
246
    dec    ecx                  ; --a_len
 
247
    jnz    L_15                 ; jmp if a_len != 0
 
248
L_16:
 
249
    movd   [edi], mm2           ; *c = carry
 
250
    emms
 
251
    pop    esi
 
252
    pop    edi
 
253
    leave  
 
254
    ret    
 
255
    nop
 
256
  }
 
257
}
 
258
 
 
259
/*
 
260
 *   ebp - 36:  caller's esi
 
261
 *   ebp - 32:  caller's edi
 
262
 *   ebp - 28:  
 
263
 *   ebp - 24:  
 
264
 *   ebp - 20:  
 
265
 *   ebp - 16:  
 
266
 *   ebp - 12:  
 
267
 *   ebp - 8:   
 
268
 *   ebp - 4:   
 
269
 *   ebp + 0:   caller's ebp
 
270
 *   ebp + 4:   return address
 
271
 *   ebp + 8:   a       argument
 
272
 *   ebp + 12:  a_len   argument
 
273
 *   ebp + 16:  b       argument
 
274
 *   ebp + 20:  c       argument
 
275
 *   registers:
 
276
 *      eax:
 
277
 *      ebx:    carry
 
278
 *      ecx:    a_len
 
279
 *      edx:
 
280
 *      esi:    a ptr
 
281
 *      edi:    c ptr
 
282
 */
 
283
__declspec(naked) void
 
284
s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
 
285
{
 
286
  __asm {
 
287
    mov    eax, is_sse
 
288
    cmp    eax, 0
 
289
    je     s_mpv_mul_d_add_prop_x86
 
290
    jg     s_mpv_mul_d_add_prop_sse2
 
291
    call   s_mpi_is_sse2
 
292
    mov    is_sse, eax
 
293
    cmp    eax, 0
 
294
    jg     s_mpv_mul_d_add_prop_sse2
 
295
s_mpv_mul_d_add_prop_x86:
 
296
    push   ebp
 
297
    mov    ebp,esp
 
298
    sub    esp,28
 
299
    push   edi
 
300
    push   esi
 
301
    push   ebx
 
302
    mov    ebx,0                ; carry = 0
 
303
    mov    ecx,[ebp+12]         ; ecx = a_len
 
304
    mov    edi,[ebp+20]
 
305
    cmp    ecx,0
 
306
    je     L_21                 ; jmp if a_len == 0
 
307
    cld
 
308
    mov    esi,[ebp+8]          ; esi = a
 
309
L_20:
 
310
    lodsd                       ; eax = [ds:esi]; esi += 4
 
311
    mov    edx,[ebp+16]         ; edx = b
 
312
    mul    edx                  ; edx:eax = Phi:Plo = a_i * b
 
313
 
 
314
    add    eax,ebx              ; add carry (ebx) to edx:eax
 
315
    adc    edx,0
 
316
    mov    ebx,[edi]            ; add in current word from *c
 
317
    add    eax,ebx              
 
318
    adc    edx,0
 
319
    mov    ebx,edx              ; high half of product becomes next carry
 
320
 
 
321
    stosd                       ; [es:edi] = ax; edi += 4;
 
322
    dec    ecx                  ; --a_len
 
323
    jnz    L_20                 ; jmp if a_len != 0
 
324
L_21:
231
325
    cmp    ebx,0                ; is carry zero?
232
 
    jz     L_8
 
326
    jz     L_23
233
327
    mov    eax,[edi]            ; add in current word from *c
234
328
    add    eax,ebx
235
329
    stosd                       ; [es:edi] = ax; edi += 4;
236
 
    jnc    L_8
237
 
L_7:
 
330
    jnc    L_23
 
331
L_22:
238
332
    mov    eax,[edi]            ; add in current word from *c
239
333
    adc    eax,0
240
334
    stosd                       ; [es:edi] = ax; edi += 4;
241
 
    jc     L_7
242
 
L_8:
 
335
    jc     L_22
 
336
L_23:
 
337
    pop    ebx
 
338
    pop    esi
 
339
    pop    edi
 
340
    leave  
 
341
    ret    
 
342
    nop
 
343
s_mpv_mul_d_add_prop_sse2:
 
344
    push   ebp
 
345
    mov    ebp, esp
 
346
    push   edi
 
347
    push   esi
 
348
    push   ebx
 
349
    psubq  mm2, mm2             ; carry = 0
 
350
    mov    ecx, [ebp+12]        ; ecx = a_len
 
351
    movd   mm1, [ebp+16]        ; mm1 = b
 
352
    mov    edi, [ebp+20]
 
353
    cmp    ecx, 0
 
354
    je     L_26                 ; jmp if a_len == 0
 
355
    mov    esi, [ebp+8]         ; esi = a
 
356
    cld
 
357
L_25:
 
358
    movd   mm0, [esi]           ; mm0 = *a++
 
359
    movd   mm3, [edi]           ; fetch the sum
 
360
    add    esi, 4
 
361
    pmuludq mm0, mm1            ; mm0 = b * *a++
 
362
    paddq  mm2, mm0             ; add the carry
 
363
    paddq  mm2, mm3             ; add *c++
 
364
    movd   [edi], mm2           ; store the 32bit result
 
365
    add    edi, 4
 
366
    psrlq  mm2, 32              ; save the carry
 
367
    dec    ecx                  ; --a_len
 
368
    jnz    L_25                 ; jmp if a_len != 0
 
369
L_26:
 
370
    movd   ebx, mm2
 
371
    cmp    ebx, 0               ; is carry zero?
 
372
    jz     L_28
 
373
    mov    eax, [edi]
 
374
    add    eax, ebx
 
375
    stosd
 
376
    jnc    L_28
 
377
L_27:
 
378
    mov    eax, [edi]           ; add in current word from *c
 
379
    adc    eax, 0
 
380
    stosd                       ; [es:edi] = ax; edi += 4;
 
381
    jc     L_27
 
382
L_28:
 
383
    emms
243
384
    pop    ebx
244
385
    pop    esi
245
386
    pop    edi
273
414
s_mpv_sqr_add_prop(const mp_digit *a, mp_size a_len, mp_digit *sqrs)
274
415
{
275
416
  __asm {
 
417
     mov    eax, is_sse
 
418
     cmp    eax, 0
 
419
     je     s_mpv_sqr_add_prop_x86
 
420
     jg     s_mpv_sqr_add_prop_sse2
 
421
     call   s_mpi_is_sse2
 
422
     mov    is_sse, eax
 
423
     cmp    eax, 0
 
424
     jg     s_mpv_sqr_add_prop_sse2
 
425
s_mpv_sqr_add_prop_x86:
276
426
     push   ebp
277
427
     mov    ebp,esp
278
428
     sub    esp,12
283
433
     mov    ecx,[ebp+12]        ; a_len
284
434
     mov    edi,[ebp+16]        ; edi = ps
285
435
     cmp    ecx,0
286
 
     je     L_11                ; jump if a_len == 0
 
436
     je     L_31                ; jump if a_len == 0
287
437
     cld
288
438
     mov    esi,[ebp+8]         ; esi = pa
289
 
L_10:
 
439
L_30:
290
440
     lodsd                      ; eax = [ds:si]; si += 4;
291
441
     mul    eax
292
442
 
302
452
     adc    ebx,0
303
453
     stosd                      ; [es:di] = eax; di += 4;
304
454
     dec    ecx                 ; --a_len
305
 
     jnz    L_10                ; jmp if a_len != 0
306
 
L_11:
 
455
     jnz    L_30                ; jmp if a_len != 0
 
456
L_31:
307
457
    cmp    ebx,0                ; is carry zero?
308
 
    jz     L_14
 
458
    jz     L_34
309
459
    mov    eax,[edi]            ; add in current word from *c
310
460
    add    eax,ebx
311
461
    stosd                       ; [es:edi] = ax; edi += 4;
312
 
    jnc    L_14
313
 
L_12:
 
462
    jnc    L_34
 
463
L_32:
314
464
    mov    eax,[edi]            ; add in current word from *c
315
465
    adc    eax,0
316
466
    stosd                       ; [es:edi] = ax; edi += 4;
317
 
    jc     L_12
318
 
L_14:
 
467
    jc     L_32
 
468
L_34:
 
469
    pop    ebx
 
470
    pop    esi
 
471
    pop    edi
 
472
    leave  
 
473
    ret    
 
474
    nop
 
475
s_mpv_sqr_add_prop_sse2:
 
476
    push   ebp
 
477
    mov    ebp, esp
 
478
    push   edi
 
479
    push   esi
 
480
    push   ebx
 
481
    psubq  mm2, mm2             ; carry = 0
 
482
    mov    ecx, [ebp+12]        ; ecx = a_len
 
483
    mov    edi, [ebp+16]
 
484
    cmp    ecx, 0
 
485
    je     L_36         ; jmp if a_len == 0
 
486
    mov    esi, [ebp+8]         ; esi = a
 
487
    cld
 
488
L_35:
 
489
    movd   mm0, [esi]           ; mm0 = *a
 
490
    movd   mm3, [edi]           ; fetch the sum
 
491
    add    esi, 4
 
492
    pmuludq mm0, mm0            ; mm0 = sqr(a)
 
493
    paddq  mm2, mm0             ; add the carry
 
494
    paddq  mm2, mm3             ; add the low word
 
495
    movd   mm3, [edi+4]
 
496
    movd   [edi], mm2           ; store the 32bit result
 
497
    psrlq  mm2, 32      
 
498
    paddq  mm2, mm3             ; add the high word
 
499
    movd   [edi+4], mm2         ; store the 32bit result
 
500
    psrlq  mm2, 32              ; save the carry.
 
501
    add    edi, 8
 
502
    dec    ecx                  ; --a_len
 
503
    jnz    L_35                 ; jmp if a_len != 0
 
504
L_36:
 
505
    movd   ebx, mm2
 
506
    cmp    ebx, 0               ; is carry zero?
 
507
    jz     L_38
 
508
    mov    eax, [edi]
 
509
    add    eax, ebx
 
510
    stosd
 
511
    jnc    L_38
 
512
L_37:
 
513
    mov    eax, [edi]           ; add in current word from *c
 
514
    adc    eax, 0
 
515
    stosd                       ; [es:edi] = ax; edi += 4;
 
516
    jc     L_37
 
517
L_38:
 
518
    emms
319
519
    pop    ebx
320
520
    pop    esi
321
521
    pop    edi