/security/nss/lib/freebl/mpi/types.pl

http://github.com/zpao/v8monkey · Perl · 162 lines · 76 code · 25 blank · 61 comment · 7 complexity · 95dd3e4fdfbb4e70ad72940af2a9527b MD5 · raw file

  1. #!/usr/bin/perl
  2. #
  3. # types.pl - find recommended type definitions for digits and words
  4. #
  5. # This script scans the Makefile for the C compiler and compilation
  6. # flags currently in use, and using this combination, attempts to
  7. # compile a simple test program that outputs the sizes of the various
  8. # unsigned integer types, in bytes. Armed with these, it finds all
  9. # the "viable" type combinations for mp_digit and mp_word, where
  10. # viability is defined by the requirement that mp_word be at least two
  11. # times the precision of mp_digit.
  12. #
  13. # Of these, the one with the largest digit size is chosen, and
  14. # appropriate typedef statements are written to standard output.
  15. # ***** BEGIN LICENSE BLOCK *****
  16. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  17. #
  18. # The contents of this file are subject to the Mozilla Public License Version
  19. # 1.1 (the "License"); you may not use this file except in compliance with
  20. # the License. You may obtain a copy of the License at
  21. # http://www.mozilla.org/MPL/
  22. #
  23. # Software distributed under the License is distributed on an "AS IS" basis,
  24. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  25. # for the specific language governing rights and limitations under the
  26. # License.
  27. #
  28. # The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
  29. #
  30. # The Initial Developer of the Original Code is
  31. # Michael J. Fromberger <sting@linguist.dartmouth.edu>.
  32. # Portions created by the Initial Developer are Copyright (C) 2000
  33. # the Initial Developer. All Rights Reserved.
  34. #
  35. # Contributor(s):
  36. #
  37. # Alternatively, the contents of this file may be used under the terms of
  38. # either the GNU General Public License Version 2 or later (the "GPL"), or
  39. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  40. # in which case the provisions of the GPL or the LGPL are applicable instead
  41. # of those above. If you wish to allow use of your version of this file only
  42. # under the terms of either the GPL or the LGPL, and not to allow others to
  43. # use your version of this file under the terms of the MPL, indicate your
  44. # decision by deleting the provisions above and replace them with the notice
  45. # and other provisions required by the GPL or the LGPL. If you do not delete
  46. # the provisions above, a recipient may use your version of this file under
  47. # the terms of any one of the MPL, the GPL or the LGPL.
  48. #
  49. # ***** END LICENSE BLOCK *****
  50. # $Id: types.pl,v 1.2 2005/02/02 22:28:22 gerv%gerv.net Exp $
  51. #
  52. @_=split(/\//,$0);chomp($prog=pop(@_));
  53. # The array of integer types to be considered...
  54. @TYPES = (
  55. "unsigned char",
  56. "unsigned short",
  57. "unsigned int",
  58. "unsigned long"
  59. );
  60. # Macro names for the maximum unsigned value of each type
  61. %TMAX = (
  62. "unsigned char" => "UCHAR_MAX",
  63. "unsigned short" => "USHRT_MAX",
  64. "unsigned int" => "UINT_MAX",
  65. "unsigned long" => "ULONG_MAX"
  66. );
  67. # Read the Makefile to find out which C compiler to use
  68. open(MFP, "<Makefile") or die "$prog: Makefile: $!\n";
  69. while(<MFP>) {
  70. chomp;
  71. if(/^CC=(.*)$/) {
  72. $cc = $1;
  73. last if $cflags;
  74. } elsif(/^CFLAGS=(.*)$/) {
  75. $cflags = $1;
  76. last if $cc;
  77. }
  78. }
  79. close(MFP);
  80. # If we couldn't find that, use 'cc' by default
  81. $cc = "cc" unless $cc;
  82. printf STDERR "Using '%s' as the C compiler.\n", $cc;
  83. print STDERR "Determining type sizes ... \n";
  84. open(OFP, ">tc$$.c") or die "$prog: tc$$.c: $!\n";
  85. print OFP "#include <stdio.h>\n\nint main(void)\n{\n";
  86. foreach $type (@TYPES) {
  87. printf OFP "\tprintf(\"%%d\\n\", (int)sizeof(%s));\n", $type;
  88. }
  89. print OFP "\n\treturn 0;\n}\n";
  90. close(OFP);
  91. system("$cc $cflags -o tc$$ tc$$.c");
  92. die "$prog: unable to build test program\n" unless(-x "tc$$");
  93. open(IFP, "./tc$$|") or die "$prog: can't execute test program\n";
  94. $ix = 0;
  95. while(<IFP>) {
  96. chomp;
  97. $size{$TYPES[$ix++]} = $_;
  98. }
  99. close(IFP);
  100. unlink("tc$$");
  101. unlink("tc$$.c");
  102. print STDERR "Selecting viable combinations ... \n";
  103. while(($type, $size) = each(%size)) {
  104. push(@ts, [ $size, $type ]);
  105. }
  106. # Sort them ascending by size
  107. @ts = sort { $a->[0] <=> $b->[0] } @ts;
  108. # Try all possible combinations, finding pairs in which the word size
  109. # is twice the digit size. The number of possible pairs is too small
  110. # to bother doing this more efficiently than by brute force
  111. for($ix = 0; $ix <= $#ts; $ix++) {
  112. $w = $ts[$ix];
  113. for($jx = 0; $jx <= $#ts; $jx++) {
  114. $d = $ts[$jx];
  115. if($w->[0] == 2 * $d->[0]) {
  116. push(@valid, [ $d, $w ]);
  117. }
  118. }
  119. }
  120. # Sort descending by digit size
  121. @valid = sort { $b->[0]->[0] <=> $a->[0]->[0] } @valid;
  122. # Select the maximum as the recommended combination
  123. $rec = shift(@valid);
  124. printf("typedef %-18s mp_sign;\n", "char");
  125. printf("typedef %-18s mp_digit; /* %d byte type */\n",
  126. $rec->[0]->[1], $rec->[0]->[0]);
  127. printf("typedef %-18s mp_word; /* %d byte type */\n",
  128. $rec->[1]->[1], $rec->[1]->[0]);
  129. printf("typedef %-18s mp_size;\n", "unsigned int");
  130. printf("typedef %-18s mp_err;\n\n", "int");
  131. printf("#define %-18s (CHAR_BIT*sizeof(mp_digit))\n", "DIGIT_BIT");
  132. printf("#define %-18s %s\n", "DIGIT_MAX", $TMAX{$rec->[0]->[1]});
  133. printf("#define %-18s (CHAR_BIT*sizeof(mp_word))\n", "MP_WORD_BIT");
  134. printf("#define %-18s %s\n\n", "MP_WORD_MAX", $TMAX{$rec->[1]->[1]});
  135. printf("#define %-18s (DIGIT_MAX+1)\n\n", "RADIX");
  136. printf("#define %-18s \"%%0%dX\"\n", "DIGIT_FMT", (2 * $rec->[0]->[0]));
  137. exit 0;