summaryrefslogtreecommitdiff
path: root/rtl/avr/math.inc
blob: 2ebe5f77ef6bba4a85ecdd35673c433059ebe955 (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
{

    This file is part of the Free Pascal run time library.
    Copyright (c) 2008 by the Free Pascal development team.

    Implementation of mathematical Routines (only for real)

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{$i divide.inc}


{$ifndef FPC_SYSTEM_HAS_DIV_BYTE}
{$define FPC_SYSTEM_HAS_DIV_BYTE}

// z (dividend) = q(quotient) x n(divisor) + p(remainder)

// z in Ra, n in Rb, 0 in Rp
function fpc_div_byte(n, z: byte): byte; assembler; nostackframe;
{$ifdef FPC_IS_SYSTEM}[public,alias: 'FPC_DIV_BYTE'];{$endif}
asm
{$ifdef CPUAVR_16_REGS}
  cp R24, R17
{$else CPUAVR_16_REGS}
  cp R24, R1
{$endif CPUAVR_16_REGS}  
  brne .LNonZero
{$ifdef CPUAVR_HAS_JMP_CALL}
  call fpc_divbyzero
.LNonZero:
  call fpc_divmod_byte
{$else  CPUAVR_HAS_JMP_CALL}
  rcall fpc_divbyzero
.LNonZero:
  rcall fpc_divmod_byte
{$endif CPUAVR_HAS_JMP_CALL}

  mov R24, R22
end;

{It is a compilerproc (systemh.inc), make an alias for internal use.}
{$ifdef FPC_IS_SYSTEM}
function fpc_div_byte(n, z: byte): byte; external name 'FPC_DIV_BYTE';
{$endif FPC_IS_SYSTEM}
{$endif FPC_SYSTEM_HAS_DIV_BYTE}


{$ifndef FPC_SYSTEM_HAS_MOD_BYTE}
{$define FPC_SYSTEM_HAS_MOD_BYTE}

// z in Ra, n in Rb, 0 in Rp
function fpc_mod_byte(n, z: byte): byte; assembler; nostackframe;
{$ifdef FPC_IS_SYSTEM}[public,alias: 'FPC_MOD_BYTE'];{$endif}
asm
{$ifdef CPUAVR_16_REGS}
  cp R24, R17
{$else CPUAVR_16_REGS}
  cp R24, R1
{$endif CPUAVR_16_REGS}
  brne .LNonZero
{$ifdef CPUAVR_HAS_JMP_CALL}
  call fpc_divbyzero
.LNonZero:
  call fpc_divmod_byte
{$else  CPUAVR_HAS_JMP_CALL}
  rcall fpc_divbyzero
.LNonZero:
  rcall fpc_divmod_byte
{$endif CPUAVR_HAS_JMP_CALL}

  mov R24, R20
end;

{It is a compilerproc (systemh.inc), make an alias for internal use.}
{$ifdef FPC_IS_SYSTEM}
function fpc_mod_byte(n, z: byte): byte; external name 'FPC_MOD_BYTE';
{$endif FPC_IS_SYSTEM}
{$endif FPC_SYSTEM_HAS_MOD_BYTE}


{$ifndef FPC_SYSTEM_HAS_DIV_WORD}
{$define FPC_SYSTEM_HAS_DIV_WORD}

// z in Ra, n in Rb, 0 in Rp
function fpc_div_word(n, z: word): word; assembler; nostackframe;
{$ifdef FPC_IS_SYSTEM}[public,alias: 'FPC_DIV_WORD'];{$endif}
asm
{$ifdef CPUAVR_16_REGS}
  cp R24, R17
  cpc R25, R17
{$else CPUAVR_16_REGS}
  cp R24, R1
  cpc R25, R1
{$endif CPUAVR_16_REGS}
  brne .LNonZero
{$ifdef CPUAVR_HAS_JMP_CALL}
  call fpc_divbyzero
.LNonZero:
  call fpc_divmod_word
{$else  CPUAVR_HAS_JMP_CALL}
  rcall fpc_divbyzero
.LNonZero:
  rcall fpc_divmod_word
{$endif CPUAVR_HAS_JMP_CALL}

{$if not(defined(CPUAVR_HAS_MOVW))}
  mov R24, R20
  mov R25, R21
{$else CPUAVR_16_REGS}
  movw R24, R22
{$endif CPUAVR_16_REGS}
end;

{It is a compilerproc (systemh.inc), make an alias for internal use.}
{$ifdef FPC_IS_SYSTEM}
function fpc_div_word(n, z: word): word; external name 'FPC_DIV_WORD';
{$endif FPC_IS_SYSTEM}
{$endif FPC_SYSTEM_HAS_DIV_WORD}


{$ifndef FPC_SYSTEM_HAS_MOD_WORD}
{$define FPC_SYSTEM_HAS_MOD_WORD}

// z in Ra, n in Rb, 0 in Rp
function fpc_mod_word(n, z: word): word; assembler; nostackframe;
{$ifdef FPC_IS_SYSTEM}[public,alias: 'FPC_MOD_WORD'];{$endif}
asm
{$ifdef CPUAVR_16_REGS}
  cp R24, R17
  cpc R25, R17
{$else CPUAVR_16_REGS}
  cp R24, R1
  cpc R25, R1
{$endif CPUAVR_16_REGS}
  brne .LNonZero
{$ifdef CPUAVR_HAS_JMP_CALL}
  call fpc_divbyzero
.LNonZero:
  call fpc_divmod_word
{$else  CPUAVR_HAS_JMP_CALL}
  rcall fpc_divbyzero
.LNonZero:
  rcall fpc_divmod_word
{$endif CPUAVR_HAS_JMP_CALL}

{$if not(defined(CPUAVR_HAS_MOVW))}
  mov R24, R20
  mov R25, R21
{$else not(defined(CPUAVR_HAS_MOVW))}
  movw R24, R20
{$endif not(defined(CPUAVR_HAS_MOVW))}
end;

{It is a compilerproc (systemh.inc), make an alias for internal use.}
{$ifdef FPC_IS_SYSTEM}
function fpc_mod_word(n, z: word): word; external name 'FPC_MOD_WORD';
{$endif FPC_IS_SYSTEM}
{$endif FPC_SYSTEM_HAS_MOD_WORD}


{$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
{$define FPC_SYSTEM_HAS_DIV_DWORD}

// z in Ra, n in Rb, 0 in Rp
function fpc_div_dword(n, z: dword): dword; assembler; nostackframe;
{$ifdef FPC_IS_SYSTEM}[public,alias: 'FPC_DIV_DWORD'];{$endif}
asm
{$ifdef CPUAVR_16_REGS}
  cp R24, R17
  cpc R25, R17
  cpc R22, R17
  cpc R23, R17
{$else CPUAVR_16_REGS}
  cp R24, R1
  cpc R25, R1
  cpc R22, R1
  cpc R23, R1
{$endif CPUAVR_16_REGS}
  brne .LNonZero
{$ifdef CPUAVR_HAS_JMP_CALL}
  call fpc_divbyzero
.LNonZero:
  call fpc_divmod_dword
{$else  CPUAVR_HAS_JMP_CALL}
  rcall fpc_divbyzero
.LNonZero:
  rcall fpc_divmod_dword
{$endif CPUAVR_HAS_JMP_CALL}

{$if not(defined(CPUAVR_HAS_MOVW))}
  mov R22, R18    // Move result from R18:R21 to R22:R25
  mov R23, R19    // Move result from R18:R21 to R22:R25
  mov R24, R20
  mov R25, R21
{$else not(defined(CPUAVR_HAS_MOVW))}
  movw R22, R18    // Move result from R18:R21 to R22:R25
  movw R24, R20
{$endif not(defined(CPUAVR_HAS_MOVW))}
end;

{It is a compilerproc (systemh.inc), make an alias for internal use.}
{$ifdef FPC_IS_SYSTEM}
function fpc_div_dword(n, z: dword): dword; external name 'FPC_DIV_DWORD';
{$endif FPC_IS_SYSTEM}
{$endif FPC_SYSTEM_HAS_DIV_DWORD}


{$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
{$define FPC_SYSTEM_HAS_MOD_DWORD}

// z in Ra, n in Rb, 0 in Rp
function fpc_mod_dword(n, z: dword): dword; assembler; nostackframe;
{$ifdef FPC_IS_SYSTEM}[public,alias: 'FPC_MOD_DWORD'];{$endif}
asm
{$ifdef CPUAVR_16_REGS}
  cp R24, R17
  cpc R25, R17
  cpc R22, R17
  cpc R23, R17
{$else CPUAVR_16_REGS}
  cp R24, R1
  cpc R25, R1
  cpc R22, R1
  cpc R23, R1
{$endif CPUAVR_16_REGS}
  brne .LNonZero
{$ifdef CPUAVR_HAS_JMP_CALL}
  call fpc_divbyzero
.LNonZero:
  call fpc_divmod_dword
{$else  CPUAVR_HAS_JMP_CALL}
  rcall fpc_divbyzero
.LNonZero:
  rcall fpc_divmod_dword
{$endif CPUAVR_HAS_JMP_CALL}
end;

{It is a compilerproc (systemh.inc), make an alias for internal use.}
{$ifdef FPC_IS_SYSTEM}
function fpc_mod_dword(n, z: dword): dword; external name 'FPC_MOD_DWORD';
{$endif FPC_IS_SYSTEM}
{$endif FPC_SYSTEM_HAS_MOD_DWORD}


{$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
{$define FPC_SYSTEM_HAS_BSF_BYTE}

function BsfByte(Const AValue: Byte): Byte;
  var
    i, value: byte;
  begin
    Value:=AValue;
    for i:=0 to 7 do
      begin
        if odd(Value) then
          exit(i);
        Value:=Value shr 1;
      end;
    result:=$FF;
  end;

{$endif}


{$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
{$define FPC_SYSTEM_HAS_BSR_BYTE}

function BsrByte(Const AValue: Byte): Byte;
  var
    i, value: byte;
  begin
    Value:=AValue;
    for i:=0 to 7 do
      begin
        if (Value and $80)<>0 then
          exit(i);
        Value:=Value shl 1;
      end;
    result:=$FF;
  end;

{$endif}