|
| 1 | +program GenerateBaseConversionTables; |
| 2 | + |
| 3 | +{$APPTYPE CONSOLE} |
| 4 | + |
| 5 | +{$R *.res} |
| 6 | + |
| 7 | +uses |
| 8 | + System.SysUtils, System.Math, System.Classes; |
| 9 | + |
| 10 | +type |
| 11 | + TBaseInfo = record |
| 12 | + MaxPower: UInt64; |
| 13 | + MaxDigits: Integer; |
| 14 | + PowerOfTwo: Boolean; |
| 15 | + MaxFactor: UInt32; |
| 16 | + end; |
| 17 | + |
| 18 | +var |
| 19 | + BaseInfos32: array[2..36] of TBaseInfo; |
| 20 | + BaseInfos64: array[2..36] of TBaseInfo; |
| 21 | + |
| 22 | + MaxFactorShift: Integer; |
| 23 | + |
| 24 | +function IsPowerofTwo(I: Integer): Boolean; |
| 25 | +begin |
| 26 | + Result := (I and (I - 1)) = 0; |
| 27 | +end; |
| 28 | + |
| 29 | +procedure FindMaxValue(const Base, Max: UInt64; var Info: TBaseInfo); |
| 30 | +var |
| 31 | + MaxPower: UInt64; |
| 32 | +begin |
| 33 | + if (Base < 2) or (Base > 36) then |
| 34 | + raise EInvalidArgument.Create('Invalid base value'); |
| 35 | + MaxPower := Max div Base; |
| 36 | + Write('Max = ', MaxPower, ': '); |
| 37 | + Info.PowerOfTwo := IsPowerofTwo(Base); |
| 38 | + Info.MaxPower := Base; |
| 39 | + Info.MaxDigits := 1; |
| 40 | + repeat |
| 41 | + Write(Info.MaxPower, ' '); |
| 42 | + Info.MaxPower := Info.MaxPower * Base; |
| 43 | + Inc(Info.MaxDigits); |
| 44 | + until Info.MaxPower >= MaxPower; |
| 45 | + Writeln(Info.MaxPower); |
| 46 | + Info.MaxFactor := Round((Ln(2) / Ln(Info.MaxPower)) * (1 shl MaxFactorShift)); |
| 47 | + Writeln; |
| 48 | +end; |
| 49 | + |
| 50 | +const |
| 51 | + Bools: array[Boolean] of string = ('False', 'True'); |
| 52 | + |
| 53 | +procedure WriteArray(W: TStreamWriter; const Name: string; const Bases: array of TBaseInfo; const Comment: string); |
| 54 | +var |
| 55 | + Comma: Char; |
| 56 | + I: Integer; |
| 57 | +begin |
| 58 | + Comma := ','; |
| 59 | + W.WriteLine(' // %s', [Comment]); |
| 60 | + W.WriteLine(' %s: array[TNumberBase] of BigInteger.TNumberBaseInfo = ', [Name]); |
| 61 | + W.WriteLine(' ('); |
| 62 | + |
| 63 | + for I := Low(Bases) to High(Bases) do |
| 64 | + begin |
| 65 | + if I = High(Bases) then |
| 66 | + Comma := ' '; |
| 67 | + W.WriteLine(Format(' (MaxPower: %20u; MaxDigits: %2d; PowerofTwo: %5s; MaxFactor: %6u)%s // Base %d', |
| 68 | + [Bases[I].MaxPower, Bases[I].MaxDigits, Bools[Bases[I].PowerOfTwo], Bases[I].MaxFactor, Comma, I + 2], |
| 69 | + TFormatSettings.Invariant)); |
| 70 | + end; |
| 71 | + |
| 72 | + W.WriteLine(' );'); |
| 73 | +end; |
| 74 | + |
| 75 | +procedure WriteArrays; |
| 76 | +var |
| 77 | + W: TStreamWriter; |
| 78 | +begin |
| 79 | + W := TStreamWriter.Create('bases.inc', False, TEncoding.UTF8); |
| 80 | + try |
| 81 | + W.WriteLine('{$IF CompilerVersion < 29.0}'); |
| 82 | + W.WriteLine(' {$IF (DEFINED(WIN32) or DEFINED(CPUX86)) AND NOT DEFINED(CPU32BITS)}'); |
| 83 | + W.WriteLine(' {$DEFINE CPU32BITS}'); |
| 84 | + W.WriteLine(' {$IFEND}'); |
| 85 | + W.WriteLine(' {$IF (DEFINED(WIN64) OR DEFINED(CPUX64)) AND NOT DEFINED(CPU64BITS)}'); |
| 86 | + W.WriteLine(' {$DEFINE CPU64BITS}'); |
| 87 | + W.WriteLine(' {$IFEND}'); |
| 88 | + W.WriteLine('{$IFEND}'); |
| 89 | + W.WriteLine; |
| 90 | + W.WriteLine('const'); |
| 91 | + W.WriteLine(' CMaxFactorShift = %d;', [MaxFactorShift]); |
| 92 | + |
| 93 | + W.WriteLine('{$IFDEF CPU64BITS}'); |
| 94 | + WriteArray(W, 'CBaseInfos', BaseInfos64, 'Maximum powers of given bases that fit into UInt64'); |
| 95 | + W.WriteLine('{$ELSE}'); |
| 96 | + WriteArray(W, 'CBaseInfos', BaseInfos32, 'Maximum powers of given bases that fit into UInt32'); |
| 97 | + W.WriteLine('{$ENDIF}'); |
| 98 | + W.WriteLine; |
| 99 | + finally |
| 100 | + W.Free; |
| 101 | + end; |
| 102 | +end; |
| 103 | + |
| 104 | +var |
| 105 | + I: Integer; |
| 106 | + |
| 107 | +begin |
| 108 | + try |
| 109 | + MaxFactorShift := 24; |
| 110 | + for I := 2 to 36 do |
| 111 | + begin |
| 112 | + FindMaxValue(I, High(UInt32), BaseInfos32[I]); |
| 113 | + FindMaxValue(I, High(UInt64), BaseInfos64[I]); |
| 114 | + end; |
| 115 | + WriteArrays; |
| 116 | + except |
| 117 | + on E: Exception do |
| 118 | + Writeln(E.ClassName, ': ', E.Message); |
| 119 | + end; |
| 120 | + Readln; |
| 121 | +end. |
| 122 | + |
0 commit comments