Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
#! /usr/bin/perl -w

# Copyright 2000-2002 Free Software Foundation, Inc.
#
#  This file is part of the GNU MP Library.
#
#  The GNU MP Library is free software; you can redistribute it and/or modify
#  it under the terms of either:
#
#    * the GNU Lesser General Public License as published by the Free
#      Software Foundation; either version 3 of the License, or (at your
#      option) any later version.
#
#  or
#
#    * the GNU General Public License as published by the Free Software
#      Foundation; either version 2 of the License, or (at your option) any
#      later version.
#
#  or both in parallel, as here.
#
#  The GNU MP Library is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#  for more details.
#
#  You should have received copies of the GNU General Public License and the
#  GNU Lesser General Public License along with the GNU MP Library.  If not,
#  see https://www.gnu.org/licenses/.


# Usage:  cd $builddir/tune
#	  perl $srcdir/tune/many.pl [-t] <files/dirs>...
#
# Output: speed-many.c
#         try-many.c
#         Makefile.many
#
# Make alternate versions of various mpn routines available for measuring
# and testing.
#
# The $srcdir and $builddir in the invocation above just means the script
# lives in the tune source directory, but should be run in the tune build
# directory.  When not using a separate object directory this just becomes
#
#	cd tune
#	perl many.pl [-t] <files/dirs>...
#
#
# SINGLE FILES
#
# Suppose $HOME/newcode/mul_1_experiment.asm is a new implementation of
# mpn_mul_1, then
#
#	cd $builddir/tune
#	perl $srcdir/tune/many.pl $HOME/newcode/mul_1_experiment.asm
#
# will produce rules and renaming so that a speed program incorporating it
# can be built,
#
#	make -f Makefile.many speed-many
#
# then for example it can be compared to the standard mul_1,
#
#	./speed-many -s 1-30 mpn_mul_1 mpn_mul_1_experiment
#
# An expanded try program can be used to check correctness,
#
#	make -f Makefile.many try-many
#
# and run
#
#	./try-many mpn_mul_1_experiment
#
# Files can be ".c", ".S" or ".asm".  ".s" files can't be used because they
# don't get any preprocessing so there's no way to do renaming of their
# functions.
#
#
# WHOLE DIRECTORIES
#
# If a directory is given, then all files in it will be made available.
# For example,
#
#	cd $builddir/tune
#	perl $srcdir/tune/many.pl $HOME/newcode
#
# Each file should have a suffix, like "_experiment" above.
#
#
# MPN DIRECTORIES
#
# mpn directories from the GMP source tree can be included, and this is a
# convenient way to compare multiple implementations suiting different chips
# in a CPU family.  For example the following would make all x86 routines
# available,
#
#	cd $builddir/tune
#	perl $srcdir/tune/many.pl `find $srcdir/mpn/x86 -type d`
#
# On a new x86 chip a comparison could then be made to see how existing code
# runs.  For example,
#
#	make -f Makefile.many speed-many
#	./speed-many -s 1-30 -c \
#		mpn_add_n_x86 mpn_add_n_pentium mpn_add_n_k6 mpn_add_n_k7
#
# Files in "mpn" subdirectories don't need the "_experiment" style suffix
# described above, instead a suffix is constructed from the subdirectory.
# For example "mpn/x86/k7/mmx/mod_1.asm" will generate a function
# mpn_mod_1_k7_mmx.  The rule is to take the last directory name after the
# "mpn", or the last two if there's three or more.  (Check the generated
# speed-many.c if in doubt.)
#
#
# GENERIC C
#
# The mpn/generic directory can be included too, just like any processor
# specific directory.  This is a good way to compare assembler and generic C
# implementations.  For example,
#
#	cd $builddir/tune
#	perl $srcdir/tune/many.pl $srcdir/mpn/generic
#
# or if just a few routines are of interest, then for example
#
#	cd $builddir/tune
#	perl $srcdir/tune/many.pl \
#		$srcdir/mpn/generic/lshift.c \
#		$srcdir/mpn/generic/mod_1.c \
#		$srcdir/mpn/generic/aorsmul_1.c
#
# giving mpn_lshift_generic etc.
#
#
# TESTS/DEVEL PROGRAMS
#
# Makefile.many also has rules to build the tests/devel programs with suitable
# renaming, and with some parameters for correctness or speed.  This is less
# convenient than the speed and try programs, but provides an independent
# check.  For example,
#
#	make -f Makefile.many tests_mul_1_experimental
#	./tests_mul_1_experimental
#
# and for speed
#
#	make -f Makefile.many tests_mul_1_experimental_sp
#	./tests_mul_1_experimental_sp
#
# Not all the programs support speed measuring, in which case only the
# correctness test will be useful.
#
# The parameters for repetitions and host clock speed are -D defines.  Some
# defaults are provided at the end of Makefile.many, but probably these will
# want to be overridden.  For example,
#
#	rm tests_mul_1_experimental.o
#	make -f Makefile.many \
#	   CFLAGS_TESTS="-DSIZE=50 -DTIMES=1000 -DRANDOM -DCLOCK=175000000" \
#	   tests_mul_1_experimental
#	./tests_mul_1_experimental
#
#
# OTHER NOTES
#
# The mappings of file names to functions, and the macros to then use for
# speed measuring etc are driven by @table below.  The scheme isn't
# completely general, it's only got as many variations as have been needed
# so far.
#
# Some functions are only made available in speed-many, or others only in
# try-many.  An @table entry speed=>none means no speed measuring is
# available, or try=>none no try program testing.  These can be removed
# if/when the respective programs get the necessary support.
#
# If a file has "1c" or "nc" carry-in entrypoints, they're renamed and made
# available too.  These are recognised from PROLOGUE or MULFUNC_PROLOGUE in
# .S and .asm files, or from a line starting with "mpn_foo_1c" in a .c file
# (possibly via a #define), and on that basis are entirely optional.  This
# entrypoint matching is done for the standard entrypoints too, but it would
# be very unusual to have for instance a mul_1c without a mul_1.
#
# Some mpz files are recognized.  For example an experimental copy of
# mpz/powm.c could be included as powm_new.c and would be called
# mpz_powm_new.  So far only speed measuring is available for these.
#
# For the ".S" and ".asm" files, both PIC and non-PIC objects are built.
# The PIC functions have a "_pic" suffix, for example "mpn_mod_1_k7_mmx_pic".
# This can be ignored for routines that don't differ for PIC, or for CPUs
# where everything is PIC anyway.
#
# K&R compilers are supported via the same ansi2knr mechanism used by
# automake, though it's hard to believe anyone will have much interest in
# measuring a compiler so old that it doesn't even have an ANSI mode.
#
# The "-t" option can be used to print a trace of the files found and what's
# done with them.  A great deal of obscure output is produced, but it can
# indicate where or why some files aren't being recognised etc.  For
# example,
#
#	cd $builddir/tune
#	perl $srcdir/tune/many.pl -t $HOME/newcode/add_n_weird.asm
#
# In general, when including new code, all that's really necessary is that
# it will compile or assemble under the current configuration.  It's fine if
# some code doesn't actually run due to bugs, or to needing a newer CPU or
# whatever, simply don't ask for the offending routines when invoking
# speed-many or try-many, or don't try to run them on sizes they don't yet
# support, or whatever.
#
#
# CPU SPECIFICS
#
# x86 - All the x86 code will assemble on any system, but code for newer
#       chips might not run on older chips.  Expect SIGILLs from new
#       instructions on old chips.
#
#       A few "new" instructions, like cmov for instance, are done as macros
#       and will generate some equivalent plain i386 code when HAVE_HOST_CPU
#       in config.m4 indicates an old CPU.  It won't run fast, but it does
#       make it possible to test correctness.
#
#
# INTERNALS
#
# The nonsense involving $ENV is some hooks used during development to add
# additional functions temporarily.
#
#
# FUTURE
#
# Maybe the C files should be compiled pic and non-pic too.  Wait until
# there's a difference that might be of interest.
#
# Warn if a file provides no functions.
#
# Allow mpz and mpn files of the same name.  Currently the mpn fib2_ui
# matching hides the mpz version of that.  Will need to check the file
# contents to see which it is.  Would be worth allowing an "mpz_" or "mpn_"
# prefix on the filenames to have working versions of both in one directory.
#
#
# LIMITATIONS
#
# Some of the command lines can become very long when a lot of files are
# included.  If this is a problem on a given system the only suggestion is
# to run many.pl for just those that are actually wanted at a particular
# time.
#
# DOS 8.3 or SysV 14 char filesystems won't work, since the long filenames
# generated will almost certainly fail to be unique.


