Skip to content

Commit

Permalink
armv9 platform + ntt-sve2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
mkannwischer committed Jul 26, 2024
1 parent 0c296a0 commit ce3139b
Show file tree
Hide file tree
Showing 17 changed files with 217 additions and 10,013 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include tests/keccak-neon/keccak-neon.mk
include tests/ntt-dilithium/ntt-dilithium.mk
include tests/ntt-kyber/ntt-kyber.mk
include tests/ntt-neon/ntt-neon.mk
include tests/ntt-sve2/ntt-sve2.mk

testname = $(shell echo $(1) | tr '[a-z]' '[A-Z]' | tr '-' '_' | tr '/' '_')
testdir = $(addprefix $(2),tests/$(firstword $(subst /, ,$1))/)
Expand Down
3 changes: 3 additions & 0 deletions envs/cross-v9a/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CFLAGS += -march=armv9-a

include ../common/cross.mk
9 changes: 9 additions & 0 deletions envs/cross-v9a/inc/hal_env.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef QEMU_V8A_HAL_ENV_H
#define QEMU_V8A_HAL_ENV_H

#define SEP ;

#define ASM_LOAD(dst,symbol) \
adrp dst, symbol ; add dst, dst, :lo12:symbol;

#endif
172 changes: 172 additions & 0 deletions envs/cross-v9a/src/hal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2022 Arm Limited
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include <hal.h>

/* Dependency on standard library:
* - rand(), srand()
* - time()
* - printf()
* - fflush()
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>

#define FILENO stderr

void rand_init( unsigned long seed )
{
((void) seed);
srand(time(NULL));
}

uint8_t get_random_byte()
{
return( rand() );
}

/* Debugging stubs */

void debug_test_start( const char *testname )
{
fprintf( FILENO, "%s ... ", testname );
fflush( FILENO );
}

void debug_printf(const char * format, ... )
{
va_list argp;
va_start( argp, format );
vfprintf( FILENO, format, argp );
va_end( argp );
}

void debug_test_ok() { printf( "Ok\n" ); }
void debug_test_fail() { printf( "FAIL!\n" ); }


#if !defined(EXTERNAL_CYCLES) && !defined(PERF_CYCLES) && !defined(PMU_CYCLES) && !defined(NO_CYCLES)
#define NO_CYCLES
#endif

#if defined(PMU_CYCLES)
void enable_cyclecounter() {
uint64_t tmp;
__asm __volatile (
"mrs %[tmp], pmcr_el0\n"
"orr %[tmp], %[tmp], #1\n"
"msr pmcr_el0, %[tmp]\n"
"mrs %[tmp], pmcntenset_el0\n"
"orr %[tmp], %[tmp], #1<<31\n"
"msr pmcntenset_el0, %[tmp]\n"
: [tmp] "=r" (tmp)
);
}

void disable_cyclecounter() {
uint64_t tmp;
__asm __volatile (
"mov %[tmp], #0x3f\n"
"orr %[tmp], %[tmp], #1<<31\n"
"msr pmcntenclr_el0, %[tmp]\n"
: [tmp] "=r" (tmp)
);
}

uint64_t get_cyclecounter() {
uint64_t retval;
__asm __volatile (
"mrs %[retval], pmccntr_el0\n"
: [retval] "=r" (retval));
return retval;
}

#elif defined(PERF_CYCLES)

#include <asm/unistd.h>
#include <linux/perf_event.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

static int perf_fd = 0;
void enable_cyclecounter() {
struct perf_event_attr pe;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_CPU_CYCLES;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;

perf_fd = syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0);

ioctl(perf_fd, PERF_EVENT_IOC_RESET, 0);
ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0);
}

void disable_cyclecounter() {
ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0);
close(perf_fd);
}

uint64_t get_cyclecounter() {
long long cpu_cycles;
ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0);
ssize_t read_count = read(perf_fd, &cpu_cycles, sizeof(cpu_cycles));
if (read_count < 0) {
perror("read");
exit(EXIT_FAILURE);
} else if (read_count == 0) {
/* Should not happen */
printf("perf counter empty\n");
exit(EXIT_FAILURE);
}
ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0);
return cpu_cycles;
}

#elif defined(EXTERNAL_CYCLES)

// nothing to do

#else /* NO_CYCLES */

void enable_cyclecounter() {
return;
}
void disable_cyclecounter() {
return;
}
uint64_t get_cyclecounter() {
return(0);
}

#endif /* NO_CYCLES */
5 changes: 5 additions & 0 deletions tests/common/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
GEN_FILL_RANDOM(8)
GEN_FILL_RANDOM(16)
GEN_FILL_RANDOM(32)
GEN_FILL_RANDOM(64)
#undef GEN_FILL_RANDOM

#define GEN_COPY( bits ) \
Expand All @@ -52,6 +53,7 @@
GEN_COPY(8)
GEN_COPY(16)
GEN_COPY(32)
GEN_COPY(64)
#undef GEN_COPY

#define GEN_COMPARE_BUF( bits ) \
Expand All @@ -67,6 +69,7 @@
GEN_COMPARE_BUF(8)
GEN_COMPARE_BUF(16)
GEN_COMPARE_BUF(32)
GEN_COMPARE_BUF(64)
#undef GEN_COMPARE_BUF

