summaryrefslogtreecommitdiff
path: root/tests/bench/bdiv.pp
blob: f97e238116c51dae0d64dda2af64b7d59761f66c (plain)
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
{ %OPT=-O2 }
program bdiv;

{$mode objfpc}{$H+}

uses
  SysUtils;

{ Utility functions }
function GetRealTime(const st: TSystemTime): Real;
  begin
    Result := st.Hour*3600.0 + st.Minute*60.0 + st.Second + st.MilliSecond/1000.0;
  end;

{$push}
{$warn 5057 off}
function GetRealTime : Real;
  var
    st:TSystemTime;
  begin
    GetLocalTime(st);
    result:=GetRealTime(st);
  end;
{$pop}

const
  ITERATIONS = 524288;
  INTERNAL_LOOPS = 64;

{ TTestAncestor }
type
  TTestAncestor = class
    private
      FStartTime: Real;
      FEndTime: Real;
      FAvgTime: Real;
      procedure SetStartTime;
      procedure SetEndTime;
    protected
      procedure DoTestIteration(Iteration: Integer); virtual; abstract;
    public
      constructor Create; virtual;
      destructor Destroy; override;
      procedure Run;
      function TestTitle: shortstring; virtual; abstract;
      function WriteResults: Boolean; virtual; abstract;
      property RunTime: Real read FAvgTime;
  end;

  TTestClass = class of TTestAncestor;

  TUInt32DivTest = class(TTestAncestor)
    protected
      FInputArray: array[$00..$FF] of Cardinal;
      FResultArray: array[$00..$FF] of Cardinal;
      function GetDivisor: Cardinal; virtual; abstract;
      function DoVariableDiv(Numerator: Cardinal): Cardinal; inline;
    public
      function WriteResults: Boolean; override;
  end;

  TUInt32ModTest = class(TUInt32DivTest)
    protected
      function DoVariableMod(Numerator: Cardinal): Cardinal; inline;
    public
      function WriteResults: Boolean; override;
  end;

  TSInt32DivTest = class(TTestAncestor)
    protected
      FInputArray: array[$00..$FF] of Integer;
      FResultArray: array[$00..$FF] of Integer;
      function GetDivisor: Integer; virtual; abstract;
      function DoVariableDiv(Numerator: Integer): Integer; inline;
    public
      function WriteResults: Boolean; override;
  end;

  TSInt32ModTest = class(TSInt32DivTest)
    protected
      function DoVariableMod(Numerator: Integer): Integer; inline;
    public
      function WriteResults: Boolean; override;
  end;

  TUInt64DivTest = class(TTestAncestor)
    protected
      FInputArray: array[$00..$FF] of QWord;
      FResultArray: array[$00..$FF] of QWord;
      function GetDivisor: QWord; virtual; abstract;
      function DoVariableDiv(Numerator: QWord): QWord; inline;
    public
      function WriteResults: Boolean; override;
  end;

  TUInt64ModTest = class(TUInt64DivTest)
    protected
      function DoVariableMod(Numerator: QWord): QWord; inline;
    public
      function WriteResults: Boolean; override;
  end;

  TSInt64DivTest = class(TTestAncestor)
    protected
      FInputArray: array[$00..$FF] of Int64;
      FResultArray: array[$00..$FF] of Int64;
      function GetDivisor: Int64; virtual; abstract;
      function DoVariableDiv(Numerator: Int64): Int64; inline;
    public
      function WriteResults: Boolean; override;
  end;

  TSInt64ModTest = class(TSInt64DivTest)
    protected
      function DoVariableMod(Numerator: Int64): Int64; inline;
    public
      function WriteResults: Boolean; override;
  end;

{$I bdiv_u32.inc}
{$I bdiv_u64.inc}
{$I bdiv_s32.inc}
{$I bdiv_s64.inc}

{ TTestAncestor }

constructor TTestAncestor.Create;
  begin
    FStartTime := 0;
    FEndTime := 0;
    FAvgTime := 0;
  end;

destructor TTestAncestor.Destroy;
  begin
    inherited Destroy;
  end;

procedure TTestAncestor.SetStartTime;
  begin
    FStartTime := GetRealTime();
  end;

procedure TTestAncestor.SetEndTime;
  begin
    FEndTime := GetRealTime();
    if FEndTime < FStartTime then { Happens if the test runs past midnight }
      FEndTime := FEndTime + 86400.0;
  end;

procedure TTestAncestor.Run;
  var
    X: Integer;
  begin
    SetStartTime;
    for X := 0 to ITERATIONS - 1 do
      DoTestIteration(X);

    SetEndTime;

    FAvgTime := FEndTime - FStartTime;
  end;

{ TUInt32DivTest }

function TUInt32DivTest.DoVariableDiv(Numerator: Cardinal): Cardinal;
  begin
    Result := Numerator div GetDivisor;
  end;

