311{
312 const uint8_t* p = static_cast<const uint8_t*>(a);
313 const uint8_t* q = static_cast<const uint8_t*>(b);
314 const uint8_t* mismatch_p = nullptr;
315 const uint8_t* mismatch_q = nullptr;
316 size_t mismatch_size = 0;
317
318 if ((size == 0) || (p == q))
319 {
320 return 0;
321 }
322
325
326
327 if ((p_off == q_off) && (p_off != 0))
328 {
330 if (head > size)
331 {
332 head = size;
333 }
334
335 while (head--)
336 {
337 int diff = static_cast<int>(*p++) - static_cast<int>(*q++);
338 if (diff != 0)
339 {
340 return diff;
341 }
342 --size;
343 }
344 }
345
347 {
348
349 if ((((reinterpret_cast<uintptr_t>(p) | reinterpret_cast<uintptr_t>(q)) & 7u) == 0u))
350 {
351 auto* pw = reinterpret_cast<const uint64_t*>(p);
352 auto* qw = reinterpret_cast<const uint64_t*>(q);
353
354 while (size >= 64)
355 {
356 if (pw[0] != qw[0])
357 {
358 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 0);
359 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 0);
360 mismatch_size = 8;
361 goto compare_fixed_bytes;
362 }
363 if (pw[1] != qw[1])
364 {
365 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 1);
366 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 1);
367 mismatch_size = 8;
368 goto compare_fixed_bytes;
369 }
370 if (pw[2] != qw[2])
371 {
372 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 2);
373 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 2);
374 mismatch_size = 8;
375 goto compare_fixed_bytes;
376 }
377 if (pw[3] != qw[3])
378 {
379 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 3);
380 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 3);
381 mismatch_size = 8;
382 goto compare_fixed_bytes;
383 }
384 if (pw[4] != qw[4])
385 {
386 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 4);
387 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 4);
388 mismatch_size = 8;
389 goto compare_fixed_bytes;
390 }
391 if (pw[5] != qw[5])
392 {
393 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 5);
394 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 5);
395 mismatch_size = 8;
396 goto compare_fixed_bytes;
397 }
398 if (pw[6] != qw[6])
399 {
400 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 6);
401 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 6);
402 mismatch_size = 8;
403 goto compare_fixed_bytes;
404 }
405 if (pw[7] != qw[7])
406 {
407 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 7);
408 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 7);
409 mismatch_size = 8;
410 goto compare_fixed_bytes;
411 }
412
413 pw += 8;
414 qw += 8;
415 size -= 64;
416 }
417
418 while (size >= 8)
419 {
420 if (*pw != *qw)
421 {
422 mismatch_p = reinterpret_cast<const uint8_t*>(pw);
423 mismatch_q = reinterpret_cast<const uint8_t*>(qw);
424 mismatch_size = 8;
425 goto compare_fixed_bytes;
426 }
427 ++pw;
428 ++qw;
429 size -= 8;
430 }
431
432 p = reinterpret_cast<const uint8_t*>(pw);
433 q = reinterpret_cast<const uint8_t*>(qw);
434 }
435 }
436 else
437 {
438
439 if ((((reinterpret_cast<uintptr_t>(p) | reinterpret_cast<uintptr_t>(q)) & 3u) == 0u))
440 {
441 auto* pw = reinterpret_cast<const uint32_t*>(p);
442 auto* qw = reinterpret_cast<const uint32_t*>(q);
443
444 while (size >= 32)
445 {
446 if (pw[0] != qw[0])
447 {
448 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 0);
449 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 0);
450 mismatch_size = 4;
451 goto compare_fixed_bytes;
452 }
453 if (pw[1] != qw[1])
454 {
455 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 1);
456 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 1);
457 mismatch_size = 4;
458 goto compare_fixed_bytes;
459 }
460 if (pw[2] != qw[2])
461 {
462 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 2);
463 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 2);
464 mismatch_size = 4;
465 goto compare_fixed_bytes;
466 }
467 if (pw[3] != qw[3])
468 {
469 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 3);
470 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 3);
471 mismatch_size = 4;
472 goto compare_fixed_bytes;
473 }
474 if (pw[4] != qw[4])
475 {
476 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 4);
477 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 4);
478 mismatch_size = 4;
479 goto compare_fixed_bytes;
480 }
481 if (pw[5] != qw[5])
482 {
483 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 5);
484 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 5);
485 mismatch_size = 4;
486 goto compare_fixed_bytes;
487 }
488 if (pw[6] != qw[6])
489 {
490 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 6);
491 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 6);
492 mismatch_size = 4;
493 goto compare_fixed_bytes;
494 }
495 if (pw[7] != qw[7])
496 {
497 mismatch_p = reinterpret_cast<const uint8_t*>(pw + 7);
498 mismatch_q = reinterpret_cast<const uint8_t*>(qw + 7);
499 mismatch_size = 4;
500 goto compare_fixed_bytes;
501 }
502
503 pw += 8;
504 qw += 8;
505 size -= 32;
506 }
507
508 while (size >= 4)
509 {
510 if (*pw != *qw)
511 {
512 mismatch_p = reinterpret_cast<const uint8_t*>(pw);
513 mismatch_q = reinterpret_cast<const uint8_t*>(qw);
514 mismatch_size = 4;
515 goto compare_fixed_bytes;
516 }
517 ++pw;
518 ++qw;
519 size -= 4;
520 }
521
522 p = reinterpret_cast<const uint8_t*>(pw);
523 q = reinterpret_cast<const uint8_t*>(qw);
524 }
525 }
526
527compare_fixed_bytes:
528 if (mismatch_size == 8)
529 {
530 int diff = static_cast<int>(mismatch_p[0]) - static_cast<int>(mismatch_q[0]);
531 if (diff != 0)
532 {
533 return diff;
534 }
535 diff = static_cast<int>(mismatch_p[1]) - static_cast<int>(mismatch_q[1]);
536 if (diff != 0)
537 {
538 return diff;
539 }
540 diff = static_cast<int>(mismatch_p[2]) - static_cast<int>(mismatch_q[2]);
541 if (diff != 0)
542 {
543 return diff;
544 }
545 diff = static_cast<int>(mismatch_p[3]) - static_cast<int>(mismatch_q[3]);
546 if (diff != 0)
547 {
548 return diff;
549 }
550 diff = static_cast<int>(mismatch_p[4]) - static_cast<int>(mismatch_q[4]);
551 if (diff != 0)
552 {
553 return diff;
554 }
555 diff = static_cast<int>(mismatch_p[5]) - static_cast<int>(mismatch_q[5]);
556 if (diff != 0)
557 {
558 return diff;
559 }
560 diff = static_cast<int>(mismatch_p[6]) - static_cast<int>(mismatch_q[6]);
561 if (diff != 0)
562 {
563 return diff;
564 }
565 return static_cast<int>(mismatch_p[7]) - static_cast<int>(mismatch_q[7]);
566 }
567
568 if (mismatch_size == 4)
569 {
570 int diff = static_cast<int>(mismatch_p[0]) - static_cast<int>(mismatch_q[0]);
571 if (diff != 0)
572 {
573 return diff;
574 }
575 diff = static_cast<int>(mismatch_p[1]) - static_cast<int>(mismatch_q[1]);
576 if (diff != 0)
577 {
578 return diff;
579 }
580 diff = static_cast<int>(mismatch_p[2]) - static_cast<int>(mismatch_q[2]);
581 if (diff != 0)
582 {
583 return diff;
584 }
585 return static_cast<int>(mismatch_p[3]) - static_cast<int>(mismatch_q[3]);
586 }
587
588
589 while (size--)
590 {
591 int diff = static_cast<int>(*p++) - static_cast<int>(*q++);
592 if (diff != 0)
593 {
594 return diff;
595 }
596 }
597
598 return 0;
599}
constexpr size_t ALIGN_SIZE
平台自然对齐大小 / Native platform alignment size