Path: blob/main/lib/libc/aarch64/string/timingsafe_bcmp.S
48266 views
/*1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 Robert Clausecker4*/56#include <machine/asm.h>78ENTRY(timingsafe_bcmp)9cmp x2, #32 // at least 33 bytes to process?10bhi .Lgt321112cmp x2, #16 // at least 17 bytes to process?13bhi .L17321415cmp x2, #8 // at least 9 bytes to process?16bhi .L09161718cmp x2, #4 // at least 5 bytes to process?19bhi .L05082021cmp x2, #2 // at least 3 bytes to process?22bhi .L03042324cbnz x2, .L0102 // buffer empty?2526mov w0, #0 // empty buffer always matches27ret2829.L0102: ldrb w3, [x0] // load first bytes30ldrb w4, [x1]31sub x2, x2, #132ldrb w5, [x0, x2] // load last bytes33ldrb w6, [x1, x2]34eor w3, w3, w435eor w5, w5, w636orr w0, w3, w537ret3839.L0304: ldrh w3, [x0] // load first halfwords40ldrh w4, [x1]41sub x2, x2, #242ldrh w5, [x0, x2] // load last halfwords43ldrh w6, [x1, x2]44eor w3, w3, w445eor w5, w5, w646orr w0, w3, w547ret4849.L0508: ldr w3, [x0] // load first words50ldr w4, [x1]51sub x2, x2, #452ldr w5, [x0, x2] // load last words53ldr w6, [x1, x2]54eor w3, w3, w455eor w5, w5, w656orr w0, w3, w557ret5859.L0916: ldr x3, [x0]60ldr x4, [x1]61sub x2, x2, #862ldr x5, [x0, x2]63ldr x6, [x1, x2]64eor x3, x3, x465eor x5, x5, x666orr x0, x3, x567orr x0, x0, x0, lsr #32 // ensure low 32 bits are nonzero iff mismatch68ret6970.L1732: ldr q0, [x0]71ldr q1, [x1]72sub x2, x2, #1673ldr q2, [x0, x2]74ldr q3, [x1, x2]75eor v0.16b, v0.16b, v1.16b76eor v2.16b, v2.16b, v3.16b77orr v0.16b, v0.16b, v2.16b78umaxv s0, v0.4s // get a nonzero word if any79mov w0, v0.s[0]80ret8182/* more than 32 bytes: process buffer in a loop */83.Lgt32: ldp q0, q1, [x0], #3284ldp q2, q3, [x1], #3285eor v0.16b, v0.16b, v2.16b86eor v1.16b, v1.16b, v3.16b87orr v4.16b, v0.16b, v1.16b88subs x2, x2, #64 // enough left for another iteration?89bls .Ltail90910: ldp q0, q1, [x0], #3292ldp q2, q3, [x1], #3293eor v0.16b, v0.16b, v2.16b94eor v1.16b, v1.16b, v3.16b95orr v0.16b, v0.16b, v1.16b96orr v4.16b, v4.16b, v0.16b97subs x2, x2, #3298bhi 0b99100/* process last 32 bytes */101.Ltail: add x0, x0, x2 // point to the last 32 bytes in the buffer102add x1, x1, x2103ldp q0, q1, [x0]104ldp q2, q3, [x1]105eor v0.16b, v0.16b, v2.16b106eor v1.16b, v1.16b, v3.16b107orr v0.16b, v0.16b, v1.16b108orr v4.16b, v4.16b, v0.16b109umaxv s0, v4.4s // get a nonzero word if any110mov w0, v0.s[0]111ret112END(timingsafe_bcmp)113114115