function TUInt32DivTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: Cardinal;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableDiv(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' div ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ TUInt32ModTest }

function TUInt32ModTest.DoVariableMod(Numerator: Cardinal): Cardinal;
  begin
    Result := Numerator mod GetDivisor;
  end;

function TUInt32ModTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: Cardinal;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableMod(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' mod ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ TSInt32DivTest }

function TSInt32DivTest.DoVariableDiv(Numerator: Integer): Integer;
  begin
    Result := Numerator div GetDivisor;
  end;

function TSInt32DivTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: Integer;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableDiv(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' div ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ TSInt32ModTest }

function TSInt32ModTest.DoVariableMod(Numerator: Integer): Integer;
  begin
    Result := Numerator mod GetDivisor;
  end;

function TSInt32ModTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: Integer;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableMod(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' mod ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ TUInt64DivTest }

function TUInt64DivTest.DoVariableDiv(Numerator: QWord): QWord;
  begin
    Result := Numerator div GetDivisor;
  end;

function TUInt64DivTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: QWord;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableDiv(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' div ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ TUInt64ModTest }

function TUInt64ModTest.DoVariableMod(Numerator: QWord): QWord;
  begin
    Result := Numerator mod GetDivisor;
  end;

function TUInt64ModTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: QWord;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableMod(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' mod ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ TSInt64DivTest }

function TSInt64DivTest.DoVariableDiv(Numerator: Int64): Int64;
  begin
    Result := Numerator div GetDivisor;
  end;

function TSInt64DivTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: Int64;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableDiv(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' div ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ TSInt64ModTest }

function TSInt64ModTest.DoVariableMod(Numerator: Int64): Int64;
  begin
    Result := Numerator mod GetDivisor;
  end;

function TSInt64ModTest.WriteResults: Boolean;
  var
    X: Integer;
    Expected: Int64;
  begin
    Result := True;
    for X := 0 to 255 do
      begin
        Expected := DoVariableMod(FInputArray[X]);
        if FResultArray[X] <> Expected then
          begin
            WriteLn('FAIL - ', FInputArray[X], ' mod ', GetDivisor, '; expected ', Expected, ' got ', FResultArray[X]);
            Result := False;
            Exit;
          end;
      end;
  end;

{ Main function }
const
  TestClasses: array[0..53] of TTestClass = (
    TUInt32Bit1Test,
    TUInt32Bit1ModTest,
    TUInt32Bit2Test,
    TUInt32Bit2ModTest,
    TUInt32Bit3Test,
    TUInt32Bit3ModTest,
    TUInt32Bit10Test,
    TUInt32Bit10ModTest,
    TUInt32Bit100Test,
    TUInt32Bit100ModTest,
    TUInt32Bit1000Test,
    TUInt32Bit1000ModTest,
    TUInt32Bit60000Test,
    TUInt32Bit60000ModTest,
    TUInt32Bit146097Test,
    TUInt32Bit146097ModTest,
    TUInt32Bit3600000Test,
    TUInt32Bit3600000ModTest,
    TUInt64Bit1Test,
    TUInt64Bit1ModTest,
    TUInt64Bit2Test,
    TUInt64Bit2ModTest,
    TUInt64Bit3Test,
    TUInt64Bit3ModTest,
    TUInt64Bit5Test,
    TUInt64Bit5ModTest,
    TUInt64Bit10Test,
    TUInt64Bit10ModTest,
    TUInt64Bit100Test,
    TUInt64Bit100ModTest,
    TUInt64Bit1000000000Test,
    TUInt64Bit1000000000ModTest,
    TSInt32Bit1Test,
    TSInt32Bit1ModTest,
    TSInt32Bit100Test,
    TSInt32Bit100ModTest,
    TSInt64Bit1Test,
    TSInt64Bit1ModTest,
    TSInt64Bit10Test,
    TSInt64Bit10ModTest,
    TSInt64Bit18Test,
    TSInt64Bit18ModTest,
    TSInt64Bit24Test,
    TSInt64Bit24ModTest,
    TSInt64Bit100Test,
    TSInt64Bit100ModTest,
    TSInt64Bit153Test,
    TSInt64Bit153ModTest,
    TSInt64Bit1461Test,
    TSInt64Bit1461ModTest,
    TSInt64Bit10000Test,
    TSInt64Bit10000ModTest,
    TSInt64Bit86400000Test,
    TSInt64Bit86400000ModTest
  );

var
  CurrentObject: TTestAncestor;
  Failed: Boolean;
  X: Integer;
  SummedUpAverageDuration, AverageDuration : Double;
begin
  SummedUpAverageDuration := 0.0;
  Failed := False;
  WriteLn('Division compilation and timing test (using constants from System and Sysutils)');
  WriteLn('-------------------------------------------------------------------------------');
  for X := Low(TestClasses) to High(TestClasses) do
    begin
      try
        CurrentObject := TestClasses[X].Create;
        try
          Write(CurrentObject.TestTitle:43, ' - ');
          CurrentObject.Run;

          if CurrentObject.WriteResults then
            begin
              AverageDuration := ((CurrentObject.RunTime * 1000000000.0) / (ITERATIONS * INTERNAL_LOOPS));
              WriteLn('Pass - average iteration duration: ', AverageDuration:1:3, ' ns');
              SummedUpAverageDuration := SummedUpAverageDuration + AverageDuration;
            end
          else
            { Final average isn't processed if a test failed, so there's no need
              to calculate and add the average duration to it }
            Failed := True;

        finally
          CurrentObject.Free;
        end;
      except on E: Exception do
        begin
          WriteLn('Exception "', E.ClassName, '" raised while running test object of class "', TestClasses[X].ClassName, '"');
          Failed := True;
        end;
      end;
    end;

  if Failed then
    Halt(1);

  WriteLn(#10'ok');
  WriteLn('- Sum of average durations: ', SummedUpAverageDuration:1:3, ' ns');
  WriteLn('- Overall average duration: ', (SummedUpAverageDuration / Length(TestClasses)):1:3, ' ns');
end.