Commit b9d197dee8eb96c48004b5b3b546e9e322a271ba

Luke Dashjr 2012-01-17T16:29:01

Refactor the CPU scanhash_* functions to use a common API. Fixes bugs. - Before, some returned bool, and others returned int (which was then turned into a bool with a comparison); now, everything returns a bool - Before, some set hashes_done to nonce - 1 when a share was found and others set it to nonce + 1 or 2. This caused some algorithms to scan/submit shares twice with the new cpu_scanhash function. Now, it has all been replaced with last_nonce, which is set to the final nonce checked by the scanhash_* func. - VIA needs the full data, and cannot use midstate. All the others were expecting midstate and data+64 for their parameters. Now, we pass midstate and the full data pointer, and let the scanhash_* function choose which to use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
diff --git a/main.c b/main.c
index bfd4c15..2ba1940 100644
--- a/main.c
+++ b/main.c
@@ -146,7 +146,6 @@ const char *algo_names[] = {
 #endif
 };
 
-typedef void (*sha256_func)();
 static const sha256_func sha256_funcs[] = {
 	[ALGO_C]		= (sha256_func)scanhash_c,
 #ifdef WANT_SSE2_4WAY
@@ -488,34 +487,20 @@ static double bench_algo_stage3(
 	struct timeval end;
 	struct timeval start;
 	uint32_t max_nonce = (1<<22);
-	unsigned long hashes_done = 0;
+	uint32_t last_nonce = 0;
 
 	gettimeofday(&start, 0);
-		#if defined(WANT_VIA_PADLOCK)
-
-			// For some reason, the VIA padlock hasher has a different API ...
-			if (ALGO_VIA==algo) {
-				(void)scanhash_via(
-					0,
-					work.data,
-					work.target,
-					max_nonce,
-					&hashes_done,
-					work.blk.nonce
-				);
-			} else
-		#endif
 			{
 				sha256_func func = sha256_funcs[algo];
 				(*func)(
 					0,
 					work.midstate,
-					work.data + 64,
+					work.data,
 					work.hash1,
 					work.hash,
 					work.target,
 					max_nonce,
-					&hashes_done,
+					&last_nonce,
 					work.blk.nonce
 				);
 			}
@@ -528,7 +513,7 @@ static double bench_algo_stage3(
 
 	double rate = -1.0;
 	if (0<usec_elapsed) {
-		rate = (1.0*hashes_done)/usec_elapsed;
+		rate = (1.0*(last_nonce+1))/usec_elapsed;
 	}
 	return rate;
 }
@@ -5465,102 +5450,28 @@ static uint64_t cpu_scanhash(struct thr_info *thr, struct work *work, uint64_t m
 {
 	const int thr_id = thr->id;
 
-	long unsigned int hashes_done = 0;
 	uint32_t first_nonce = work->blk.nonce;
-	bool rc = false;
+	uint32_t last_nonce;
+	bool rc;
+
+CPUSearch:
+	last_nonce = first_nonce;
+	rc = false;
 
 	/* scan nonces for a proof-of-work hash */
-	switch (opt_algo) {
-	case ALGO_C:
-		rc = scanhash_c(thr_id, work->midstate, work->data + 64,
-			        work->hash1, work->hash, work->target,
-				max_nonce, &hashes_done,
-				work->blk.nonce);
-		break;
-#ifdef WANT_X8632_SSE2
-	case ALGO_SSE2_32: {
-		unsigned int rc5 =
-		        scanhash_sse2_32(thr_id, work->midstate, work->data + 64,
-					 work->hash1, work->hash,
-					 work->target,
-				         max_nonce, &hashes_done,
-					 work->blk.nonce);
-		rc = (rc5 == -1) ? false : true;
-		}
-		break;
-#endif
-#ifdef WANT_X8664_SSE2
-	case ALGO_SSE2_64: {
-		unsigned int rc5 =
-		        scanhash_sse2_64(thr_id, work->midstate, work->data + 64,
-					 work->hash1, work->hash,
-					 work->target,
-				         max_nonce, &hashes_done,
-					 work->blk.nonce);
-		rc = (rc5 == -1) ? false : true;
-		}
-		break;
-#endif
-#ifdef WANT_X8664_SSE4
-	case ALGO_SSE4_64: {
-		unsigned int rc5 =
-		        scanhash_sse4_64(thr_id, work->midstate, work->data + 64,
-					 work->hash1, work->hash,
-					 work->target,
-				         max_nonce, &hashes_done,
-					 work->blk.nonce);
-		rc = (rc5 == -1) ? false : true;
-		}
-		break;
-#endif
-#ifdef WANT_SSE2_4WAY
-	case ALGO_4WAY: {
-		unsigned int rc4 =
-			ScanHash_4WaySSE2(thr_id, work->midstate, work->data + 64,
-					  work->hash1, work->hash,
-					  work->target,
-					  max_nonce, &hashes_done,
-					  work->blk.nonce);
-		rc = (rc4 == -1) ? false : true;
-		}
-		break;
-#endif
-#ifdef WANT_ALTIVEC_4WAY
-        case ALGO_ALTIVEC_4WAY:
-        {
-            unsigned int rc4 = ScanHash_altivec_4way(thr_id, work->midstate, work->data + 64,
-                    work->hash1, work->hash,
-                    work->target,
-                    max_nonce, &hashes_done,
-                    work->blk.nonce);
-            rc = (rc4 == -1) ? false : true;
-        }
-        break;
-#endif
-#ifdef WANT_VIA_PADLOCK
-	case ALGO_VIA:
-		rc = scanhash_via(thr_id, work->data, work->target,
-				  max_nonce, &hashes_done,
-				  work->blk.nonce);
-		break;
-#endif
-	case ALGO_CRYPTOPP:
-		rc = scanhash_cryptopp(thr_id, work->midstate, work->data + 64,
-			        work->hash1, work->hash, work->target,
-				max_nonce, &hashes_done,
-				work->blk.nonce);
-		break;
-#ifdef WANT_CRYPTOPP_ASM32
-	case ALGO_CRYPTOPP_ASM32:
-		rc = scanhash_asm32(thr_id, work->midstate, work->data + 64,
-			        work->hash1, work->hash, work->target,
-				max_nonce, &hashes_done,
-				work->blk.nonce);
-		break;
-#endif
-	default:
-		/* should never happen */
-		applog(LOG_ERR, "Unrecognized hash algorithm! This should be impossible!");
+	{
+		sha256_func func = sha256_funcs[opt_algo];
+		rc = (*func)(
+			thr_id,
+			work->midstate,
+			work->data,
+			work->hash1,
+			work->hash,
+			work->target,
+			max_nonce,
+			&last_nonce,
+			work->blk.nonce
+		);
 	}
 
 	/* if nonce found, submit work */
@@ -5570,10 +5481,15 @@ static uint64_t cpu_scanhash(struct thr_info *thr, struct work *work, uint64_t m
 		if (unlikely(!submit_work_sync(thr, work))) {
 			applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
 		}
+		work->blk.nonce = last_nonce + 1;
+		goto CPUSearch;
 	}
+	else
+	if (unlikely(last_nonce == first_nonce))
+		return 0;
 
-	work->blk.nonce = hashes_done;
-	return (uint64_t)hashes_done - first_nonce;
+	work->blk.nonce = last_nonce + 1;
+	return last_nonce - first_nonce + 1;
 }
 
 struct device_api cpu_api = {
diff --git a/miner.h b/miner.h
index 33518b1..86352a3 100644
--- a/miner.h
+++ b/miner.h
@@ -381,57 +381,62 @@ extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
 extern char *bin2hex(const unsigned char *p, size_t len);
 extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
 
-extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
-	unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
-	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
-
-extern unsigned int ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
+typedef bool (*sha256_func)(int thr_id, const unsigned char *pmidstate,
 	unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
+	uint32_t max_nonce,
+	uint32_t *last_nonce,
+	uint32_t nonce);
 
-extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
+extern bool ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
 	unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone);
+	uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
+
+extern bool ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
+	unsigned char *pdata,
+	unsigned char *phash1, unsigned char *phash,
+	const unsigned char *ptarget,
+	uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
 
-extern bool scanhash_via(int, unsigned char *data_inout,
+extern bool scanhash_via(int, const unsigned char *pmidstate,
+	unsigned char *pdata,
+	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *target,
-	uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
+	uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
 
 extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
 	      unsigned char *hash1, unsigned char *hash,
 	      const unsigned char *target,
-	      uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
+	      uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
 
 extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
 	      unsigned char *hash1, unsigned char *hash,
 	      const unsigned char *target,
-	      uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
+	      uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
 
 extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
 	      unsigned char *hash1, unsigned char *hash,
 	      const unsigned char *target,
-	      uint32_t max_nonce, unsigned long *hashes_done, uint32_t nonce);
+	      uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
 
-extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
+extern bool scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce);
 
-extern int scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
+extern bool scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce);
 
-extern int scanhash_sse2_32(int, const unsigned char *pmidstate, unsigned char *pdata,
+extern bool scanhash_sse2_32(int, const unsigned char *pmidstate, unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce);
 
 extern int
diff --git a/sha256_4way.c b/sha256_4way.c
index c81e05b..15e852a 100644
--- a/sha256_4way.c
+++ b/sha256_4way.c
@@ -101,14 +101,16 @@ static const unsigned int pSHA256InitState[8] =
 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
 
 
-unsigned int ScanHash_4WaySSE2(int thr_id, const unsigned char *pmidstate,
+bool ScanHash_4WaySSE2(int thr_id, const unsigned char *pmidstate,
 	unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce)
 {
-    unsigned int *nNonce_p = (unsigned int*)(pdata + 12);
+    unsigned int *nNonce_p = (unsigned int*)(pdata + 76);
+
+	pdata += 64;
 
     work_restart[thr_id].restart = 0;
 
@@ -132,17 +134,18 @@ unsigned int ScanHash_4WaySSE2(int thr_id, const unsigned char *pmidstate,
                     ((unsigned int*)phash)[i] = thash[i][j];
 
 		if (fulltest(phash, ptarget)) {
-			*nHashesDone = nonce;
-			*nNonce_p = nonce + j;
-                	return nonce + j;
+					nonce += j;
+					*last_nonce = nonce;
+					*nNonce_p = nonce;
+					return true;
 		}
             }
         }
 
         if ((nonce >= max_nonce) || work_restart[thr_id].restart)
         {
-            *nHashesDone = nonce;
-            return -1;
+            *last_nonce = nonce;
+            return false;
         }
     }
 }
diff --git a/sha256_altivec_4way.c b/sha256_altivec_4way.c
index b70e4d0..aa50486 100644
--- a/sha256_altivec_4way.c
+++ b/sha256_altivec_4way.c
@@ -74,14 +74,16 @@ static const unsigned int pSHA256InitState[8] =
 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
 
 
-unsigned int ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
+bool ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
 	unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce)
 {
-    unsigned int *nNonce_p = (unsigned int*)(pdata + 12);
+    unsigned int *nNonce_p = (unsigned int*)(pdata + 76);
+
+	pdata += 64;
 
     work_restart[thr_id].restart = 0;
 
@@ -104,17 +106,18 @@ unsigned int ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
                     ((unsigned int*)phash)[i] = thash[i][j];
 
 		if (fulltest(phash, ptarget)) {
-			*nHashesDone = nonce;
-			*nNonce_p = nonce + j;
-                	return nonce + j;
+					nonce += j;
+					*last_nonce = nonce;
+					*nNonce_p = nonce;
+					return true;
 		}
             }
         }
 
         if ((nonce >= max_nonce) || work_restart[thr_id].restart)
         {
-            *nHashesDone = nonce;
-            return -1;
+            *last_nonce = nonce;
+            return false;
         }
 
         nonce += NPAR;
diff --git a/sha256_cryptopp.c b/sha256_cryptopp.c
index c0c1b6f..11c1c5c 100644
--- a/sha256_cryptopp.c
+++ b/sha256_cryptopp.c
@@ -97,11 +97,13 @@ bool scanhash_cryptopp(int thr_id, const unsigned char *midstate,
 		unsigned char *data,
 	        unsigned char *hash1, unsigned char *hash,
 		const unsigned char *target,
-	        uint32_t max_nonce, unsigned long *hashes_done,
+	        uint32_t max_nonce, uint32_t *last_nonce,
 		uint32_t n)
 {
 	uint32_t *hash32 = (uint32_t *) hash;
-	uint32_t *nonce = (uint32_t *)(data + 12);
+	uint32_t *nonce = (uint32_t *)(data + 76);
+
+	data += 64;
 
 	work_restart[thr_id].restart = 0;
 
@@ -113,12 +115,12 @@ bool scanhash_cryptopp(int thr_id, const unsigned char *midstate,
 		runhash(hash, hash1, sha256_init_state);
 
 		if (unlikely((hash32[7] == 0) && fulltest(hash, target))) {
-			*hashes_done = n;
+			*last_nonce = n;
 			return true;
 		}
 
 		if ((n >= max_nonce) || work_restart[thr_id].restart) {
-			*hashes_done = n;
+			*last_nonce = n;
 			return false;
 		}
 	}
@@ -579,11 +581,13 @@ bool scanhash_asm32(int thr_id, const unsigned char *midstate,
 		unsigned char *data,
 	        unsigned char *hash1, unsigned char *hash,
 		const unsigned char *target,
-	        uint32_t max_nonce, unsigned long *hashes_done,
+	        uint32_t max_nonce, uint32_t *last_nonce,
 		uint32_t n)
 {
 	uint32_t *hash32 = (uint32_t *) hash;
-	uint32_t *nonce = (uint32_t *)(data + 12);
+	uint32_t *nonce = (uint32_t *)(data + 76);
+
+	data += 64;
 
 	work_restart[thr_id].restart = 0;
 
@@ -595,12 +599,12 @@ bool scanhash_asm32(int thr_id, const unsigned char *midstate,
 		runhash32(hash, hash1, sha256_init_state);
 
 		if (unlikely((hash32[7] == 0) && fulltest(hash, target))) {
-			*hashes_done = n;
+			*last_nonce = n;
 			return true;
 		}
 
 		if ((n >= max_nonce) || work_restart[thr_id].restart) {
-			*hashes_done = n;
+			*last_nonce = n;
 			return false;
 		}
 	}
diff --git a/sha256_generic.c b/sha256_generic.c
index 5bc4d97..05f4c37 100644
--- a/sha256_generic.c
+++ b/sha256_generic.c
@@ -242,13 +242,15 @@ const uint32_t sha256_init_state[8] = {
 bool scanhash_c(int thr_id, const unsigned char *midstate, unsigned char *data,
 	        unsigned char *hash1, unsigned char *hash,
 		const unsigned char *target,
-	        uint32_t max_nonce, unsigned long *hashes_done,
+	        uint32_t max_nonce, uint32_t *last_nonce,
 		uint32_t n)
 {
 	uint32_t *hash32 = (uint32_t *) hash;
-	uint32_t *nonce = (uint32_t *)(data + 12);
+	uint32_t *nonce = (uint32_t *)(data + 76);
 	unsigned long stat_ctr = 0;
 
+	data += 64;
+
 	work_restart[thr_id].restart = 0;
 
 	while (1) {
@@ -261,12 +263,12 @@ bool scanhash_c(int thr_id, const unsigned char *midstate, unsigned char *data,
 		stat_ctr++;
 
 		if (unlikely((hash32[7] == 0) && fulltest(hash, target))) {
-			*hashes_done = n;
+			*last_nonce = n;
 			return true;
 		}
 
 		if ((n >= max_nonce) || work_restart[thr_id].restart) {
-			*hashes_done = n;
+			*last_nonce = n;
 			return false;
 		}
 	}
diff --git a/sha256_sse2_amd64.c b/sha256_sse2_amd64.c
index 561aa3a..5c82314 100644
--- a/sha256_sse2_amd64.c
+++ b/sha256_sse2_amd64.c
@@ -50,14 +50,14 @@ const uint32_t sha256_init[8]__attribute__((aligned(0x100))) =
 __m128i g_4sha256_k[64];
 __m128i sha256_consts_m128i[64]__attribute__((aligned(0x1000)));
 
-int scanhash_sse2_64(int thr_id, const unsigned char *pmidstate,
+bool scanhash_sse2_64(int thr_id, const unsigned char *pmidstate,
 	unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce)
 {
-    uint32_t *nNonce_p = (uint32_t *)(pdata + 12);
+    uint32_t *nNonce_p = (uint32_t *)(pdata + 76);
     uint32_t m_midstate[8], m_w[16], m_w1[16];
     __m128i m_4w[64] __attribute__ ((aligned (0x100)));
     __m128i m_4hash[64] __attribute__ ((aligned (0x100)));
@@ -65,6 +65,8 @@ int scanhash_sse2_64(int thr_id, const unsigned char *pmidstate,
     __m128i offset;
     int i;
 
+	pdata += 64;
+
     work_restart[thr_id].restart = 0;
 
     /* For debugging */
@@ -114,19 +116,20 @@ int scanhash_sse2_64(int thr_id, const unsigned char *pmidstate,
 		}
 
 		if (fulltest(phash, ptarget)) {
-		     *nHashesDone = nonce;
-		     *nNonce_p = nonce + j;
-		     return nonce + j;
+		     nonce += j;
+		     *last_nonce = nonce + 1;
+		     *nNonce_p = nonce;
+		     return true;
 		}
 	}
 
-	nonce += 4;
-
         if (unlikely((nonce >= max_nonce) || work_restart[thr_id].restart))
         {
-            *nHashesDone = nonce;
-            return -1;
+			*last_nonce = nonce;
+			return false;
 	}
+
+	nonce += 4;
    }
 }
 
diff --git a/sha256_sse2_i386.c b/sha256_sse2_i386.c
index ef3f0ee..0935095 100644
--- a/sha256_sse2_i386.c
+++ b/sha256_sse2_i386.c
@@ -50,14 +50,14 @@ const uint32_t sha256_32init[8]__attribute__((aligned(0x100))) =
 __m128i g_4sha256_k[64];
 __m128i sha256_consts_m128i[64]__attribute__((aligned(0x1000)));
 
-int scanhash_sse2_32(int thr_id, const unsigned char *pmidstate,
+bool scanhash_sse2_32(int thr_id, const unsigned char *pmidstate,
 	unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce)
 {
-    uint32_t *nNonce_p = (uint32_t *)(pdata + 12);
+    uint32_t *nNonce_p = (uint32_t *)(pdata + 76);
     uint32_t m_midstate[8], m_w[16], m_w1[16];
     __m128i m_4w[64] __attribute__ ((aligned (0x100)));
     __m128i m_4hash[64] __attribute__ ((aligned (0x100)));
@@ -65,6 +65,8 @@ int scanhash_sse2_32(int thr_id, const unsigned char *pmidstate,
     __m128i offset;
     int i;
 
+	pdata += 64;
+
     work_restart[thr_id].restart = 0;
 
     /* For debugging */
@@ -116,19 +118,20 @@ int scanhash_sse2_32(int thr_id, const unsigned char *pmidstate,
 		}
 
 		if (fulltest(phash, ptarget)) {
-		     *nHashesDone = nonce;
-		     *nNonce_p = nonce + j;
-		     return nonce + j;
+		     nonce += j;
+		     *last_nonce = nonce;
+		     *nNonce_p = nonce;
+		     return true;
 		}
 	}
 
+	if (unlikely((nonce >= max_nonce) || work_restart[thr_id].restart)) {
+		*last_nonce = nonce;
+		return false;
+	}
+
 	nonce += 4;
 
-        if (unlikely((nonce >= max_nonce) || work_restart[thr_id].restart))
-        {
-            *nHashesDone = nonce;
-            return -1;
-	}
    }
 }
 
diff --git a/sha256_sse4_amd64.c b/sha256_sse4_amd64.c
index f150e8c..67e0604 100644
--- a/sha256_sse4_amd64.c
+++ b/sha256_sse4_amd64.c
@@ -49,19 +49,21 @@ static uint32_t g_sha256_hinit[8] =
 
 __m128i g_4sha256_k[64];
 
-int scanhash_sse4_64(int thr_id, const unsigned char *pmidstate,
+bool scanhash_sse4_64(int thr_id, const unsigned char *pmidstate,
 	unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash,
 	const unsigned char *ptarget,
-	uint32_t max_nonce, unsigned long *nHashesDone,
+	uint32_t max_nonce, uint32_t *last_nonce,
 	uint32_t nonce)
 {
-    uint32_t *nNonce_p = (uint32_t *)(pdata + 12);
+    uint32_t *nNonce_p = (uint32_t *)(pdata + 76);
     uint32_t m_midstate[8], m_w[16], m_w1[16];
     __m128i m_4w[64], m_4hash[64], m_4hash1[64];
     __m128i offset;
     int i;
 
+	pdata += 64;
+
     work_restart[thr_id].restart = 0;
 
     /* For debugging */
@@ -113,19 +115,20 @@ int scanhash_sse4_64(int thr_id, const unsigned char *pmidstate,
 		}
 
 		if (fulltest(phash, ptarget)) {
-		     *nHashesDone = nonce;
-		     *nNonce_p = nonce + j;
-		     return nonce + j;
+			nonce += j;
+			*last_nonce = nonce;
+			*nNonce_p = nonce;
+			return true;
 		}
 	}
 
-	nonce += 4;
-
         if (unlikely((nonce >= max_nonce) || work_restart[thr_id].restart))
         {
-            *nHashesDone = nonce;
-            return -1;
+			*last_nonce = nonce;
+			return false;
 	}
+
+	nonce += 4;
    }
 }
 
diff --git a/sha256_via.c b/sha256_via.c
index fdf0045..550807c 100644
--- a/sha256_via.c
+++ b/sha256_via.c
@@ -19,9 +19,11 @@ static void via_sha256(void *hash, void *buf, unsigned len)
 		     :"memory");
 }
 
-bool scanhash_via(int thr_id, unsigned char *data_inout,
-		  const unsigned char *target,
-		  uint32_t max_nonce, unsigned long *hashes_done,
+bool scanhash_via(int thr_id, const unsigned char *pmidstate,
+	unsigned char *data_inout,
+	unsigned char *phash1, unsigned char *phash,
+	const unsigned char *target,
+		  uint32_t max_nonce, uint32_t *last_nonce,
 		  uint32_t n)
 {
 	unsigned char data[128] __attribute__((aligned(128)));
@@ -70,12 +72,12 @@ bool scanhash_via(int thr_id, unsigned char *data_inout,
 				dout32[i] = swab32(data32[i]);
 			}
 
-			*hashes_done = n;
+			*last_nonce = n;
 			return true;
 		}
 
 		if ((n >= max_nonce) || work_restart[thr_id].restart) {
-			*hashes_done = n;
+			*last_nonce = n;
 			return false;
 		}
 	}