2016-04-20 10:10:43 +08:00
|
|
|
#! /usr/bin/env perl
|
2020-04-23 20:55:52 +08:00
|
|
|
# Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2016-04-20 10:10:43 +08:00
|
|
|
#
|
2018-12-06 20:22:12 +08:00
|
|
|
# Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-04-20 10:10:43 +08:00
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2008-01-06 06:28:38 +08:00
|
|
|
$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
|
|
|
|
push(@INC,"${dir}","${dir}../../perlasm");
|
1998-12-21 19:00:56 +08:00
|
|
|
require "x86asm.pl";
|
|
|
|
|
Unify all assembler file generators
They now generally conform to the following argument sequence:
script.pl "$(PERLASM_SCHEME)" [ C preprocessor arguments ... ] \
$(PROCESSOR) <output file>
However, in the spirit of being able to use these scripts manually,
they also allow for no argument, or for only the flavour, or for only
the output file. This is done by only using the last argument as
output file if it's a file (it has an extension), and only using the
first argument as flavour if it isn't a file (it doesn't have an
extension).
While we're at it, we make all $xlate calls the same, i.e. the $output
argument is always quoted, and we always die on error when trying to
start $xlate.
There's a perl lesson in this, regarding operator priority...
This will always succeed, even when it fails:
open FOO, "something" || die "ERR: $!";
The reason is that '||' has higher priority than list operators (a
function is essentially a list operator and gobbles up everything
following it that isn't lower priority), and since a non-empty string
is always true, so that ends up being exactly the same as:
open FOO, "something";
This, however, will fail if "something" can't be opened:
open FOO, "something" or die "ERR: $!";
The reason is that 'or' has lower priority that list operators,
i.e. it's performed after the 'open' call.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9884)
2019-09-13 06:06:46 +08:00
|
|
|
$output = pop and open STDOUT,">$output";
|
2016-03-07 22:41:33 +08:00
|
|
|
|
2017-05-11 02:24:56 +08:00
|
|
|
&asm_init($ARGV[0]);
|
1998-12-21 19:00:56 +08:00
|
|
|
|
|
|
|
&bn_mul_comba("bn_mul_comba8",8);
|
|
|
|
&bn_mul_comba("bn_mul_comba4",4);
|
|
|
|
&bn_sqr_comba("bn_sqr_comba8",8);
|
|
|
|
&bn_sqr_comba("bn_sqr_comba4",4);
|
|
|
|
|
|
|
|
&asm_finish();
|
|
|
|
|
2020-02-17 10:17:53 +08:00
|
|
|
close STDOUT or die "error closing STDOUT: $!";
|
2016-03-07 22:41:33 +08:00
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
sub mul_add_c
|
|
|
|
{
|
|
|
|
local($a,$ai,$b,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
|
|
|
|
|
|
|
|
# pos == -1 if eax and edx are pre-loaded, 0 to load from next
|
|
|
|
# words, and 1 if load return value
|
|
|
|
|
|
|
|
&comment("mul a[$ai]*b[$bi]");
|
|
|
|
|
|
|
|
# "eax" and "edx" will always be pre-loaded.
|
|
|
|
# &mov("eax",&DWP($ai*4,$a,"",0)) ;
|
|
|
|
# &mov("edx",&DWP($bi*4,$b,"",0));
|
|
|
|
|
|
|
|
&mul("edx");
|
|
|
|
&add($c0,"eax");
|
2020-02-03 05:54:01 +08:00
|
|
|
&mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # load next a
|
1998-12-21 19:00:56 +08:00
|
|
|
&mov("eax",&wparam(0)) if $pos > 0; # load r[]
|
|
|
|
###
|
|
|
|
&adc($c1,"edx");
|
2020-02-03 05:54:01 +08:00
|
|
|
&mov("edx",&DWP(($nb)*4,$b,"",0)) if $pos == 0; # load next b
|
|
|
|
&mov("edx",&DWP(($nb)*4,$b,"",0)) if $pos == 1; # load next b
|
1998-12-21 19:00:56 +08:00
|
|
|
###
|
|
|
|
&adc($c2,0);
|
2016-10-11 00:01:24 +08:00
|
|
|
# is pos > 1, it means it is the last loop
|
1998-12-21 19:00:56 +08:00
|
|
|
&mov(&DWP($i*4,"eax","",0),$c0) if $pos > 0; # save r[];
|
2020-02-03 05:54:01 +08:00
|
|
|
&mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # load next a
|
1998-12-21 19:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub sqr_add_c
|
|
|
|
{
|
|
|
|
local($r,$a,$ai,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
|
|
|
|
|
|
|
|
# pos == -1 if eax and edx are pre-loaded, 0 to load from next
|
|
|
|
# words, and 1 if load return value
|
|
|
|
|
|
|
|
&comment("sqr a[$ai]*a[$bi]");
|
|
|
|
|
|
|
|
# "eax" and "edx" will always be pre-loaded.
|
|
|
|
# &mov("eax",&DWP($ai*4,$a,"",0)) ;
|
|
|
|
# &mov("edx",&DWP($bi*4,$b,"",0));
|
|
|
|
|
|
|
|
if ($ai == $bi)
|
|
|
|
{ &mul("eax");}
|
|
|
|
else
|
|
|
|
{ &mul("edx");}
|
|
|
|
&add($c0,"eax");
|
|
|
|
&mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # load next a
|
|
|
|
###
|
|
|
|
&adc($c1,"edx");
|
|
|
|
&mov("edx",&DWP(($nb)*4,$a,"",0)) if ($pos == 1) && ($na != $nb);
|
|
|
|
###
|
|
|
|
&adc($c2,0);
|
2016-10-11 00:01:24 +08:00
|
|
|
# is pos > 1, it means it is the last loop
|
1998-12-21 19:00:56 +08:00
|
|
|
&mov(&DWP($i*4,$r,"",0),$c0) if $pos > 0; # save r[];
|
|
|
|
&mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # load next b
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sqr_add_c2
|
|
|
|
{
|
|
|
|
local($r,$a,$ai,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
|
|
|
|
|
|
|
|
# pos == -1 if eax and edx are pre-loaded, 0 to load from next
|
|
|
|
# words, and 1 if load return value
|
|
|
|
|
|
|
|
&comment("sqr a[$ai]*a[$bi]");
|
|
|
|
|
|
|
|
# "eax" and "edx" will always be pre-loaded.
|
|
|
|
# &mov("eax",&DWP($ai*4,$a,"",0)) ;
|
|
|
|
# &mov("edx",&DWP($bi*4,$a,"",0));
|
|
|
|
|
|
|
|
if ($ai == $bi)
|
|
|
|
{ &mul("eax");}
|
|
|
|
else
|
|
|
|
{ &mul("edx");}
|
|
|
|
&add("eax","eax");
|
|
|
|
###
|
|
|
|
&adc("edx","edx");
|
|
|
|
###
|
|
|
|
&adc($c2,0);
|
|
|
|
&add($c0,"eax");
|
|
|
|
&adc($c1,"edx");
|
|
|
|
&mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # load next a
|
|
|
|
&mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # load next b
|
|
|
|
&adc($c2,0);
|
|
|
|
&mov(&DWP($i*4,$r,"",0),$c0) if $pos > 0; # save r[];
|
|
|
|
&mov("edx",&DWP(($nb)*4,$a,"",0)) if ($pos <= 1) && ($na != $nb);
|
|
|
|
###
|
|
|
|
}
|
|
|
|
|
|
|
|
sub bn_mul_comba
|
|
|
|
{
|
|
|
|
local($name,$num)=@_;
|
|
|
|
local($a,$b,$c0,$c1,$c2);
|
|
|
|
local($i,$as,$ae,$bs,$be,$ai,$bi);
|
|
|
|
local($tot,$end);
|
|
|
|
|
|
|
|
&function_begin_B($name,"");
|
|
|
|
|
|
|
|
$c0="ebx";
|
|
|
|
$c1="ecx";
|
|
|
|
$c2="ebp";
|
|
|
|
$a="esi";
|
|
|
|
$b="edi";
|
2016-10-11 00:01:24 +08:00
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
$as=0;
|
|
|
|
$ae=0;
|
|
|
|
$bs=0;
|
|
|
|
$be=0;
|
|
|
|
$tot=$num+$num-1;
|
|
|
|
|
|
|
|
&push("esi");
|
|
|
|
&mov($a,&wparam(1));
|
|
|
|
&push("edi");
|
|
|
|
&mov($b,&wparam(2));
|
|
|
|
&push("ebp");
|
|
|
|
&push("ebx");
|
|
|
|
|
|
|
|
&xor($c0,$c0);
|
2016-10-11 00:01:24 +08:00
|
|
|
&mov("eax",&DWP(0,$a,"",0)); # load the first word
|
1998-12-21 19:00:56 +08:00
|
|
|
&xor($c1,$c1);
|
2016-10-11 00:01:24 +08:00
|
|
|
&mov("edx",&DWP(0,$b,"",0)); # load the first second
|
1998-12-21 19:00:56 +08:00
|
|
|
|
|
|
|
for ($i=0; $i<$tot; $i++)
|
|
|
|
{
|
|
|
|
$ai=$as;
|
|
|
|
$bi=$bs;
|
|
|
|
$end=$be+1;
|
|
|
|
|
2016-10-11 00:01:24 +08:00
|
|
|
&comment("################## Calculate word $i");
|
1998-12-21 19:00:56 +08:00
|
|
|
|
|
|
|
for ($j=$bs; $j<$end; $j++)
|
|
|
|
{
|
|
|
|
&xor($c2,$c2) if ($j == $bs);
|
|
|
|
if (($j+1) == $end)
|
|
|
|
{
|
|
|
|
$v=1;
|
|
|
|
$v=2 if (($i+1) == $tot);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ $v=0; }
|
|
|
|
if (($j+1) != $end)
|
|
|
|
{
|
|
|
|
$na=($ai-1);
|
|
|
|
$nb=($bi+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$na=$as+($i < ($num-1));
|
|
|
|
$nb=$bs+($i >= ($num-1));
|
|
|
|
}
|
|
|
|
#printf STDERR "[$ai,$bi] -> [$na,$nb]\n";
|
|
|
|
&mul_add_c($a,$ai,$b,$bi,$c0,$c1,$c2,$v,$i,$na,$nb);
|
|
|
|
if ($v)
|
|
|
|
{
|
|
|
|
&comment("saved r[$i]");
|
|
|
|
# &mov("eax",&wparam(0));
|
|
|
|
# &mov(&DWP($i*4,"eax","",0),$c0);
|
|
|
|
($c0,$c1,$c2)=($c1,$c2,$c0);
|
|
|
|
}
|
|
|
|
$ai--;
|
|
|
|
$bi++;
|
|
|
|
}
|
|
|
|
$as++ if ($i < ($num-1));
|
|
|
|
$ae++ if ($i >= ($num-1));
|
|
|
|
|
|
|
|
$bs++ if ($i >= ($num-1));
|
|
|
|
$be++ if ($i < ($num-1));
|
|
|
|
}
|
|
|
|
&comment("save r[$i]");
|
|
|
|
# &mov("eax",&wparam(0));
|
|
|
|
&mov(&DWP($i*4,"eax","",0),$c0);
|
|
|
|
|
|
|
|
&pop("ebx");
|
|
|
|
&pop("ebp");
|
|
|
|
&pop("edi");
|
|
|
|
&pop("esi");
|
|
|
|
&ret();
|
|
|
|
&function_end_B($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub bn_sqr_comba
|
|
|
|
{
|
|
|
|
local($name,$num)=@_;
|
|
|
|
local($r,$a,$c0,$c1,$c2)=@_;
|
|
|
|
local($i,$as,$ae,$bs,$be,$ai,$bi);
|
|
|
|
local($b,$tot,$end,$half);
|
|
|
|
|
|
|
|
&function_begin_B($name,"");
|
|
|
|
|
|
|
|
$c0="ebx";
|
|
|
|
$c1="ecx";
|
|
|
|
$c2="ebp";
|
|
|
|
$a="esi";
|
|
|
|
$r="edi";
|
|
|
|
|
|
|
|
&push("esi");
|
|
|
|
&push("edi");
|
|
|
|
&push("ebp");
|
|
|
|
&push("ebx");
|
|
|
|
&mov($r,&wparam(0));
|
|
|
|
&mov($a,&wparam(1));
|
|
|
|
&xor($c0,$c0);
|
|
|
|
&xor($c1,$c1);
|
|
|
|
&mov("eax",&DWP(0,$a,"",0)); # load the first word
|
|
|
|
|
|
|
|
$as=0;
|
|
|
|
$ae=0;
|
|
|
|
$bs=0;
|
|
|
|
$be=0;
|
|
|
|
$tot=$num+$num-1;
|
|
|
|
|
|
|
|
for ($i=0; $i<$tot; $i++)
|
|
|
|
{
|
|
|
|
$ai=$as;
|
|
|
|
$bi=$bs;
|
|
|
|
$end=$be+1;
|
|
|
|
|
|
|
|
&comment("############### Calculate word $i");
|
|
|
|
for ($j=$bs; $j<$end; $j++)
|
|
|
|
{
|
|
|
|
&xor($c2,$c2) if ($j == $bs);
|
|
|
|
if (($ai-1) < ($bi+1))
|
|
|
|
{
|
|
|
|
$v=1;
|
|
|
|
$v=2 if ($i+1) == $tot;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ $v=0; }
|
|
|
|
if (!$v)
|
|
|
|
{
|
|
|
|
$na=$ai-1;
|
|
|
|
$nb=$bi+1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$na=$as+($i < ($num-1));
|
|
|
|
$nb=$bs+($i >= ($num-1));
|
|
|
|
}
|
|
|
|
if ($ai == $bi)
|
|
|
|
{
|
|
|
|
&sqr_add_c($r,$a,$ai,$bi,
|
|
|
|
$c0,$c1,$c2,$v,$i,$na,$nb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
&sqr_add_c2($r,$a,$ai,$bi,
|
|
|
|
$c0,$c1,$c2,$v,$i,$na,$nb);
|
|
|
|
}
|
|
|
|
if ($v)
|
|
|
|
{
|
|
|
|
&comment("saved r[$i]");
|
|
|
|
#&mov(&DWP($i*4,$r,"",0),$c0);
|
|
|
|
($c0,$c1,$c2)=($c1,$c2,$c0);
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
$ai--;
|
|
|
|
$bi++;
|
|
|
|
}
|
|
|
|
$as++ if ($i < ($num-1));
|
|
|
|
$ae++ if ($i >= ($num-1));
|
|
|
|
|
|
|
|
$bs++ if ($i >= ($num-1));
|
|
|
|
$be++ if ($i < ($num-1));
|
|
|
|
}
|
|
|
|
&mov(&DWP($i*4,$r,"",0),$c0);
|
|
|
|
&pop("ebx");
|
|
|
|
&pop("ebp");
|
|
|
|
&pop("edi");
|
|
|
|
&pop("esi");
|
|
|
|
&ret();
|
|
|
|
&function_end_B($name);
|
|
|
|
}
|