xref: /openssl/crypto/riscv32cpuid.pl (revision 7ed6de99)
1#! /usr/bin/env perl
2# Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9
10# $output is the last argument if it looks like a file (it has an extension)
11# $flavour is the first argument if it doesn't look like a file
12$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
13$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
14
15$output and open STDOUT,">$output";
16
17{
18my ($in_a,$in_b,$len,$x,$temp1,$temp2) = ('a0','a1','a2','t0','t1','t2');
19$code.=<<___;
20################################################################################
21# int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
22################################################################################
23.text
24.balign 16
25.globl CRYPTO_memcmp
26.type   CRYPTO_memcmp,\@function
27CRYPTO_memcmp:
28    li      $x,0
29    beqz    $len,2f   # len == 0
301:
31    lbu     $temp1,0($in_a)
32    lbu     $temp2,0($in_b)
33    addi    $in_a,$in_a,1
34    addi    $in_b,$in_b,1
35    addi    $len,$len,-1
36    xor     $temp1,$temp1,$temp2
37    or      $x,$x,$temp1
38    bgtz    $len,1b
392:
40    mv      a0,$x
41    ret
42___
43}
44{
45my ($ptr,$len,$temp1,$temp2) = ('a0','a1','t0','t1');
46$code.=<<___;
47################################################################################
48# void OPENSSL_cleanse(void *ptr, size_t len)
49################################################################################
50.text
51.balign 16
52.globl OPENSSL_cleanse
53.type   OPENSSL_cleanse,\@function
54OPENSSL_cleanse:
55    beqz    $len,2f         # len == 0, return
56    srli    $temp1,$len,4
57    bnez    $temp1,3f       # len > 15
58
591:  # Store <= 15 individual bytes
60    sb      x0,0($ptr)
61    addi    $ptr,$ptr,1
62    addi    $len,$len,-1
63    bnez    $len,1b
642:
65    ret
66
673:  # Store individual bytes until we are aligned
68    andi    $temp1,$ptr,0x3
69    beqz    $temp1,4f
70    sb      x0,0($ptr)
71    addi    $ptr,$ptr,1
72    addi    $len,$len,-1
73    j       3b
74
754:  # Store aligned words
76    li      $temp2,4
774:
78    sw      x0,0($ptr)
79    addi    $ptr,$ptr,4
80    addi    $len,$len,-4
81    bge     $len,$temp2,4b  # if len>=4 loop
82    bnez    $len,1b         # if len<4 and len != 0, store remaining bytes
83    ret
84___
85}
86
87{
88my ($ret) = ('a0');
89$code .= <<___;
90################################################################################
91# size_t riscv_vlen_asm(void)
92# Return VLEN (i.e. the length of a vector register in bits).
93.p2align 3
94.globl riscv_vlen_asm
95.type riscv_vlen_asm,\@function
96riscv_vlen_asm:
97    csrr $ret, vlenb
98    slli $ret, $ret, 3
99    ret
100.size riscv_vlen_asm,.-riscv_vlen_asm
101___
102}
103
104print $code;
105close STDOUT or die "error closing STDOUT: $!";
106