Path: blob/main/lib/libc/aarch64/string/timingsafe_memcmp.S
48266 views
/*1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 Robert Clausecker4*/56#include <machine/asm.h>78ENTRY(timingsafe_memcmp)9cmp x2, #16 // at least 17 bytes to process?10bhi .Lgt161112cmp x2, #8 // at least 9 bytes to process?13bhi .L09161415cmp x2, #4 // at least 5 bytes to process?16bhi .L05081718cmp x2, #2 // at least 3 bytes to process?19bhi .L03042021cbnz x2, .L0102 // buffer empty?2223mov w0, #0 // empty buffer always matches24ret2526.L0102: ldrb w3, [x0] // load first bytes27ldrb w4, [x1]28sub x2, x2, #129ldrb w5, [x0, x2] // load last bytes30ldrb w6, [x1, x2]31bfi w5, w3, #8, #8 // join bytes in big endian32bfi w6, w4, #8, #833sub w0, w5, w634ret353637.L0304: ldrh w3, [x0] // load first halfwords38ldrh w4, [x1]39sub x2, x2, #240ldrh w5, [x0, x2] // load last halfwords41ldrh w6, [x1, x2]42bfi w3, w5, #16, #16 // join halfwords in little endian43bfi w4, w6, #16, #1644rev w3, w3 // swap word order45rev w4, w446cmp w3, w447csetm w0, lo // w0 = w3 >= w4 ? 0 : -148csinc w0, w0, wzr, ls // w0 = w3 <=> w4 ? 1 : 0 : -149ret5051.L0508: ldr w3, [x0] // load first words52ldr w4, [x1]53sub x2, x2, #454ldr w5, [x0, x2] // load last words55ldr w6, [x1, x2]56bfi x3, x5, #32, #32 // join words in little endian57bfi x4, x6, #32, #3258rev x3, x3 // swap word order59rev x4, x460cmp x3, x461csetm w0, lo // x0 = x3 >= w4 ? 0 : -162csinc w0, w0, wzr, ls // x0 = x3 <=> w4 ? 1 : 0 : -163ret6465.L0916: ldr x3, [x0]66ldr x4, [x1]67sub x2, x2, #868ldr x5, [x0, x2]69ldr x6, [x1, x2]70cmp x3, x4 // mismatch in first pair?71csel x3, x3, x5, ne // use second pair if first pair equal72csel x4, x4, x6, ne73rev x3, x374rev x4, x475cmp x3, x476csetm w0, lo77csinc w0, w0, wzr, ls78ret7980/* more than 16 bytes: process buffer in a loop */81.Lgt16: ldp x3, x4, [x0], #1682ldp x5, x6, [x1], #1683cmp x3, x5 // mismatch in first pair?84csel x3, x3, x4, ne // use second pair if first pair equal85csel x5, x5, x6, ne86subs x2, x2, #3287bls .Ltail88890: ldp x4, x7, [x0], #1690ldp x6, x8, [x1], #1691cmp x4, x6 // mismatch in first pair?92csel x4, x4, x7, ne // if not, try second pair93csel x6, x6, x8, ne94cmp x3, x5 // was there a mismatch previously?95csel x3, x3, x4, ne // apply new pair if there was not96csel x5, x5, x6, ne97subs x2, x2, #1698bhi 0b99100.Ltail: add x0, x0, x2101add x1, x1, x2102ldp x4, x7, [x0]103ldp x6, x8, [x1]104cmp x4, x6 // mismatch in first pair?105csel x4, x4, x7, ne // if not, try second pair106csel x6, x6, x8, ne107cmp x3, x5 // was there a mismatch previously?108csel x3, x3, x4, ne // apply new pair if there was not109csel x5, x5, x6, ne110rev x3, x3111rev x5, x5112cmp x3, x5113csetm w0, lo114csinc w0, w0, wzr, ls115ret116END(timingsafe_memcmp)117118119