-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPLMATH.PAS
369 lines (329 loc) · 7.98 KB
/
APLMATH.PAS
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
{$I COMPILER.INC}
unit AplMath;
interface
uses
AplConst;
type
PWordArray = ^TWordArray;
TWordArray = array[1..MaxVarSize div SizeOf(word)] of word;
function Min(ANum1, ANum2: integer): integer; far;
function MinB(ANum1, ANum2: byte): byte; far;
function MinS(ANum1, ANum2: shortint): shortint; far;
function MinW(ANum1, ANum2: word): word; far;
function MinL(ANum1, ANum2: longint): longint; far;
function Max(ANum1, ANum2: integer): integer; far;
function MaxB(ANum1, ANum2: byte): byte; far;
function MaxS(ANum1, ANum2: shortint): shortint; far;
function MaxW(ANum1, ANum2: word): word; far;
function MaxL(ANum1, ANum2: longint): longint; far;
function Clamp(AValue, AMin, AMax: integer): integer; far;
function ClampB(AValue, AMin, AMax: byte): byte; far;
function ClampW(AValue, AMin, AMax: word): word; far;
function ClampL(AValue, AMin, AMax: longint): longint; far;
function ClampR(AValue, AMin, AMax: real): real; far;
function ClampS(AValue, AMin, AMax: single): single; far;
function ClampD(AValue, AMin, AMax: double): double; far;
function LowestW(AValues: PWordArray; ACount: word): word; far;
function RangeContains(AValue, AMin, AMax: integer): boolean; far;
function RangeContainsW(AValue, AMin, AMax: word): boolean; far;
function RangeContainsB(AValue, AMin, AMax: byte): boolean; far;
function RangeContainsS(AValue, AMin, AMax: shortint): boolean; far;
function RangeContainsL(AValue, AMin, AMax: longint): boolean; far;
function HighestW(AValues: PWordArray; ACount: word): word; far;
function SumW(AValues: PWordArray; ACount: word): longint; far;
function GreatestDivisorL(ANum1, ANum2: longint): longint; far;
function GreatestDivisorW(ANum1, ANum2: word): word; far;
function GreatestDivisorB(ANum1, ANum2: byte): byte; far;
function GreatestDivisorS(ANum1, ANum2: shortint): shortint; far;
function GreatestDivisor(ANum1, ANum2: integer): integer; far;
const
RadianConvert: real = 57.295779513082320876798154814105;
DegreeConvert: real = 0.017453292519943295769236907684886;
implementation
function Min(ANum1, ANum2: integer): integer; assembler;
asm
mov ax, ANum1
cmp ax, ANum2
jle @end
mov ax, ANum2
@end:
end;
function MinW(ANum1, ANum2: word): word; assembler;
asm
mov ax, ANum1
cmp ax, ANum2
jbe @end
mov ax, ANum2
@end:
end;
function MinS(ANum1, ANum2: shortint): shortint; assembler;
asm
mov al, ANum1
cmp al, ANum2
jle @end
mov al, ANum2
@end:
end;
function MinB(ANum1, ANum2: byte): byte; assembler;
asm
mov al, ANum1
cmp al, ANum2
jbe @end
mov al, ANum2
@end:
end;
function MinL(ANum1, ANum2: longint): longint;
begin
MinL := ANum1;
if ANum2 < ANum1 then
MinL := ANum2;
end;
function Max(ANum1, ANum2: integer): integer; assembler;
asm
mov ax, ANum1
cmp ax, ANum2
jge @end
mov ax, ANum2
@end:
end;
function MaxW(ANum1, ANum2: word): word; assembler;
asm
mov ax, ANum1
cmp ax, ANum2
jae @end
mov ax, ANum2
@end:
end;
function MaxS(ANum1, ANum2: shortint): shortint; assembler;
asm
mov al, ANum1
cmp al, ANum2
jge @end
mov al, ANum2
@end:
end;
function MaxB(ANum1, ANum2: byte): byte; assembler;
asm
mov al, ANum1
cmp al, ANum2
jae @end
mov al, ANum2
@end:
end;
function MaxL(ANum1, ANum2: longint): longint;
begin
MaxL := ANum1;
if ANum2 > ANum1 then
MaxL := ANum2;
end;
function MinReal(ANum1, ANum2: real): real;
begin
MinReal := ANum1;
if ANum2 < ANum1 then
MinReal := ANum2;
end;
function MaxReal(ANum1, ANum2: real): real;
begin
MaxReal := ANum1;
if ANum2 > ANum1 then
MaxReal := ANum2;
end;
function Clamp(AValue, AMin, AMax: integer): integer; assembler;
asm
mov ax, AValue
mov dx, AMin
cmp ax, dx
jge @max
mov ax, dx
jmp @end
@max:
mov dx, AMax
cmp ax, dx
jle @end
mov ax, dx
@end:
end;
function ClampB(AValue, AMin, AMax: byte): byte; assembler;
asm
mov al, AValue
mov dl, AMin
cmp al, dl
jae @max
mov al, dl
jmp @end
@max:
mov dl, AMax
cmp al, dl
jbe @end
mov al, dl
@end:
end;
function ClampW(AValue, AMin, AMax: word): word; assembler;
asm
mov ax, AValue
mov dx, AMin
cmp ax, dx
jae @max
mov ax, dx
jmp @end
@max:
mov dx, AMax
cmp ax, dx
jbe @end
mov ax, dx
@end:
end;
function ClampL(AValue, AMin, AMax: longint): longint;
begin
ClampL := AValue;
if AValue < AMin then
ClampL := AMin
else if AValue > AMax then
ClampL := AMax;
end;
function ClampR(AValue, AMin, AMax: real): real;
begin
ClampR := AValue;
if AValue < AMin then
ClampR := AMin
else if AValue > AMax then
ClampR := AMax;
end;
function ClampS(AValue, AMin, AMax: single): single;
begin
ClampS := AValue;
if AValue < AMin then
ClampS := AMin
else if AValue > AMax then
ClampS := AMax;
end;
function ClampD(AValue, AMin, AMax: double): double;
begin
ClampD := AValue;
if AValue < AMin then
ClampD := AMin
else if AValue > AMax then
ClampD := AMax;
end;
function LowestW(AValues: PWordArray; ACount: word): word; assembler;
asm
les di, AValues
mov cx, ACount
xor ax, ax
@loop:
mov bx, es:[di]
cmp ax, bx
jl @skip
mov ax, bx
@skip:
add di, 2
loop @loop
end;
function HighestW(AValues: PWordArray; ACount: word): word; assembler;
asm
les di, AValues
mov cx, ACount
xor ax, ax
@loop:
mov bx, es:[di]
cmp ax, bx
jg @skip
mov ax, bx
@skip:
add di, 2
loop @loop
end;
function SumW(AValues: PWordArray; ACount: word): longint; assembler;
asm
les di, AValues
mov cx, ACount
xor ax, ax
xor dx, dx
@loop:
mov bx, es:[di]
add ax, bx
adc dx, 0
add di, 2
loop @loop
end;
function GreatestDivisorL(ANum1, ANum2: longint): longint;
begin
GreatestDivisorL := ANum1;
while ANum2 <> 0 do begin
ANum1 := ANum1 mod ANum2;
if ANum1 = 0 then begin
GreatestDivisorL := ANum2;
break;
end;
ANum2 := ANum2 mod ANum1;
end;
end;
function GreatestDivisorW(ANum1, ANum2: word): word;
begin
GreatestDivisorW := ANum1;
while ANum2 <> 0 do begin
ANum1 := ANum1 mod ANum2;
if ANum1 = 0 then begin
GreatestDivisorW := ANum2;
break;
end;
ANum2 := ANum2 mod ANum1;
end;
end;
function GreatestDivisorB(ANum1, ANum2: byte): byte;
begin
GreatestDivisorB := ANum1;
while ANum2 <> 0 do begin
ANum1 := ANum1 mod ANum2;
if ANum1 = 0 then begin
GreatestDivisorB := ANum2;
break;
end;
ANum2 := ANum2 mod ANum1;
end;
end;
function GreatestDivisorS(ANum1, ANum2: shortint): shortint;
begin
GreatestDivisorS := ANum1;
while ANum2 <> 0 do begin
ANum1 := ANum1 mod ANum2;
if ANum1 = 0 then begin
GreatestDivisorS := ANum2;
break;
end;
ANum2 := ANum2 mod ANum1;
end;
end;
function GreatestDivisor(ANum1, ANum2: integer): integer;
begin
GreatestDivisor := ANum1;
while ANum2 <> 0 do begin
ANum1 := ANum1 mod ANum2;
if ANum1 = 0 then begin
GreatestDivisor := ANum2;
break;
end;
ANum2 := ANum2 mod ANum1;
end;
end;
function RangeContains(AValue, AMin, AMax: integer): boolean;
begin
RangeContains := (AValue >= AMin) and (AValue <= AMax);
end;
function RangeContainsW(AValue, AMin, AMax: word): boolean;
begin
RangeContainsW := (AValue >= AMin) and (AValue <= AMax);
end;
function RangeContainsB(AValue, AMin, AMax: byte): boolean;
begin
RangeContainsB := (AValue >= AMin) and (AValue <= AMax);
end;
function RangeContainsS(AValue, AMin, AMax: shortint): boolean;
begin
RangeContainsS := (AValue >= AMin) and (AValue <= AMax);
end;
function RangeContainsL(AValue, AMin, AMax: longint): boolean;
begin
RangeContainsL := (AValue >= AMin) and (AValue <= AMax);
end;
end.