#define GEN_PRINT_BUF( bits ) \
Expand All @@ -86,6 +89,7 @@
GEN_PRINT_BUF(8)
GEN_PRINT_BUF(16)
GEN_PRINT_BUF(32)
GEN_PRINT_BUF(64)
#undef GEN_PRINT_BUF

#define GEN_PRINT_BUF_S( bits ) \
Expand All @@ -105,6 +109,7 @@
GEN_PRINT_BUF_S(8)
GEN_PRINT_BUF_S(16)
GEN_PRINT_BUF_S(32)
GEN_PRINT_BUF_S(64)
#undef GEN_PRINT_BUF_S

/* Helper to transpose buffers in case this is needed for input preparation. */
Expand Down
8 changes: 7 additions & 1 deletion tests/common/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,21 @@
void fill_random_u8 ( uint8_t *buf, unsigned len );
void fill_random_u16( uint16_t *buf, unsigned len );
void fill_random_u32( uint32_t *buf, unsigned len );
void fill_random_u64( uint64_t *buf, unsigned len );

/* Copy buffers */
void copy_buf_u8 ( uint8_t *dst, uint8_t const *src, unsigned len );
void copy_buf_u16( uint16_t *dst, uint16_t const *src, unsigned len );
void copy_buf_u32( uint32_t *dst, uint32_t const *src, unsigned len );
void copy_buf_u64( uint64_t *dst, uint64_t const *src, unsigned len );

/* Compare buffers
* Same semantics as memcmp(), but we want to rely on stdlib
* as little as possible. */
int compare_buf_u8 ( uint8_t const *src_a, uint8_t const *src_b, unsigned len );
int compare_buf_u16( uint16_t const *src_a, uint16_t const *src_b, unsigned len );
int compare_buf_u32( uint32_t const *src_a, uint32_t const *src_b, unsigned len );
int compare_buf_u64( uint64_t const *src_a, uint64_t const *src_b, unsigned len );

static inline int compare_buf_s8( int8_t const *src_a,
int8_t const *src_b,
Expand Down Expand Up @@ -109,9 +112,11 @@ static inline int compare_buf_s32( int32_t const *src_a,
void debug_print_buf_u8 ( uint8_t const *buf, unsigned entries, const char *prefix );
void debug_print_buf_u16( uint16_t const *buf, unsigned entries, const char *prefix );
void debug_print_buf_u32( uint32_t const *buf, unsigned entries, const char *prefix );
void debug_print_buf_u64( uint64_t const *buf, unsigned entries, const char *prefix );
void debug_print_buf_s8 ( int8_t const *buf, unsigned entries, const char *prefix );
void debug_print_buf_s16( int16_t const *buf, unsigned entries, const char *prefix );
void debug_print_buf_s32( int32_t const *buf, unsigned entries, const char *prefix );
void debug_print_buf_s64( int64_t const *buf, unsigned entries, const char *prefix );

/* Transpose buffers */
void buffer_transpose_u8 ( uint8_t *dst, uint8_t const *src,
Expand All @@ -120,7 +125,8 @@ void buffer_transpose_u16( uint16_t *dst, uint16_t const *src,
unsigned block_length, unsigned dim_x, unsigned dim_y );
void buffer_transpose_u32( uint32_t *dst, uint32_t const *src,
unsigned block_length, unsigned dim_x, unsigned dim_y );

void buffer_transpose_u64( uint64_t *dst, uint64_t const *src,
unsigned block_length, unsigned dim_x, unsigned dim_y );
#define ALIGN(x) __attribute__((aligned(x)))

#endif /* MVE_POLY_ARITHMETIC_TESTS_MISC */
File renamed without changes.
20 changes: 20 additions & 0 deletions tests/ntt-sve2/ntt-sve2.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Test name - needs to match the directory name
TESTS += ntt-sve2

# All further variables must be prefixed with the capitalized test name

# Platforms this test should run on (matching the directory name in envs/)
NTT_SVE2_PLATFORMS += cross-v9a

# C sources required for this test
NTT_SVE2_SOURCES += main.c
NTT_SVE2_SOURCES += ntt.c

# Assembly sources required for this test
NTT_SVE2_ASMS += ../../asm/manual/basemul_s64/basemul_64_72057594067788289.s

NTT_SVE2_ASMDIR = ../../asm/auto/ntt_sve2
NTT_SVE2_ASMS += $(NTT_SVE2_ASMDIR)/ntt_u32_incomplete_33556993_28678040_var_3_3_0.s
NTT_SVE2_ASMS += $(NTT_SVE2_ASMDIR)/ntt_u64_incomplete_72057594067788289_60277548896192635_var_3_3_0.s
NTT_SVE2_ASMS += $(NTT_SVE2_ASMDIR)/ntt_u64_incomplete_72057594067788289_60277548896192635_var_3_3_1.s
NTT_SVE2_ASMS += $(NTT_SVE2_ASMDIR)/ntt_u64_incomplete_72057594067788289_60277548896192635_var_3_3_2.s
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ce3139b

Please sign in to comment.