xref: /openssl/crypto/loongarch64cpuid.pl (revision a607546e)
1#! /usr/bin/env perl
2# Copyright 2022 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($zero,$ra,$tp,$sp)=map("\$r$_",(0..3));
13($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$r$_",(4..11));
14($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9)=map("\$r$_",(12..21));
15($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$r$_",(23..30));
16($vr0,$vr1,$vr2,$vr3,$vr4,$vr5,$vr6,$vr7,$vr8,$vr9,$vr10,$vr11,$vr12,$vr13,$vr14,$vr15,$vr16,$vr17,$vr18,$vr19)=map("\$vr$_",(0..19));
17($fp)=map("\$r$_",(22));
18
19
20for (@ARGV) {   $output=$_ if (/\w[\w\-]*\.\w+$/);      }
21open STDOUT,">$output";
22while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
23open STDOUT,">$output";
24
25{
26my ($in_a,$in_b,$len,$m,$temp1,$temp2) = ($a0,$a1,$a2,$t0,$t1,$t2);
27$code.=<<___;
28################################################################################
29# int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
30################################################################################
31.text
32.balign 16
33.globl CRYPTO_memcmp
34.type   CRYPTO_memcmp,\@function
35CRYPTO_memcmp:
36    li.d    $m,0
37    beqz    $len,2f   # len == 0
381:
39    ld.bu   $temp1,$in_a,0
40    ld.bu   $temp2,$in_b,0
41    addi.d  $in_a,$in_a,1
42    addi.d  $in_b,$in_b,1
43    addi.d  $len,$len,-1
44    xor     $temp1,$temp1,$temp2
45    or      $m,$m,$temp1
46    blt     $zero,$len,1b
472:
48    move    $a0,$m
49    jr      $ra
50___
51}
52{
53my ($ptr,$len,$temp1,$temp2) = ($a0,$a1,$t0,$t1);
54$code.=<<___;
55################################################################################
56# void OPENSSL_cleanse(void *ptr, size_t len)
57################################################################################
58.text
59.balign 16
60.globl OPENSSL_cleanse
61.type   OPENSSL_cleanse,\@function
62OPENSSL_cleanse:
63    beqz    $len,2f         # len == 0, return
64    srli.d  $temp1,$len,4
65    bnez    $temp1,3f       # len > 15
66
671:  # Store <= 15 individual bytes
68    st.b    $zero,$ptr,0
69    addi.d  $ptr,$ptr,1
70    addi.d  $len,$len,-1
71    bnez    $len,1b
722:
73    jr      $ra
74
753:  # Store individual bytes until we are aligned
76    andi    $temp1,$ptr,0x7
77    beqz    $temp1,4f
78    st.b    $zero,$ptr,0
79    addi.d  $ptr,$ptr,1
80    addi.d  $len,$len,-1
81    b       3b
82
834:  # Store aligned dwords
84    li.d    $temp2,8
854:
86    st.d    $zero,$ptr,0
87    addi.d  $ptr,$ptr,8
88    addi.d  $len,$len,-8
89    bge     $len,$temp2,4b  # if len>=8 loop
90    bnez    $len,1b         # if len<8 and len != 0, store remaining bytes
91    jr      $ra
92___
93}
94{
95$code.=<<___;
96################################################################################
97# uint32_t OPENSSL_rdtsc(void)
98################################################################################
99.text
100.balign 16
101.globl OPENSSL_rdtsc
102.type   OPENSSL_rdtsc,\@function
103OPENSSL_rdtsc:
104    rdtimel.w $a0,$zero
105    jr        $ra
106___
107}
108
109$code =~ s/\`([^\`]*)\`/eval($1)/gem;
110
111print $code;
112
113close STDOUT or die "error closing STDOUT: $!";
114