Optimize cdot function for POWER10
[platform/upstream/openblas.git] / c_check
1 #!/usr/bin/perl
2
3 #use File::Basename;
4 # use File::Temp qw(tempfile);
5
6 # Checking cross compile
7 $hostos   = `uname -s | sed -e s/\-.*//`;    chop($hostos);
8 $hostarch = `uname -m | sed -e s/i.86/x86/`;
9 $hostarch = `uname -p` if ($hostos eq "AIX" || $hostos eq "SunOS");
10 chop($hostarch);
11 $hostarch = "x86_64" if ($hostarch eq "amd64");
12 $hostarch = "arm" if ($hostarch ne "arm64" && $hostarch =~ /^arm.*/);
13 $hostarch = "arm64" if ($hostarch eq "aarch64");
14 $hostarch = "power" if ($hostarch =~ /^(powerpc|ppc).*/);
15 $hostarch = "zarch" if ($hostarch eq "s390x");
16
17 #$tmpf = new File::Temp( UNLINK => 1 );
18 $binary = $ENV{"BINARY"};
19
20 $makefile = shift(@ARGV);
21 $config   = shift(@ARGV);
22
23 $compiler_name = shift(@ARGV);
24 $flags = join(" ", @ARGV);
25
26 # First, we need to know the target OS and compiler name
27
28 $data = `$compiler_name $flags -E ctest.c`;
29
30 if ($?) {
31     printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
32     die 1;
33 }
34
35 $cross_suffix = "";
36
37 eval "use File::Basename";
38 if ($@){ 
39     warn "could not load PERL module File::Basename, emulating its functionality";
40     my $dirnam = substr($compiler_name, 0, rindex($compiler_name, "/")-1 );
41     if ($dirnam ne ".") {
42         $cross_suffix .= $dirnam . "/";
43     }
44     my $basnam = substr($compiler_name, rindex($compiler_name,"/")+1, length($compiler_name)-rindex($compiler_name,"/")-1);
45         if ($basnam =~ /([^\s]*-)(.*)/) {
46         $cross_suffix .= $1;
47     }
48 } else {
49     if (dirname($compiler_name) ne ".") {
50         $cross_suffix .= dirname($compiler_name) . "/";
51     }
52
53     if (basename($compiler_name) =~ /([^\s]*-)(.*)/) {
54         $cross_suffix .= $1;
55     }
56 }
57
58 $compiler = "";
59 $compiler = LSB       if ($data =~ /COMPILER_LSB/);
60 $compiler = CLANG     if ($data =~ /COMPILER_CLANG/);
61 $compiler = PGI       if ($data =~ /COMPILER_PGI/);
62 $compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
63 $compiler = INTEL     if ($data =~ /COMPILER_INTEL/);
64 $compiler = OPEN64    if ($data =~ /COMPILER_OPEN64/);
65 $compiler = SUN       if ($data =~ /COMPILER_SUN/);
66 $compiler = IBM       if ($data =~ /COMPILER_IBM/);
67 $compiler = DEC       if ($data =~ /COMPILER_DEC/);
68 $compiler = GCC       if ($compiler eq "");
69
70 $os = Linux           if ($data =~ /OS_LINUX/);
71 $os = FreeBSD         if ($data =~ /OS_FREEBSD/);
72 $os = NetBSD          if ($data =~ /OS_NETBSD/);
73 $os = OpenBSD         if ($data =~ /OS_OPENBSD/);
74 $os = DragonFly       if ($data =~ /OS_DRAGONFLY/);
75 $os = Darwin          if ($data =~ /OS_DARWIN/);
76 $os = SunOS           if ($data =~ /OS_SUNOS/);
77 $os = AIX             if ($data =~ /OS_AIX/);
78 $os = osf             if ($data =~ /OS_OSF/);
79 $os = WINNT           if ($data =~ /OS_WINNT/);
80 $os = CYGWIN_NT       if ($data =~ /OS_CYGWIN_NT/);
81 $os = Interix         if ($data =~ /OS_INTERIX/);
82 $os = Android         if ($data =~ /OS_ANDROID/);
83 $os = Haiku           if ($data =~ /OS_HAIKU/);
84
85 $architecture = x86    if ($data =~ /ARCH_X86/);
86 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
87 $architecture = power  if ($data =~ /ARCH_POWER/);
88 $architecture = mips   if ($data =~ /ARCH_MIPS/);
89 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
90 $architecture = alpha  if ($data =~ /ARCH_ALPHA/);
91 $architecture = sparc  if ($data =~ /ARCH_SPARC/);
92 $architecture = ia64   if ($data =~ /ARCH_IA64/);
93 $architecture = arm    if ($data =~ /ARCH_ARM/);
94 $architecture = arm64  if ($data =~ /ARCH_ARM64/);
95 $architecture = zarch  if ($data =~ /ARCH_ZARCH/);
96 $architecture = riscv64 if ($data =~ /ARCH_RISCV64/);
97
98 $defined = 0;
99
100 if ($os eq "AIX") {
101     $compiler_name .= " -maix32" if ($binary eq "32");
102     $compiler_name .= " -maix64" if ($binary eq "64");
103     $defined = 1;
104 }
105
106 if ($architecture eq "mips") {
107     $compiler_name .= " -mabi=32";
108     $defined = 1;
109 }
110
111 if ($architecture eq "mips64") {
112     $compiler_name .= " -mabi=n32" if ($binary eq "32");
113     $compiler_name .= " -mabi=64" if ($binary eq "64");
114     $defined = 1;
115 }
116
117 if (($architecture eq "arm") || ($architecture eq "arm64")) {
118     $defined = 1;
119 }
120
121 if ($architecture eq "zarch") {
122     $defined = 1;
123     $binary = 64;
124 }
125
126 if ($architecture eq "alpha") {
127     $defined = 1;
128     $binary = 64;
129 }
130
131 if ($architecture eq "ia64") {
132     $defined = 1;
133     $binary = 64;
134 }
135
136 if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
137     $defined = 1;
138     $binary =32;
139 }
140
141 if ($architecture eq "riscv64") {
142     $defined = 1;
143     $binary = 64;
144 }
145
146 if ($compiler eq "PGI") {
147     $compiler_name .= " -tp p7"    if ($binary eq "32");
148     $compiler_name .= " -tp p7-64" if ($binary eq "64");
149     $openmp = "-mp";
150     $defined = 1;
151 }
152
153 if ($compiler eq "IBM") {
154     $compiler_name .= " -q32"  if ($binary eq "32");
155     $compiler_name .= " -q64"  if ($binary eq "64");
156     $openmp = "-qsmp=omp";
157     $defined = 1;
158 }
159
160 if ($compiler eq "INTEL") {
161     $openmp = "-openmp";
162 }
163
164 if ($compiler eq "PATHSCALE") {
165     $openmp = "-mp";
166 }
167
168 if ($compiler eq "OPEN64") {
169     $openmp = "-mp";
170 }
171
172 if ($compiler eq "CLANG") {
173     $openmp = "-fopenmp";
174 }
175
176 if ($compiler eq "GCC" || $compiler eq "LSB") {
177     $openmp = "-fopenmp";
178 }
179
180 if ($defined == 0) {
181     $compiler_name .= " -m32" if ($binary eq "32");
182     $compiler_name .= " -m64" if ($binary eq "64");
183 }
184
185 # Do again
186
187 $data = `$compiler_name $flags -E ctest.c`;
188
189 if ($?) {
190     printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
191     die 1;
192 }
193
194 $have_msa = 0;
195 if (($architecture eq "mips") || ($architecture eq "mips64")) {
196     eval "use File::Temp qw(tempfile)";
197     if ($@){ 
198         warn "could not load PERL module File::Temp, so could not check MSA capatibility";
199     } else {
200         $tmpf = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
201         $code = '"addvi.b $w0, $w1, 1"';
202         $msa_flags = "-mmsa -mfp64 -mload-store-pairs";
203         print $tmpf "#include <msa.h>\n\n";
204         print $tmpf "void main(void){ __asm__ volatile($code); }\n";
205
206         $args = "$msa_flags -o $tmpf.o $tmpf";
207         my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
208         system(@cmd) == 0;
209         if ($? != 0) {
210             $have_msa = 0;
211         } else {
212             $have_msa = 1;
213         }
214         unlink("$tmpf.o");
215     }
216 }
217
218 $architecture = x86    if ($data =~ /ARCH_X86/);
219 $architecture = x86_64 if ($data =~ /ARCH_X86_64/);
220 $architecture = power  if ($data =~ /ARCH_POWER/);
221 $architecture = mips   if ($data =~ /ARCH_MIPS/);
222 $architecture = mips64 if ($data =~ /ARCH_MIPS64/);
223 $architecture = alpha  if ($data =~ /ARCH_ALPHA/);
224 $architecture = sparc  if ($data =~ /ARCH_SPARC/);
225 $architecture = ia64   if ($data =~ /ARCH_IA64/);
226 $architecture = arm    if ($data =~ /ARCH_ARM/);
227 $architecture = arm64  if ($data =~ /ARCH_ARM64/);
228 $architecture = zarch  if ($data =~ /ARCH_ZARCH/);
229
230 $binformat    = bin32;
231 $binformat    = bin64  if ($data =~ /BINARY_64/);
232
233 $no_avx512= 0;
234 if (($architecture eq "x86") || ($architecture eq "x86_64")) {
235     eval "use File::Temp qw(tempfile)";
236     if ($@){ 
237         warn "could not load PERL module File::Temp, so could not check compiler compatibility with AVX512";
238         $no_avx512 = 0;
239     } else {
240 #       $tmpf = new File::Temp( UNLINK => 1 );
241         ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
242         $code = '"vbroadcastss -4 * 4(%rsi), %zmm2"';
243         print $tmpf "#include <immintrin.h>\n\nint main(void){ __asm__ volatile($code); }\n";
244         $args = " -march=skylake-avx512 -c -o $tmpf.o $tmpf";
245         if ($compiler eq "PGI") {
246             $args = " -tp skylake -c -o $tmpf.o $tmpf";
247         }
248         my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
249         system(@cmd) == 0;
250         if ($? != 0) {
251             $no_avx512 = 1;
252         } else {
253             $no_avx512 = 0;
254         }
255         unlink("$tmpf.o");
256     }
257 }
258
259 $c11_atomics = 0;
260 if ($data =~ /HAVE_C11/) {
261     eval "use File::Temp qw(tempfile)";
262     if ($@){ 
263        warn "could not load PERL module File::Temp, so could not check compiler compatibility with C11";
264        $c11_atomics = 0;
265     } else {
266        ($fh,$tmpf) = tempfile( SUFFIX => '.c' , UNLINK => 1 );
267        print $tmpf "#include <stdatomic.h>\nint main(void){}\n";
268        $args = " -c -o $tmpf.o $tmpf";
269        my @cmd = ("$compiler_name $flags $args >/dev/null 2>/dev/null");
270        system(@cmd) == 0;
271        if ($? != 0) {
272            $c11_atomics = 0;
273        } else {
274            $c11_atomics = 1;
275        }
276        unlink("$tmpf.o");
277     }
278 }
279
280 if ($compiler eq "GCC" &&( ($architecture eq "x86") || ($architecture eq "x86_64"))) {
281         $no_avx2 = 0;
282         $oldgcc = 0;
283         $data = `$compiler_name -dumpversion`;
284         if ($data <= 4.6) {
285                 $no_avx2 = 1;
286                 $oldgcc = 1;
287         }
288 }
289
290 $data = `$compiler_name $flags -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
291
292 $data =~ /globl\s([_\.]*)(.*)/;
293
294 $need_fu      = $1;
295
296 $cross = 0;
297
298 if ($architecture ne $hostarch) {
299     $cross = 1;
300     $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
301     $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
302 }
303
304 $cross = 1 if ($os ne $hostos);
305
306 $openmp = "" if $ENV{USE_OPENMP} != 1;
307
308 $linker_L = "";
309 $linker_l = "";
310 $linker_a = "";
311
312 {
313     $link = `$compiler_name $flags -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $flags $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`;
314
315     $link =~ s/\-Y\sP\,/\-Y/g;
316
317     @flags = split(/[\s\,\n]/, $link);
318     # remove leading and trailing quotes from each flag.
319     @flags = map {s/^['"]|['"]$//g; $_} @flags;
320
321     foreach $flags (@flags) {
322         if (
323             ($flags =~ /^\-L/)
324             && ($flags !~ /^-LIST:/)
325             && ($flags !~ /^-LANG:/)
326             ) {
327             $linker_L .= $flags . " "
328             }
329
330         if ($flags =~ /^\-Y/) {
331             $linker_L .= "-Wl,". $flags . " "
332             }
333
334         if ($flags =~ /^\--exclude-libs/) {
335             $linker_L .= "-Wl,". $flags . " ";
336             $flags="";
337            }
338
339         if (
340             ($flags =~ /^\-l/)
341             && ($flags !~ /gfortranbegin/)
342             && ($flags !~ /frtbegin/)
343             && ($flags !~ /pathfstart/)
344             && ($flags !~ /numa/)
345             && ($flags !~ /crt[0-9]/)
346             && ($flags !~ /gcc/)
347             && ($flags !~ /user32/)
348             && ($flags !~ /kernel32/)
349             && ($flags !~ /advapi32/)
350             && ($flags !~ /shell32/)
351             && ($flags !~ /omp/)
352             && ($flags !~ /[0-9]+/)
353             ) {
354             $linker_l .= $flags . " "
355         }
356
357         $linker_a .= $flags . " " if $flags =~ /\.a$/;
358     }
359
360 }
361
362 open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
363 open(CONFFILE, "> $config"  ) || die "Can't create $config";
364
365 # print $data, "\n";
366
367 print MAKEFILE "OSNAME=$os\n";
368 print MAKEFILE "ARCH=$architecture\n";
369 print MAKEFILE "C_COMPILER=$compiler\n";
370 print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
371 print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
372 print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
373 print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
374 print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
375 print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross != 0 && $cross_suffix ne "";
376 print MAKEFILE "CROSS=1\n" if $cross != 0;
377 print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
378 print MAKEFILE "HAVE_MSA=1\n" if $have_msa eq 1;
379 print MAKEFILE "MSA_FLAGS=$msa_flags\n" if $have_msa eq 1;
380 print MAKEFILE "NO_AVX512=1\n" if $no_avx512 eq 1;
381 print MAKEFILE "NO_AVX2=1\n" if $no_avx2 eq 1;
382 print MAKEFILE "OLDGCC=1\n" if $oldgcc eq 1;
383
384 $os           =~ tr/[a-z]/[A-Z]/;
385 $architecture =~ tr/[a-z]/[A-Z]/;
386 $compiler     =~ tr/[a-z]/[A-Z]/;
387
388 print CONFFILE "#define OS_$os\t1\n";
389 print CONFFILE "#define ARCH_$architecture\t1\n";
390 print CONFFILE "#define C_$compiler\t1\n";
391 print CONFFILE "#define __32BIT__\t1\n"  if $binformat eq bin32;
392 print CONFFILE "#define __64BIT__\t1\n"  if $binformat eq bin64;
393 print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
394 print CONFFILE "#define HAVE_MSA\t1\n"  if $have_msa eq 1;
395 print CONFFILE "#define HAVE_C11\t1\n" if $c11_atomics eq 1;
396
397
398 if ($os eq "LINUX") {
399
400 #    @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
401
402 #    if ($pthread[2] ne "") {
403 #       print CONFFILE "#define PTHREAD_CREATE_FUNC     $pthread[2]\n";
404 #    } else {
405         print CONFFILE "#define PTHREAD_CREATE_FUNC     pthread_create\n";
406 #    }
407 } else {
408     print CONFFILE "#define PTHREAD_CREATE_FUNC pthread_create\n";
409 }
410
411 close(MAKEFILE);
412 close(CONFFILE);