use strict;
use File::Basename;
use Getopt::Std;

my %opt;
getopts('t', \%opt);

my @DIRECTORIES = @ARGV;
if (defined $ENV{directories}) { push @DIRECTORIES, @{$ENV{directories}} }


# regexp - matched against the start of the filename.  If a grouping "(...)"
#          is present then only the first such part is used.
#
# mulfunc - filenames to be generated from a multi-function file.
#
# funs - functions provided by the file, defaulting to the filename with mpn
#          (or mpX).
#
# mpX - prefix like "mpz", defaulting to "mpn".
#
# ret - return value type.
#
# args, args_<fun> - arguments for the given function.  If an args_<fun> is
#          set then it's used, otherwise plain args is used.  "mp_limb_t
#          carry" is appended for carry-in variants.
#
# try - try.c TYPE_ to use, defaulting to TYPE_fun with the function name
#          in upper case.  "C" is appended for carry-in variants.  Can be
#          'none' for no try program entry.
#
# speed - SPEED_ROUTINE_ to use, handled like "try".
#
# speed_flags - SPEED_ROUTINE_ to use, handled like "try".


my @table =
    (
     {
       'regexp'=> 'add_n|sub_n|addlsh1_n|sublsh1_n|rsh1add_n|rsh1sub_n',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_srcptr yp, mp_size_t size',
       'speed' => 'SPEED_ROUTINE_MPN_BINARY_N',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
     },
     {
       'regexp'=> 'aors_n',
       'mulfunc'=> ['add_n','sub_n'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_srcptr yp, mp_size_t size',
       'speed' => 'SPEED_ROUTINE_MPN_BINARY_N',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
     },

     {
       'regexp'=> 'addmul_1|submul_1',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_limb_t mult',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_1',
       'speed_flags'=> 'FLAG_R',
     },
     {
       'regexp'=> 'aorsmul_1',
       'mulfunc'=> ['addmul_1','submul_1'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_limb_t mult',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_1',
       'speed_flags'=> 'FLAG_R',
     },

     {
       'regexp'=> 'addmul_2|submul_2',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr yp',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_2',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try-minsize' => 2,
     },
     {
       'regexp'=> 'addmul_3|submul_3',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr yp',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_3',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try-minsize' => 3,
     },
     {
       'regexp'=> 'addmul_4|submul_4',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr yp',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_4',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try-minsize' => 4,
     },
     {
       'regexp'=> 'addmul_5|submul_5',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr yp',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_5',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try-minsize' => 5,
     },
     {
       'regexp'=> 'addmul_6|submul_6',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr yp',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_6',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try-minsize' => 6,
     },
     {
       'regexp'=> 'addmul_7|submul_7',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr yp',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_7',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try-minsize' => 7,
     },
     {
       'regexp'=> 'addmul_8|submul_8',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr yp',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_8',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try-minsize' => 8,
     },

     {
       'regexp'=> 'add_n_sub_n',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr sum, mp_ptr diff, mp_srcptr xp, mp_srcptr yp, mp_size_t size',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
     },

     {
       'regexp'=> 'com|copyi|copyd',
       'ret'   => 'void',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size',
       'speed' => 'SPEED_ROUTINE_MPN_COPY',
     },

     {
       'regexp'=> 'dive_1',
       'funs'  => ['divexact_1'],
       'ret'   => 'void',
       'args'  => 'mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t divisor',
       'speed_flags'=> 'FLAG_R',
     },
     {
       'regexp'=> 'diveby3',
       'funs'  => ['divexact_by3c'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr dst, mp_srcptr src, mp_size_t size',
       'carrys'=> [''],
       'speed' => 'SPEED_ROUTINE_MPN_COPY',
     },

     # mpn_preinv_divrem_1 is an optional extra entrypoint
     {
       'regexp'=> 'divrem_1',
       'funs'  => ['divrem_1', 'preinv_divrem_1'],
       'ret'   => 'mp_limb_t',
       'args_divrem_1' => 'mp_ptr rp, mp_size_t xsize, mp_srcptr sp, mp_size_t size, mp_limb_t divisor',
       'args_preinv_divrem_1' => 'mp_ptr rp, mp_size_t xsize, mp_srcptr sp, mp_size_t size, mp_limb_t divisor, mp_limb_t inverse, unsigned shift',
       'speed_flags'=> 'FLAG_R',
       'speed_suffixes' => ['f'],
     },
     {
       'regexp'=> 'pre_divrem_1',
       'funs'  => ['preinv_divrem_1'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr qp, mp_size_t qxn, mp_srcptr ap, mp_size_t asize, mp_limb_t divisor, mp_limb_t inverse, int shift',
       'speed_flags' => 'FLAG_R',
     },

     {
       'regexp'=> 'divrem_2',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr qp, mp_size_t qxn, mp_srcptr np, mp_size_t nsize, mp_srcptr dp',
       'try'   => 'none',
     },

     {
       'regexp'=> 'sb_divrem_mn',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr qp, mp_ptr np, mp_size_t nsize, mp_srcptr dp, mp_size_t dsize',
       'speed' => 'SPEED_ROUTINE_MPN_DC_DIVREM_SB',
       'try-minsize' => 3,
     },
     {
       'regexp'=> 'tdiv_qr',
       'ret'   => 'void',
       'args'  => 'mp_ptr qp, mp_size_t qxn, mp_ptr np, mp_size_t nsize, mp_srcptr dp, mp_size_t dsize',
       'speed' => 'none',
     },

     {
       'regexp'=> 'get_str',
       'ret'   => 'size_t',
       'args'  => 'unsigned char *str, int base, mp_ptr mptr, mp_size_t msize',
       'speed_flags' => 'FLAG_R_OPTIONAL',
       'try'   => 'none',
     },
     {
       'regexp'=> 'set_str',
       'ret'   => 'mp_size_t',
       'args'  => 'mp_ptr xp, const unsigned char *str, size_t str_len, int base',
       'speed_flags' => 'FLAG_R_OPTIONAL',
       'try'   => 'none',
     },

     {
       'regexp'=> 'fac_ui',
       'mpX'   => 'mpz',
       'ret'   => 'void',
       'args'  => 'mpz_ptr r, unsigned long n',
       'speed_flags' => 'FLAG_NODATA',
       'try'   => 'none',
     },

     {
       'regexp'=> 'fib2_ui',
       'ret'   => 'void',
       'args'  => 'mp_ptr fp, mp_ptr f1p, unsigned long n',
       'rename'=> ['__gmp_fib_table'],
       'speed_flags' => 'FLAG_NODATA',
       'try'   => 'none',
     },
     {
       'regexp'=> 'fib_ui',
       'mpX'   => 'mpz',
       'ret'   => 'void',
       'args'  => 'mpz_ptr fn, unsigned long n',
       'speed_flags' => 'FLAG_NODATA',
       'try'   => 'none',
     },
     {
       'regexp'=> 'fib2_ui',
       'mpX'   => 'mpz',
       'ret'   => 'void',
       'args'  => 'mpz_ptr fn, mpz_ptr fnsub1, unsigned long n',
       'speed_flags' => 'FLAG_NODATA',
       'try'   => 'none',
     },

     {
       'regexp'=> 'lucnum_ui',
       'mpX'   => 'mpz',
       'ret'   => 'void',
       'args'  => 'mpz_ptr ln, unsigned long n',
       'speed_flags' => 'FLAG_NODATA',
       'try'   => 'none',
     },
     {
       'regexp'=> 'lucnum2_ui',
       'mpX'   => 'mpz',
       'ret'   => 'void',
       'args'  => 'mpz_ptr ln, mpz_ptr lnsub1, unsigned long n',
       'speed_flags' => 'FLAG_NODATA',
       'try'   => 'none',
     },

     {
       'regexp'=> 'gcd_1',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr xp, mp_size_t xsize, mp_limb_t y',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'speed_suffixes' => ['N'],
     },
     {
       'regexp'=> '(gcd)(?!(_1|ext|_finda))',
       'ret'   => 'mp_size_t',
       'args'  => 'mp_ptr gp, mp_ptr up, mp_size_t usize, mp_ptr vp, mp_size_t vsize',
     },
     {
       'regexp'=> 'gcd_finda',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_srcptr cp',
     },


     {
       'regexp'=> 'jacobi',
       'funs'  => ['jacobi', 'legendre', 'kronecker'],
       'mpX'   => 'mpz',
       'ret'   => 'int',
       'args'  => 'mpz_srcptr a, mpz_srcptr b',
       'try-legendre' => 'TYPE_MPZ_JACOBI',
     },
     {
       'regexp'=> 'jacbase',
       'funs'  => ['jacobi_base'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_limb_t a, mp_limb_t b, int bit1',
       'speed' => 'SPEED_ROUTINE_MPN_JACBASE',
       'try'   => 'none',
     },

     {
       'regexp'=> 'logops_n',
       'mulfunc'=> ['and_n','andn_n','nand_n','ior_n','iorn_n','nior_n','xor_n','xnor_n'],
       'ret'   => 'void',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_srcptr yp, mp_size_t size',
       'speed' => 'SPEED_ROUTINE_MPN_BINARY_N',
     },

     {
       'regexp'=> '[lr]shift',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, unsigned shift',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_1',
       'speed_flags'=> 'FLAG_R',
     },

     # mpn_preinv_mod_1 is an optional extra entrypoint
     {
       'regexp'=> '(mod_1)(?!_rs)',
       'funs'  => ['mod_1','preinv_mod_1'],
       'ret'   => 'mp_limb_t',
       'args_mod_1'       => 'mp_srcptr xp, mp_size_t size, mp_limb_t divisor',
       'args_preinv_mod_1'=> 'mp_srcptr xp, mp_size_t size, mp_limb_t divisor, mp_limb_t inverse',
       'speed_flags'=> 'FLAG_R',
     },
     {
       'regexp'=> 'pre_mod_1',
       'funs'  => ['preinv_mod_1'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_srcptr xp, mp_size_t size, mp_limb_t divisor, mp_limb_t inverse',
       'speed_flags'=> 'FLAG_R',
     },
     {
       'regexp'=> 'mod_34lsub1',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_srcptr src, mp_size_t len',
     },
     {
       'regexp'=> 'invert_limb',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_limb_t divisor',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try'   => 'none',
     },

     {
       # not for use with hppa reversed argument versions of mpn_umul_ppmm
       'regexp'=> 'udiv',
       'funs'  => ['udiv_qrnnd','udiv_qrnnd_r'],
       'ret'   => 'mp_limb_t',
       'args_udiv_qrnnd'   => 'mp_limb_t *, mp_limb_t, mp_limb_t, mp_limb_t',
       'args_udiv_qrnnd_r' => 'mp_limb_t, mp_limb_t, mp_limb_t, mp_limb_t *',
       'speed' => 'none',
       'try-minsize' => 2,
     },

     {
       'regexp'=> 'mode1o',
       'funs'  => ['modexact_1_odd'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_srcptr src, mp_size_t size, mp_limb_t divisor',
       'speed_flags'=> 'FLAG_R',
     },
     {
       'regexp'=> 'modlinv',
       'funs'  => ['modlimb_invert'],
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_limb_t v',
       'carrys'=> [''],
       'try'   => 'none',
     },

     {
       'regexp'=> 'mul_1',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_limb_t mult',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_1',
       'speed_flags'=> 'FLAG_R',
     },
     {
       'regexp'=> 'mul_2',
       'ret'   => 'mp_limb_t',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size, mp_srcptr mult',
       'speed' => 'SPEED_ROUTINE_MPN_UNARY_2',
       'speed_flags'=> 'FLAG_R',
     },

     {
       'regexp'=> 'mul_basecase',
       'ret'   => 'void',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t xsize, mp_srcptr yp, mp_size_t ysize',
       'speed_flags' => 'FLAG_R_OPTIONAL | FLAG_RSIZE',
     },
     {
       'regexp'=> '(mul_n)[_.]',
       'ret'   => 'void',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_srcptr yp, mp_size_t size',
       'rename'=> ['kara_mul_n','kara_sqr_n','toom3_mul_n','toom3_sqr_n'],
     },
     {
       'regexp'=> 'umul',
       'funs'  => ['umul_ppmm','umul_ppmm_r'],
       'ret'   => 'mp_limb_t',
       'args_umul_ppmm'   => 'mp_limb_t *lowptr, mp_limb_t m1, mp_limb_t m2',
       'args_umul_ppmm_r' => 'mp_limb_t m1, mp_limb_t m2, mp_limb_t *lowptr',
       'speed' => 'none',
       'try-minsize' => 3,
     },


     {
       'regexp'=> 'popham',
       'mulfunc'=> ['popcount','hamdist'],
       'ret'   => 'unsigned long',
       'args_popcount'=> 'mp_srcptr xp, mp_size_t size',
       'args_hamdist' => 'mp_srcptr xp, mp_srcptr yp, mp_size_t size',
     },
     {
       'regexp'=> 'popcount',
       'ret'   => 'unsigned long',
       'args'  => 'mp_srcptr xp, mp_size_t size',
     },
     {
       'regexp'=> 'hamdist',
       'ret'   => 'unsigned long',
       'args'  => 'mp_srcptr xp, mp_srcptr yp, mp_size_t size',
       # extra renaming to support sharing a data table with mpn_popcount
       'rename'=> ['popcount'],
     },

     {
       'regexp'=> 'sqr_basecase',
       'ret'   => 'void',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size',
       'speed' => 'SPEED_ROUTINE_MPN_SQR',
       'try'   => 'TYPE_SQR',
     },
     {
       'regexp'=> 'sqr_diagonal',
       'ret'   => 'void',
       'args'  => 'mp_ptr wp, mp_srcptr xp, mp_size_t size',
       'try'   => 'none',
     },

     {
       'regexp'=> 'sqrtrem',
       'ret'   => 'mp_size_t',
       'args'  => 'mp_ptr root, mp_ptr rem, mp_srcptr src, mp_size_t size',
       'try'   => 'none',
     },

     {
       'regexp'=> 'cntlz',
       'funs'  => ['count_leading_zeros'],
       'ret'   => 'unsigned',
       'args'  => 'mp_limb_t',
       'macro-before' => "#undef COUNT_LEADING_ZEROS_0",
       'macro-speed'  =>
'#ifdef COUNT_LEADING_ZEROS_0
#define COUNT_LEADING_ZEROS_0_ALLOWED   1
#else
#define COUNT_LEADING_ZEROS_0_ALLOWED   0
#endif
  SPEED_ROUTINE_COUNT_ZEROS_A (1, COUNT_LEADING_ZEROS_0_ALLOWED);
  $fun (c, n);
  SPEED_ROUTINE_COUNT_ZEROS_B ()',
       'speed_flags'=> 'FLAG_R_OPTIONAL',
       'try'   => 'none',
     },
     {
       'regexp'=> 'cnttz',
       'funs'  => ['count_trailing_zeros'],
       'ret'   => 'unsigned',
       'args'  => 'mp_limb_t',
       'macro-speed' => '
  SPEED_ROUTINE_COUNT_ZEROS_A (0, 0);
  $fun (c, n);
  SPEED_ROUTINE_COUNT_ZEROS_B ()',
       'speed_flags' => 'FLAG_R_OPTIONAL',
       'try'   => 'none',
     },

     {
       'regexp'=> 'zero',
       'ret'   => 'void',
       'args'  => 'mp_ptr ptr, mp_size_t size',
     },

     {
       'regexp'=> '(powm)(?!_ui)',
       'mpX'   => 'mpz',
       'ret'   => 'void',
       'args'  => 'mpz_ptr r, mpz_srcptr b, mpz_srcptr e, mpz_srcptr m',
       'try'   => 'none',
     },
     {
       'regexp'=> 'powm_ui',
       'mpX'   => 'mpz',
       'ret'   => 'void',
       'args'  => 'mpz_ptr r, mpz_srcptr b, unsigned long e, mpz_srcptr m',
       'try'   => 'none',
     },

     # special for use during development
     {
       'regexp'=> 'back',
       'funs'  => ['back_to_back'],
       'ret'   => 'void',
       'args'  => 'void',
       'pic'   => 'no',
       'try'   => 'none',
       'speed_flags'=> 'FLAG_NODATA',
     },
     );

if (defined $ENV{table2}) {
  my @newtable = @{$ENV{table2}};
  push @newtable, @table;
  @table = @newtable;
}


my %pictable =
    (
     'yes' => {
       'suffix' =>  '_pic',
       'asmflags'=> '$(ASMFLAGS_PIC)',
       'cflags' =>  '$(CFLAGS_PIC)',
     },
     'no' => {
       'suffix' =>  '',
       'asmflags'=> '',
       'cflags' =>  '',
     },
     );


my $builddir = $ENV{builddir};
$builddir = "." if (! defined $builddir);

my $top_builddir = "${builddir}/..";


open(MAKEFILE, "<${builddir}/Makefile")
  or die "Cannot open ${builddir}/Makefile: $!\n"
       . "Is this a tune build directory?";
my ($srcdir, $top_srcdir);
while (<MAKEFILE>) {
  if (/^srcdir = (.*)/) {     $srcdir = $1;     }
  if (/^top_srcdir = (.*)/) { $top_srcdir = $1; }
}
die "Cannot find \$srcdir in Makefile\n" if (! defined $srcdir);
die "Cannot find \$top_srcdir in Makefile\n" if (! defined $top_srcdir);
print "srcdir $srcdir\n" if $opt{'t'};
print "top_srcdir $top_srcdir\n" if $opt{'t'};
close(MAKEFILE);


open(SPEED, ">speed-many.c") or die;
print SPEED
"/* speed-many.c generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST */

";
my $SPEED_EXTRA_ROUTINES = "#define SPEED_EXTRA_ROUTINES \\\n";
my $SPEED_EXTRA_PROTOS = "#define SPEED_EXTRA_PROTOS \\\n";
my $SPEED_CODE = "";

open(TRY, ">try-many.c") or die;
print TRY
    "/* try-many.c generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST */\n" .
    "\n";
my $TRY_EXTRA_ROUTINES = "#define EXTRA_ROUTINES \\\n";
my $TRY_EXTRA_PROTOS = "#define EXTRA_PROTOS \\\n";

open(FD,"<${top_builddir}/libtool") or die "Cannot open \"${top_builddir}/libtool\": $!\n";
my $pic_flag;
while (<FD>) {
  if (/^pic_flag="?([^"]*)"?$/) {
    $pic_flag=$1;
    last;
  }
}
close FD;
if (! defined $pic_flag) {
  die "Cannot find pic_flag in ${top_builddir}/libtool";
}

my $CFLAGS_PIC = $pic_flag;

my $ASMFLAGS_PIC = "";
foreach (split /[ \t]/, $pic_flag) {
  if (/^-D/) {
    $ASMFLAGS_PIC .= " " . $_;
  }
}

open(MAKEFILE, ">Makefile.many") or die;
print MAKEFILE
    "# Makefile.many generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST\n" .
    "\n" .
    "all: speed-many try-many\n" .
    "\n" .
    "#--------- begin included copy of basic Makefile ----------\n" .
    "\n";
open(FD,"<${builddir}/Makefile") or die "Cannot open \"${builddir}/Makefile\": $!\n";
print MAKEFILE <FD>;
close FD;
print MAKEFILE
    "\n" .
    "#--------- end included copy of basic Makefile ----------\n" .
    "\n" .
    "CFLAGS_PIC = $CFLAGS_PIC\n" .
    "ASMFLAGS_PIC = $ASMFLAGS_PIC\n" .
    "\n";

my $CLEAN="";
my $MANY_OBJS="";


sub print_ansi2knr {
  my ($base,$file,$includes) = @_;
  if (! defined $file)     { $file = "$base.c"; }
  if (! defined $includes) { $includes = ""; }

  print MAKEFILE <<EOF;
${base}_.c: $file \$(ANSI2KNR)
	\$(CPP) \$(DEFS) \$(INCLUDES) $includes \$(AM_CPPFLAGS) \$(CPPFLAGS) $file | sed 's/^# \([0-9]\)/#line \\1/' | \$(ANSI2KNR) >${base}_.c

EOF
}


# Spawning a glob is a touch slow when there's lots of files.
my @files = ();
foreach my $dir (@DIRECTORIES) {
  print "dir $dir\n" if $opt{'t'};
  if (-f $dir) {
    push @files,$dir;
  } else {
    if (! opendir DD,$dir) {
      print "Cannot open $dir: $!\n";
    } else {
      push @files, map {$_="$dir/$_"} grep /\.(c|asm|S|h)$/, readdir DD;
      closedir DD;
    }
  }
}
@files = sort @files;
print "@files ",join(" ",@files),"\n" if $opt{'t'};

my $count_files = 0;
my $count_functions = 0;
my %seen_obj;
my %seen_file;

foreach my $file_full (@files) {
  if (! -f $file_full) {
    print "Not a file: $file_full\n";
    next;
  }
  if (defined $seen_file{$file_full}) {
    print "Skipping duplicate file: $file_full\n";
    next;
  }
  $seen_file{$file_full} = 1;

  my ($FILE,$path,$lang) = fileparse($file_full,"\.[a-zA-Z]+");
  $path =~ s/\/$//;
  print "file $FILE path $path lang $lang\n" if $opt{'t'};

  my @pic_choices;
  if ($lang eq '.asm')  { @pic_choices=('no','yes'); }
  elsif ($lang eq '.c') { @pic_choices=('no'); }
  elsif ($lang eq '.S') { @pic_choices=('no','yes'); }
  elsif ($lang eq '.h') { @pic_choices=('no'); }
  else { next };

  my ($t, $file_match);
  foreach my $p (@table) {
    # print " ",$p->{'regexp'},"\n" if $opt{'t'};
    if ($FILE =~ "^($p->{'regexp'})") {
      $t = $p;
      $file_match = $1;
      $file_match = $2 if defined $2;
      last;
    }
  }
  next if ! defined $t;
  print "match $t->{'regexp'} $FILE ($file_full)\n" if $opt{'t'};

  if (! open FD,"<$file_full") { print "Can't open $file_full: $!\n"; next }
  my @file_contents = <FD>;
  close FD;

  my $objs;
  if (defined $t->{'mulfunc'}) { $objs = $t->{'mulfunc'}; }
  else                         { $objs = [$file_match]; }
  print "objs @$objs\n" if $opt{'t'};

  my $ret = $t->{'ret'};
  if (! defined $ret && $lang eq '.h') { $ret = ''; }
  if (! defined $ret) { die "$FILE return type not defined\n" };
  print "ret $ret\n" if $opt{'t'};

  my $mpX = $t->{'mpX'};
  if (! defined $mpX) { $mpX = ($lang eq '.h' ? '' : 'mpn'); }
  $mpX = "${mpX}_" if $mpX ne '';
  print "mpX $mpX\n" if $opt{'t'};

  my $carrys;
  if (defined $t->{'carrys'}) { $carrys = $t->{'carrys'}; }
  else                        { $carrys = ['','c'];       }
  print "carrys $carrys @$carrys\n" if $opt{'t'};

  # some restriction functions are implemented, but they're not very useful
  my $restriction='';

  my $suffix;
  if ($FILE =~ ("${file_match}_(.+)")) {
    $suffix = $1;
  } elsif ($path =~ /\/mp[zn]\/(.*)$/) {
    # derive the suffix from the path
    $suffix = $1;
    $suffix =~ s/\//_/g;
    # use last directory name, or if there's 3 or more then the last two
    if ($suffix =~ /([^_]*_)+([^_]+_[^_]+)$/) {
      $suffix = $2;
    } elsif ($suffix =~ /([^_]*_)*([^_]+)$/) {
      $suffix = $2;
    }
  } else {
    die "Can't determine suffix for: $file_full (path $path)\n";
  }
  print "suffix $suffix\n" if $opt{'t'};

  $count_files++;

  foreach my $obj (@{$objs}) {
    print "obj $obj\n" if $opt{'t'};

    my $obj_with_suffix = "${obj}_$suffix";
    if (defined $seen_obj{$obj_with_suffix}) {
      print "Skipping duplicate object: $obj_with_suffix\n";
      print "   first from: $seen_obj{$obj_with_suffix}\n";
      print "   now from:   $file_full\n";
      next;
    }
    $seen_obj{$obj_with_suffix} = $file_full;

    my $funs = $t->{'funs'};
    $funs = [$obj] if ! defined $funs;
    print "funs @$funs\n" if $opt{'t'};

    if (defined $t->{'pic'}) { @pic_choices = ('no'); }

    foreach my $pic (map {$pictable{$_}} @pic_choices) {
      print "pic $pic->{'suffix'}\n" if $opt{'t'};

      my $objbase = "${obj}_$suffix$pic->{'suffix'}";
      print "objbase $objbase\n" if $opt{'t'};

      if ($path !~ "." && -f "${objbase}.c") {
	die "Already have ${objbase}.c";
      }

      my $tmp_file = "tmp-$objbase.c";

      my $renaming;
      foreach my $fun (@{$funs}) {
        if ($mpX eq 'mpn_' && $lang eq '.c') {
          $renaming .= "\t\t-DHAVE_NATIVE_mpn_$fun=1 \\\n";
        }

        # The carry-in variant is with a "c" appended, unless there's a "_1"
        # somewhere, eg. "modexact_1_odd", in which case that becomes "_1c".
	my $fun_carry = $fun;
	if (! ($fun_carry =~ s/_1/_1c/)) { $fun_carry = "${fun}c"; }

	$renaming .=
	    "\t\t-D__g$mpX$fun=$mpX${fun}_$suffix$pic->{'suffix'} \\\n" .
	    "\t\t-D__g$mpX$fun_carry=$mpX${fun_carry}_$suffix$pic->{'suffix'} \\\n";
      }
      foreach my $r (@{$t->{'rename'}}) {
	if ($r =~ /^__gmp/) {
	  $renaming .= "\\\n" .
	      "\t\t-D$r=${r}_$suffix$pic->{'suffix'}";
	} else {
	  $renaming .= "\\\n" .
	      "\t\t-D__g$mpX$r=$mpX${r}_$suffix$pic->{'suffix'}";
	}
      }
      print "renaming $renaming\n" if $opt{'t'};

      print MAKEFILE "\n";
      if ($lang eq '.asm') {
	print MAKEFILE
	    "$objbase.o: $file_full \$(ASM_HEADERS)\n" .
	    "	\$(M4) \$(M4FLAGS) -DOPERATION_$obj $pic->{'asmflags'} \\\n" .
  	    "$renaming" .
	    "		$file_full >tmp-$objbase.s\n" .
            "	\$(CCAS) \$(COMPILE_FLAGS) $pic->{'cflags'} tmp-$objbase.s -o $objbase.o\n" .
            "	\$(RM_TMP) tmp-$objbase.s\n";
	$MANY_OBJS .= " $objbase.o";

      } elsif ($lang eq '.c') {
	print MAKEFILE
	    "$objbase.o: $file_full\n" .
	    "	\$(COMPILE) -DOPERATION_$obj $pic->{'cflags'} \\\n" .
  	    "$renaming" .
	    "		-c $file_full -o $objbase.o\n";
	print_ansi2knr($objbase,
		       $file_full,
		       " -DOPERATION_$obj\\\n$renaming\t\t");
	$MANY_OBJS .= " $objbase\$U.o";

      } elsif ($lang eq '.S') {
	print MAKEFILE
	    "$objbase.o: $file_full\n" .
            "	\$(COMPILE) -g $pic->{'asmflags'} \\\n" .
  	    "$renaming" .
            "	-c $file_full -o $objbase.o\n";
	$MANY_OBJS .= " $objbase.o";

      } elsif ($lang eq '.h') {
	print MAKEFILE
	    "$objbase.o: tmp-$objbase.c $file_full\n" .
	    "	\$(COMPILE) -DOPERATION_$obj $pic->{'cflags'} \\\n" .
  	    "$renaming" .
	    "		-c tmp-$objbase.c -o $objbase.o\n";
	print_ansi2knr($objbase,
		       "tmp-$objbase.c",
		       " -DOPERATION_$obj\\\n$renaming\t\t");
	$MANY_OBJS .= " $objbase\$U.o";

        $CLEAN .= " tmp-$objbase.c";
	open(TMP_C,">tmp-$objbase.c")
	    or die "Can't create tmp-$objbase.c: $!\n";
	print TMP_C
"/* tmp-$objbase.c generated by many.pl - DO NOT EDIT, CHANGES WILL BE LOST */

#include \"gmp.h\"
#include \"gmp-impl.h\"
#include \"longlong.h\"
#include \"speed.h\"

";
      }

      my $tests_program = "$top_srcdir/tests/devel/$obj.c";
      if (-f $tests_program) {
	$tests_program = "\$(top_srcdir)/tests/devel/$obj.c";
	print_ansi2knr("tests_${objbase}",
		       $tests_program,
		       "\\\n$renaming\t\t\$(CFLAGS_TESTS_SP)");
	print_ansi2knr("tests_${objbase}_sp",
		       $tests_program,
		       "\\\n$renaming\t\t\$(CFLAGS_TESTS_SP)");

	print MAKEFILE <<EOF;
tests_$objbase.o: $tests_program
	\$(COMPILE) \$(CFLAGS_TESTS) \\
$renaming		-c $tests_program -o tests_$objbase.o

tests_$objbase: $objbase\$U.o tests_$objbase\$U.o ../libgmp.la
	\$(LINK) tests_$objbase\$U.o $objbase\$U.o ../libgmp.la -o tests_$objbase

tests_${objbase}_sp.o: $tests_program
	\$(COMPILE) \$(CFLAGS_TESTS_SP) \\
$renaming		-c $tests_program -o tests_${objbase}_sp.o

tests_${objbase}_sp: $objbase\$U.o tests_${objbase}_sp\$U.o ../libgmp.la
	\$(LINK) tests_${objbase}_sp\$U.o $objbase\$U.o ../libgmp.la -o tests_${objbase}_sp

EOF
        $CLEAN .= " tests_$objbase tests_${objbase}_sp";
      }

      foreach my $fun (@{$funs}) {
	print "fun $fun\n" if $opt{'t'};

	if ($lang eq '.h') {
          my $macro_before = $t->{'macro_before'};
          $macro_before = "" if ! defined $macro_before;
	  print TMP_C
"$macro_before
#undef $fun
#include \"$file_full\"

";
	}

	my $args = $t->{"args_$fun"};
	if (! defined $args) { $args = $t->{'args'}; }
	if (! defined $args) { die "Need args for $fun\n"; }
	print "args $args\n" if $opt{'t'};

	foreach my $carry (@$carrys) {
	  print "carry $carry\n" if $opt{'t'};

	  my $fun_carry = $fun;
	  if (! ($fun_carry =~ s/_1/_1$carry/)) { $fun_carry = "$fun$carry"; }
          print "fun_carry $fun_carry\n" if $opt{'t'};

	  if ($lang =~ /\.(asm|S)/
	      && ! grep(m"PROLOGUE\((.* )?$mpX$fun_carry[ ,)]",@file_contents)) {
	    print "no PROLOGUE $mpX$fun_carry\n" if $opt{'t'};
	    next;
	  }
	  if ($lang eq '.c'
	      && ! grep(m"^(#define FUNCTION\s+)?$mpX$fun_carry\W", @file_contents)) {
	    print "no mention of $mpX$fun_carry\n" if $opt{'t'};
	    next;
	  }
	  if ($lang eq '.h'
	      && ! grep(m"^#define $fun_carry\W", @file_contents)) {
	    print "no mention of #define $fun_carry\n" if $opt{'t'};
	    next;
	  }

	  $count_functions++;

	  my $carryarg;
	  if (defined $t->{'carryarg'}) { $carryarg = $t->{'carryarg'}; }
	  if ($carry eq '')             { $carryarg = ''; }
	  else                          { $carryarg = ', mp_limb_t carry'; }
	  print "carryarg $carryarg\n" if $opt{'t'};

	  my $funfull="$mpX${fun_carry}_$suffix$pic->{'suffix'}";
	  print "funfull $funfull\n" if $opt{'t'};

	  if ($lang ne '.h') {
	    my $proto = "$t->{'ret'} $funfull _PROTO (($args$carryarg)); \\\n";
	    $SPEED_EXTRA_PROTOS .= $proto;
	    $TRY_EXTRA_PROTOS .= $proto;
	  }

	  my $try_type = $t->{"try-$fun"};
	  $try_type = $t->{'try'} if ! defined $try_type;
	  if (! defined $try_type) {
	    if ($mpX eq 'mpn_') {
	      $try_type = "TYPE_\U$fun_carry";
	    } else {
	      $try_type = "TYPE_\U$mpX\U$fun_carry";
	    }
	  }
	  print "try_type $try_type\n" if $opt{'t'};

	  my $try_minsize = $t->{'try-minsize'};
	  if (defined $try_minsize) {
	    $try_minsize = ", " . $try_minsize;
	  } else {
	    $try_minsize = "";
	  }
	  print "try_minsize $try_minsize\n" if $opt{'t'};

	  if ($try_type ne 'none') {
	    $TRY_EXTRA_ROUTINES .=
		"  { TRY($mpX${fun_carry}_$suffix$pic->{'suffix'}), $try_type$try_minsize }, \\\n";
	  }

	  my $speed_flags = $t->{'speed_flags'};
	  $speed_flags = '0' if ! defined $speed_flags;
	  print "speed_flags $speed_flags\n" if $opt{'t'};

	  my $speed_routine = $t->{'speed'};
	  $speed_routine = "SPEED_ROUTINE_\U$mpX\U$fun"
	      if !defined $speed_routine;
	  if (! ($speed_routine =~ s/_1/_1\U$carry/)) {
	    $speed_routine = "$speed_routine\U$carry";
	  }
	  print "speed_routine $speed_routine\n" if $opt{'t'};

	  my @speed_suffixes = ();
	  push (@speed_suffixes, '') if $speed_routine ne 'none';
	  push (@speed_suffixes, @{$t->{'speed_suffixes'}})
	      if defined $t->{'speed_suffixes'};

          my $macro_speed = $t->{'macro-speed'};
          $macro_speed = "$speed_routine ($fun_carry)" if ! defined $macro_speed;
          $macro_speed =~ s/\$fun/$fun_carry/g;

	  foreach my $S (@speed_suffixes) {
	    my $Sfunfull="$mpX${fun_carry}${S}_$suffix$pic->{'suffix'}";

	    $SPEED_EXTRA_PROTOS .=
	      "double speed_$Sfunfull _PROTO ((struct speed_params *s)); \\\n";
	    $SPEED_EXTRA_ROUTINES .=
	      "  { \"$Sfunfull\", speed_$Sfunfull, $speed_flags }, \\\n";
	    if ($lang eq '.h') {
              print TMP_C
"double
speed_$Sfunfull (struct speed_params *s)
{
$macro_speed
}

";
            } else {
	      $SPEED_CODE .=
	        "double\n" .
	        "speed_$Sfunfull (struct speed_params *s)\n" .
                "{\n" .
                "$restriction" .
	        "  $speed_routine\U$S\E ($funfull)\n" .
                "}\n";
            }
	  }
	}
      }
    }
  }
}


print SPEED $SPEED_EXTRA_PROTOS . "\n";
print SPEED $SPEED_EXTRA_ROUTINES . "\n";
if (defined $ENV{speedinc}) { print SPEED $ENV{speedinc} . "\n"; }
print SPEED
    "#include \"speed.c\"\n" .
    "\n";
print SPEED $SPEED_CODE;

print TRY $TRY_EXTRA_ROUTINES . "\n";
print TRY $TRY_EXTRA_PROTOS . "\n";
my $tryinc = "";
if (defined $ENV{tryinc}) {
  $tryinc = $ENV{tryinc};
  print TRY "#include \"$tryinc\"\n";
}
print "tryinc $tryinc\n" if $opt{'t'};
print TRY
    "#include \"try.c\"\n" .
    "\n";

my $extra_libraries = "";
if (defined $ENV{extra_libraries}) { $extra_libraries = $ENV{extra_libraries};}

my $trydeps = "";
if (defined $ENV{trydeps}) { $trydeps = $ENV{trydeps}; }
$trydeps .= " $tryinc";
print "trydeps $trydeps\n" if $opt{'t'};

print MAKEFILE <<EOF;

MANY_OBJS = $MANY_OBJS
MANY_CLEAN = \$(MANY_OBJS) \\
	speed-many.c speed-many\$U.o speed-many\$(EXEEXT) \\
	try-many.c try-many\$U.o try-many \\
	$CLEAN
MANY_DISTCLEAN = Makefile.many

speed-many: \$(MANY_OBJS) speed-many\$U.o libspeed.la $extra_libraries
	\$(LINK) \$(LDFLAGS) speed-many\$U.o \$(MANY_OBJS) \$(LDADD) \$(LIBS) $extra_libraries

try-many: \$(MANY_OBJS) try-many\$U.o libspeed.la $extra_libraries
	\$(LINK) \$(LDFLAGS) try-many\$U.o \$(MANY_OBJS)  \$(LDADD) \$(LIBS) $extra_libraries

try-many.o: try-many.c \$(top_srcdir)/tests/devel/try.c $trydeps
	\$(COMPILE) -I\$(top_srcdir)/tests/devel -c try-many.c

EOF

print_ansi2knr("speed-many");
print_ansi2knr("try-many",
	       "\$(top_srcdir)/tests/devel/try.c",
	       "-I\$(top_srcdir)/tests/devel");

print MAKEFILE <<EOF;
RM_TMP = rm -f
CFLAGS_TESTS = -DSIZE=50 -DTIMES=1 -DRANDOM -DCLOCK=333000000
CFLAGS_TESTS_SP = -DSIZE=1024 -DNOCHECK -DOPS=200000000 -DCLOCK=333000000
EOF

close MAKEFILE or die;

print "Total $count_files files, $count_functions functions\n";



# Local variables:
# perl-indent-level: 2
# End: