-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SiViC_Common.pas
166 lines (128 loc) · 4.33 KB
/
SiViC_Common.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
unit SiViC_Common;
{$INCLUDE '.\SiViC_defs.inc'}
interface
uses
AuxTypes;
{$IFDEF FPC_DisableWarns}
{$WARN 3031 OFF} // Values in enumeration types have to be ascending
{$ENDIF}
type
TSVCValueSize = (vsUndefined,vsByte,vsWord,vsLong,vsQuad,
vsNative = vsWord,vsComp = vsLong);
// basic integer types
TSVCUByte = UInt8; TSVCByte = TSVCUByte;
TSVCSByte = Int8;
TSVCUWord = UInt16; TSVCWord = TSVCUWord;
TSVCSWord = Int16;
TSVCULong = UInt32; TSVCLong = TSVCULong;
TSVCSLong = Int32;
TSVCUQuad = UInt64; TSVCQuad = TSVCUQuad;
TSVCSQuad = Int64;
// native integers (width of a register)
TSVCUNative = TSVCUWord; TSVCNative = TSVCUNative;
TSVCSNative = TSVCSWord;
// smallest signed integer larger than native (used for computations on natives)
TSVCComp = TSVCSLong;
// some other integer types
TSVCRel8 = TSVCSByte;
TSVCRel16 = TSVCSWord;
TSVCRel32 = TSVCSLong;
TSVCRel64 = TSVCSQuad;
TSVCNumber = TSVCSLong;
// dynamic array types
TSVCByteArray = array of TSVCByte;
TSVCWordArray = array of TSVCWord;
TSVCNativeArray = array of TSVCNative;
const
SVC_SZ_BYTE = SizeOf(TSVCByte);
SVC_SZ_WORD = SizeOf(TSVCWord);
SVC_SZ_LONG = SizeOf(TSVCLong);
SVC_SZ_QUAD = SizeOf(TSVCQuad);
SVC_SZ_NATIVE = SizeOf(TSVCNative);
Function ByteParity(Value: TSVCByte): Boolean;
Function WordParity(Value: TSVCWord): Boolean;
Function IntMin(A,B: Integer): Integer;{$IFDEF CanInline} inline;{$ENDIF}
Function MemMin(A,B: TMemSize): TMemSize;{$IFDEF CanInline} inline;{$ENDIF}
Function BoolToByte(Val: Boolean): TSVCByte;{$IFDEF CanInline} inline;{$ENDIF}
Function ValueSize(ValueSize: TSVCValueSize): TSVCNumber;
procedure AddToArray(var Arr: TSVCByteArray; const Data; Size: TSVCNative);
type
TSVCCharSet = set of AnsiChar;
Function SVC_CharInSet(C: AnsiChar; const CharSet: TSVCCharSet): Boolean; overload;{$IFDEF CanInline} inline;{$ENDIF}
Function SVC_CharInSet(C: WideChar; const CharSet: TSVCCharSet): Boolean; overload;{$IFDEF CanInline} inline;{$ENDIF}
implementation
{$IFDEF FPC_DisableWarns}
{$WARN 4055 OFF} // Conversion between ordinals and pointers is not portable
{$ENDIF}
Function ByteParity(Value: TSVCByte): Boolean;
begin
Value := Value xor (Value shr 4);
Value := Value xor (Value shr 2);
Value := Value xor (Value shr 1);
Result := (Value and 1) = 0;
end;
//------------------------------------------------------------------------------
Function WordParity(Value: TSVCWord): Boolean;
begin
Value := Value xor (Value shr 8);
Value := Value xor (Value shr 4);
Value := Value xor (Value shr 2);
Value := Value xor (Value shr 1);
Result := (Value and 1) = 0;
end;
//------------------------------------------------------------------------------
Function IntMin(A,B: Integer): Integer;
begin
If A > B then Result := B
else Result := A;
end;
//------------------------------------------------------------------------------
Function MemMin(A,B: TMemSize): TMemSize;
begin
If A > B then Result := B
else Result := A;
end;
//------------------------------------------------------------------------------
Function BoolToByte(Val: Boolean): TSVCByte;
begin
If Val then Result := 1
else Result := 0;
end;
//------------------------------------------------------------------------------
Function ValueSize(ValueSize: TSVCValueSize): TSVCNumber;
begin
case ValueSize of
vsByte: Result := SVC_SZ_BYTE;
vsWord: Result := SVC_SZ_WORD;
vsLong: Result := SVC_SZ_LONG;
vsQuad: Result := SVC_SZ_QUAD;
else
Result := 0;
end;
end;
//------------------------------------------------------------------------------
procedure AddToArray(var Arr: TSVCByteArray; const Data; Size: TSVCNative);
var
i: Integer;
begin
If size > 0 then
begin
SetLength(Arr,Length(Arr) + Size);
For i := 0 to Pred(Size) do
Arr[Length(Arr) - Size + i] := TSVCByte(Pointer(PtrUInt(Addr(Data)) + PtrUInt(i))^);
end;
end;
//------------------------------------------------------------------------------
Function SVC_CharInSet(C: AnsiChar; const CharSet: TSVCCharSet): Boolean; overload;
begin
Result := C in CharSet;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function SVC_CharInSet(C: WideChar; const CharSet: TSVCCharSet): Boolean; overload;
begin
If Ord(C) <= 255 then
Result := AnsiChar(C) in CharSet
else
Result := False;
end;
end.