/*
 * Copyright (c) The mldsa-native project authors
 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
 */
/*yaml
  Name: poly_chknorm_aarch64_asm
  Description: AArch64 infinity-norm bound check on polynomial coefficients
  Signature: int mld_poly_chknorm_aarch64_asm(const int32_t *a, int32_t B)
  ABI:
    Architecture: aarch64
    CallingConvention: AAPCS64
    x0:
      type: buffer
      size_bytes: 1024
      permissions: read-only
      c_parameter: const int32_t *a
      description: Input polynomial (256 x int32_t)
    x1:
      type: scalar
      c_parameter: int32_t B
      description: Norm bound
      test_with: 131072  # representative non-negative bound (1 << 17)
*/

#include "../../../common.h"

#if defined(MLD_ARITH_BACKEND_AARCH64) && !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED)

/*
 * WARNING: This file is auto-derived from the mldsa-native source file
 *   dev/aarch64_opt/src/poly_chknorm_aarch64_asm.S using scripts/simpasm. Do not modify it directly.
 */

.text
.balign 4
.global MLD_ASM_NAMESPACE(poly_chknorm_aarch64_asm)
MLD_ASM_FN_SYMBOL(poly_chknorm_aarch64_asm)

        .cfi_startproc
        dup v20.4s, w1
        eor v21.16b, v21.16b, v21.16b
        mov x2, #0x10               // =16

Lpoly_chknorm_loop:
        ldr q1, [x0, #0x10]
        ldr q2, [x0, #0x20]
        ldr q3, [x0, #0x30]
        ldr q0, [x0], #0x40
        abs v1.4s, v1.4s
        cmge v1.4s, v1.4s, v20.4s
        orr v21.16b, v21.16b, v1.16b
        abs v2.4s, v2.4s
        cmge v2.4s, v2.4s, v20.4s
        orr v21.16b, v21.16b, v2.16b
        abs v3.4s, v3.4s
        cmge v3.4s, v3.4s, v20.4s
        orr v21.16b, v21.16b, v3.16b
        abs v0.4s, v0.4s
        cmge v0.4s, v0.4s, v20.4s
        orr v21.16b, v21.16b, v0.16b
        subs x2, x2, #0x1
        b.ne Lpoly_chknorm_loop
        umaxv s21, v21.4s
        fmov w0, s21
        and w0, w0, #0x1
        ret
        .cfi_endproc

MLD_ASM_FN_SIZE(poly_chknorm_aarch64_asm)

#endif /* MLD_ARITH_BACKEND_AARCH64 && !MLD_CONFIG_MULTILEVEL_NO_SHARED */

#if defined(__ELF__)
.section .note.GNU-stack,"",%progbits
#endif
