riscv64cpuid.pl 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. # $output is the last argument if it looks like a file (it has an extension)
  9. # $flavour is the first argument if it doesn't look like a file
  10. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  11. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  12. $output and open STDOUT,">$output";
  13. {
  14. my ($in_a,$in_b,$len,$x,$temp1,$temp2) = ('a0','a1','a2','t0','t1','t2');
  15. $code.=<<___;
  16. ################################################################################
  17. # int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
  18. ################################################################################
  19. .text
  20. .balign 16
  21. .globl CRYPTO_memcmp
  22. .type CRYPTO_memcmp,\@function
  23. CRYPTO_memcmp:
  24. li $x,0
  25. beqz $len,2f # len == 0
  26. 1:
  27. lbu $temp1,0($in_a)
  28. lbu $temp2,0($in_b)
  29. addi $in_a,$in_a,1
  30. addi $in_b,$in_b,1
  31. addi $len,$len,-1
  32. xor $temp1,$temp1,$temp2
  33. or $x,$x,$temp1
  34. bgtz $len,1b
  35. 2:
  36. mv a0,$x
  37. ret
  38. ___
  39. }
  40. {
  41. my ($ptr,$len,$temp1,$temp2) = ('a0','a1','t0','t1');
  42. $code.=<<___;
  43. ################################################################################
  44. # void OPENSSL_cleanse(void *ptr, size_t len)
  45. ################################################################################
  46. .text
  47. .balign 16
  48. .globl OPENSSL_cleanse
  49. .type OPENSSL_cleanse,\@function
  50. OPENSSL_cleanse:
  51. beqz $len,2f # len == 0, return
  52. srli $temp1,$len,4
  53. bnez $temp1,3f # len > 15
  54. 1: # Store <= 15 individual bytes
  55. sb x0,0($ptr)
  56. addi $ptr,$ptr,1
  57. addi $len,$len,-1
  58. bnez $len,1b
  59. 2:
  60. ret
  61. 3: # Store individual bytes until we are aligned
  62. andi $temp1,$ptr,0x7
  63. beqz $temp1,4f
  64. sb x0,0($ptr)
  65. addi $ptr,$ptr,1
  66. addi $len,$len,-1
  67. j 3b
  68. 4: # Store aligned dwords
  69. li $temp2,8
  70. 4:
  71. sd x0,0($ptr)
  72. addi $ptr,$ptr,8
  73. addi $len,$len,-8
  74. bge $len,$temp2,4b # if len>=8 loop
  75. bnez $len,1b # if len<8 and len != 0, store remaining bytes
  76. ret
  77. ___
  78. }
  79. print $code;
  80. close STDOUT or die "error closing STDOUT: $!";