msebits.pas 21.8 KB
Newer Older
1
{ MSEgui Copyright (c) 1999-2012 by Martin Schreiber
mse1's avatar
mse1 committed
2 3 4 5 6 7 8 9 10 11

    See the file COPYING.MSE, 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.
}
unit msebits;

mse1's avatar
mse1 committed
12
{$ifdef FPC}{$mode objfpc}{$h+}{$endif}
mse1's avatar
mse1 committed
13 14
//bitmanipulationhelpers
interface
mse1's avatar
mse1 committed
15 16 17
uses
 msetypes;
 
mse1's avatar
mse1 committed
18 19 20
type
 bitnumty = 0..32;
const
21
 twoexp32 = flo64(4294967296.0);
22 23 24 25
 bytebits: array[0..8] of byte = ($01,$02,$04,$08,$10,$20,$40,$80,$00);
 bytemask: array[0..8] of byte = ($00,$01,$03,$07,$0f,$1f,$3f,$7f,$ff);
 bytebitsreverse: array[0..8] of byte = ($80,$40,$20,$10,$08,$04,$02,$01,$00);
         
mse1's avatar
mse1 committed
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
 bits: array[bitnumty] of longword = (
         $00000001,$00000002,$00000004,$00000008,
         $00000010,$00000020,$00000040,$00000080,
         $00000100,$00000200,$00000400,$00000800,
         $00001000,$00002000,$00004000,$00008000,
         $00010000,$00020000,$00040000,$00080000,
         $00100000,$00200000,$00400000,$00800000,
         $01000000,$02000000,$04000000,$08000000,
         $10000000,$20000000,$40000000,$80000000,
         $00000000);
 bitmask: array[bitnumty] of longword = (
         $00000000,$00000001,$00000003,$00000007,
         $0000000f,$0000001f,$0000003f,$0000007f,
         $000000ff,$000001ff,$000003ff,$000007ff,
         $00000fff,$00001fff,$00003fff,$00007fff,
         $0000ffff,$0001ffff,$0003ffff,$0007ffff,
         $000fffff,$001fffff,$003fffff,$007fffff,
         $00ffffff,$01ffffff,$03ffffff,$07ffffff,
         $0fffffff,$1fffffff,$3fffffff,$7fffffff,
         $ffffffff);
 bitreverse: array[byte] of byte = (
             $00,$80,$40,$c0,$20,$a0,$60,$e0,$10,$90,$50,$d0,$30,$b0,$70,$f0,
             $08,$88,$48,$c8,$28,$a8,$68,$e8,$18,$98,$58,$d8,$38,$b8,$78,$f8,
             $04,$84,$44,$c4,$24,$a4,$64,$e4,$14,$94,$54,$d4,$34,$b4,$74,$f4,
             $0c,$8c,$4c,$cc,$2c,$ac,$6c,$ec,$1c,$9c,$5c,$dc,$3c,$bc,$7c,$fc,
             $02,$82,$42,$c2,$22,$a2,$62,$e2,$12,$92,$52,$d2,$32,$b2,$72,$f2,
             $0a,$8a,$4a,$ca,$2a,$aa,$6a,$ea,$1a,$9a,$5a,$da,$3a,$ba,$7a,$fa,
             $06,$86,$46,$c6,$26,$a6,$66,$e6,$16,$96,$56,$d6,$36,$b6,$76,$f6,
             $0e,$8e,$4e,$ce,$2e,$ae,$6e,$ee,$1e,$9e,$5e,$de,$3e,$be,$7e,$fe,
             $01,$81,$41,$c1,$21,$a1,$61,$e1,$11,$91,$51,$d1,$31,$b1,$71,$f1,
             $09,$89,$49,$c9,$29,$a9,$69,$e9,$19,$99,$59,$d9,$39,$b9,$79,$f9,
             $05,$85,$45,$c5,$25,$a5,$65,$e5,$15,$95,$55,$d5,$35,$b5,$75,$f5,
             $0d,$8d,$4d,$cd,$2d,$ad,$6d,$ed,$1d,$9d,$5d,$dd,$3d,$bd,$7d,$fd,
             $03,$83,$43,$c3,$23,$a3,$63,$e3,$13,$93,$53,$d3,$33,$b3,$73,$f3,
             $0b,$8b,$4b,$cb,$2b,$ab,$6b,$eb,$1b,$9b,$5b,$db,$3b,$bb,$7b,$fb,
             $07,$87,$47,$c7,$27,$a7,$67,$e7,$17,$97,$57,$d7,$37,$b7,$77,$f7,
             $0f,$8f,$4f,$cf,$2f,$af,$6f,$ef,$1f,$9f,$5f,$df,$3f,$bf,$7f,$ff);
63 64 65

 intexp10ar: array[0..9] of integer = 
          (1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000);
66 67 68 69 70
 int64exp10ar: array[0..19] of int64 = 
          (1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,
           1000000000,10000000000,100000000000,1000000000000,10000000000000,
           100000000000000,1000000000000000,10000000000000000,
           100000000000000000,1000000000000000000);
mse1's avatar
mse1 committed
71 72
type
 int64recty = record
mse1's avatar
mse1 committed
73 74
  lsw: longword;
  msw: longword;
mse1's avatar
mse1 committed
75 76
 end;
 
mse1's avatar
mse1 committed
77
function highestbit(value: longword): integer;
mse1's avatar
mse1 committed
78
//0-> first, 31-> last($80000000), -1-> none ($00000000)
mse1's avatar
mse1 committed
79
function lowestbit(value: longword): integer;
mse1's avatar
mse1 committed
80
//0-> first, 31-> last($80000000), -1-> none ($00000000)
mse1's avatar
mse1 committed
81
function nextpowerof2(const avalue: longword): longword;
mse1's avatar
mse1 committed
82

mse1's avatar
mse1 committed
83 84 85 86 87
function highestbit64(value: qword): integer;
//0-> first, 63-> last($8000000000000000), -1-> none ($0000000000000000)
function lowestbit64(value: qword): integer;
//0-> first, 63-> last($8000000000000000), -1-> none ($0000000000000000)

mse1's avatar
mse1 committed
88 89 90
function replacebits(const new,old,mask: byte): byte; overload;
function replacebits(const new,old,mask: word): word; overload;
function replacebits(const new,old,mask: longword): longword; overload;
91 92 93 94 95
function replacebits1(var dest: byte; const value,mask: byte): boolean; overload;
function replacebits1(var dest: word; const value,mask: word): boolean; overload;
function replacebits1(var dest: longword;
                                   const value,mask: longword): boolean; overload;
                            //true if modified
mse1's avatar
mse1 committed
96 97 98 99 100 101 102
function bitschanged(const a,b,mask: byte): boolean; overload;
function bitschanged(const a,b,mask: word): boolean; overload;
function bitschanged(const a,b,mask: longword): boolean; overload;

function setsinglebit(const new,old,mask: byte): byte; overload;
function setsinglebit(const new,old,mask: word): word; overload;
function setsinglebit(const new,old,mask: longword): longword; overload;
103 104 105 106 107 108
function setsinglebit(const new,old: byte;
                            const mask: array of byte): byte; overload;
function setsinglebit(const new,old: word;
                            const mask: array of word): word; overload;
function setsinglebit(const new,old: longword;
                            const mask: array of longword): longword; overload;
109
{$ifndef FPC}
110 111
function setsinglebitar8(const new,old: byte;
                            const mask: array of byte): byte;
112
function setsinglebitar16(const new,old: word;
113
                            const mask: array of word): word;
114
function setsinglebitar32(const new,old: longword;
115
                            const mask: array of longword): longword;
116
{$endif}
mse1's avatar
mse1 committed
117 118 119 120 121 122 123 124 125 126 127

function checkbit(const value: byte; const bitnum: integer): boolean; overload;
function checkbit(const value: word; const bitnum: integer): boolean; overload;
function checkbit(const value: longword; const bitnum: integer): boolean; overload;

function updatebit(var dest: byte; bitnum: integer; value: boolean): boolean; overload;
             //true if changed
function updatebit(var dest: word; bitnum: integer; value: boolean): boolean; overload;
             //true if changed
function updatebit(var dest: longword; bitnum: integer; value: boolean):boolean; overload;
             //true if changed
128 129 130
procedure updatebit1(var dest: byte; bitnum: integer; value: boolean); overload;
procedure updatebit1(var dest: word; bitnum: integer; value: boolean); overload;
procedure updatebit1(var dest: longword; bitnum: integer; value: boolean); overload;
mse1's avatar
mse1 committed
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

procedure setbit1(var dest: byte; bitnum: integer); overload;
procedure setbit1(var dest: word; bitnum: integer); overload;
procedure setbit1(var dest: longword; bitnum: integer); overload;
function setbit(const source: byte; bitnum: integer): byte; overload;
function setbit(const source: word; bitnum: integer): word; overload;
function setbit(const source: longword; bitnum: integer): longword; overload;

procedure clearbit1(var dest: byte; bitnum: integer); overload;
procedure clearbit1(var dest: word; bitnum: integer); overload;
procedure clearbit1(var dest: longword; bitnum: integer); overload;
function clearbit(const source: byte; bitnum: integer): byte; overload;
function clearbit(const source: word; bitnum: integer): word; overload;
function clearbit(const source: longword; bitnum: integer): longword; overload;

procedure togglebit1(var dest: byte; bitnum: integer); overload;
procedure togglebit1(var dest: word; bitnum: integer); overload;
procedure togglebit1(var dest: longword; bitnum: integer); overload;
function togglebit(const source: byte; bitnum: integer): byte; overload;
function togglebit(const source: word; bitnum: integer): word; overload;
function togglebit(const source: longword; bitnum: integer): longword; overload;

function iszero(address: pointer; count: integer): boolean;

function swapbytes(const value: word): word; overload;
 //value and result at different adresses!
mse1's avatar
mse1 committed
157
function swapbytes(const value: longword): longword; overload;
mse1's avatar
mse1 committed
158 159
 //value and result at different adresses!
procedure swapbytes1(var value: word); overload;
mse1's avatar
mse1 committed
160
procedure swapbytes1(var value: longword); overload;
Martin Schreiber's avatar
Martin Schreiber committed
161 162 163 164 165 166 167 168

function swapbits(const value: word): word; overload;
 //value and result at different adresses!
function swapbits(const value: longword): longword; overload;
 //value and result at different adresses!
procedure swapbits1(var value: word); overload;
procedure swapbits1(var value: longword); overload;

mse1's avatar
mse1 committed
169 170
procedure swaprgb1(var value: longword);
function swaprgb(const value: longword): longword;
mse1's avatar
mse1 committed
171

172
function roundint(const value: integer; const step: integer): integer;
mse1's avatar
mse1 committed
173 174 175 176 177 178
procedure scaleexp101(var value: int64; const exp: integer); overload;
procedure scaleexp101(var value: integer; const exp: integer); overload;
procedure scaleexp101(var value: currency; const exp: integer); overload;
function scaleexp10(const value: int64; const exp: integer): int64; overload;
function scaleexp10(const value: integer; const exp: integer): integer; overload;
function scaleexp10(const value: currency; const exp: integer): currency; overload;
mse1's avatar
mse1 committed
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

implementation

function updatebit(var dest: byte; bitnum: integer; value: boolean): boolean;
var
 by1: byte;
begin
 by1:= dest;
 if value then begin
  dest:= dest or bits[bitnum];
 end
 else begin
  dest:= dest and not bits[bitnum];
 end;
 result:= dest <> by1;
end;

function updatebit(var dest: word; bitnum: integer; value: boolean): boolean;
var
 wo1: word;
begin
 wo1:= dest;
 if value then begin
  dest:= dest or bits[bitnum];
 end
 else begin
  dest:= dest and not bits[bitnum];
 end;
 result:= dest <> wo1;
end;

function updatebit(var dest: longword; bitnum: integer; value: boolean): boolean;
var
 wo1: longword;
begin
 wo1:= dest;
 if value then begin
  dest:= dest or bits[bitnum];
 end
 else begin
  dest:= dest and not bits[bitnum];
 end;
 result:= dest <> wo1;
end;

224
procedure updatebit1(var dest: byte; bitnum: integer; value: boolean);
mse1's avatar
mse1 committed
225 226
//var
// by1: byte;
227
begin
mse1's avatar
mse1 committed
228
// by1:= dest;
229 230 231 232 233 234 235 236 237
 if value then begin
  dest:= dest or bits[bitnum];
 end
 else begin
  dest:= dest and not bits[bitnum];
 end;
end;

procedure updatebit1(var dest: word; bitnum: integer; value: boolean);
mse1's avatar
mse1 committed
238 239
//var
// wo1: word;
240
begin
mse1's avatar
mse1 committed
241
// wo1:= dest;
242 243 244 245 246 247 248 249 250
 if value then begin
  dest:= dest or bits[bitnum];
 end
 else begin
  dest:= dest and not bits[bitnum];
 end;
end;

procedure updatebit1(var dest: longword; bitnum: integer; value: boolean);
mse1's avatar
mse1 committed
251 252
//var
// wo1: longword;
253
begin
mse1's avatar
mse1 committed
254
// wo1:= dest;
255 256 257 258 259 260 261 262
 if value then begin
  dest:= dest or bits[bitnum];
 end
 else begin
  dest:= dest and not bits[bitnum];
 end;
end;

mse1's avatar
mse1 committed
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
procedure setbit1(var dest: byte; bitnum: integer);
begin
 dest:= dest or bits[bitnum];
end;

procedure setbit1(var dest: word; bitnum: integer);
begin
 dest:= dest or bits[bitnum];
end;

procedure setbit1(var dest: longword; bitnum: integer);
begin
 dest:= dest or bits[bitnum];
end;

procedure clearbit1(var dest: byte; bitnum: integer);
begin
 dest:= dest and not bits[bitnum];
end;

procedure clearbit1(var dest: word; bitnum: integer);
begin
 dest:= dest and not bits[bitnum];
end;

procedure clearbit1(var dest: longword; bitnum: integer);
begin
 dest:= dest and not bits[bitnum];
end;

procedure togglebit1(var dest: byte; bitnum: integer);
begin
 dest:= dest xor byte(bits[bitnum]);
end;

procedure togglebit1(var dest: word; bitnum: integer);
begin
 dest:= dest xor word(bits[bitnum]);
end;

procedure togglebit1(var dest: longword; bitnum: integer);
begin
 dest:= dest xor bits[bitnum];
end;

function setbit(const source: byte; bitnum: integer): byte;
begin
 result:= source or bits[bitnum];
end;

function setbit(const source: word; bitnum: integer): word;
begin
 result:= source or bits[bitnum];
end;

function setbit(const source: longword; bitnum: integer): longword;
begin
 result:= source or bits[bitnum];
end;

function clearbit(const source: byte; bitnum: integer): byte;
begin
 result:= source and not bits[bitnum];
end;

function clearbit(const source: word; bitnum: integer): word;
begin
 result:= source and not bits[bitnum];
end;

function clearbit(const source: longword; bitnum: integer): longword;
begin
 result:= source and not bits[bitnum];
end;

function togglebit(const source: byte; bitnum: integer): byte;
begin
 result:= source xor byte(bits[bitnum]);
end;

function togglebit(const source: word; bitnum: integer): word;
begin
 result:= source xor word(bits[bitnum]);
end;

function togglebit(const source: longword; bitnum: integer): longword;
begin
 result:= source xor bits[bitnum];
end;

function replacebits(const new,old,mask: byte): byte;
begin
 result:= old and not mask or new and mask;
end;

function replacebits(const new,old,mask: word): word;
begin
 result:= old and not mask or new and mask;
end;

mse1's avatar
mse1 committed
363
function replacebits(const new,old,mask: longword): longword;
mse1's avatar
mse1 committed
364 365 366 367
begin
 result:= old and not mask or new and mask;
end;

368
function replacebits1(var dest: byte; const value,mask: byte): boolean;
mse1's avatar
mse1 committed
369
begin
370 371 372 373
 result:= (dest xor value) and mask <> 0;
 if result then begin
  dest:= dest and not mask or value and mask
 end;
mse1's avatar
mse1 committed
374 375
end;

376 377
function replacebits1(var dest: word;
                              const value,mask: word): boolean; overload;
mse1's avatar
mse1 committed
378
begin
379 380 381 382
 result:= (dest xor value) and mask <> 0;
 if result then begin
  dest:= dest and not mask or value and mask
 end;
mse1's avatar
mse1 committed
383 384
end;

385 386
function replacebits1(var dest: longword;
                              const value,mask: longword): boolean; overload;
mse1's avatar
mse1 committed
387
begin
388 389 390 391
 result:= (dest xor value) and mask <> 0;
 if result then begin
  dest:= dest and not mask or value and mask
 end;
mse1's avatar
mse1 committed
392 393 394 395 396 397 398 399 400 401 402 403
end;

function bitschanged(const a,b,mask: byte): boolean; overload;
begin
 result:= (a xor b) and mask <> 0;
end;

function bitschanged(const a,b,mask: word): boolean; overload;
begin
 result:= (a xor b) and mask <> 0;
end;

mse1's avatar
mse1 committed
404
function bitschanged(const a,b,mask: longword): boolean; overload;
mse1's avatar
mse1 committed
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
begin
 result:= (a xor b) and mask <> 0;
end;

function setsinglebit(const new,old,mask: byte): byte; overload;
var
 v1: byte;
begin
 if new and mask = 0 then begin
  result:= new and not mask;
 end
 else begin
  v1:= (old xor new) and mask;
  if v1 = 0 then begin
   result:= new; //no change
  end
  else begin
   result:= (new and  not mask) or
            bits[lowestbit(v1 and new)];
  end;
 end;
end;

function setsinglebit(const new,old,mask: word): word; overload;
var
 v1: word;
begin
 if new and mask = 0 then begin
  result:= new and not mask;
 end
 else begin
  v1:= (old xor new) and mask;
  if v1 = 0 then begin
   result:= new; //no change
  end
  else begin
   result:= (new and  not mask) or
            bits[lowestbit(v1 and new)];
  end;
 end;
end;

mse1's avatar
mse1 committed
447
function setsinglebit(const new,old,mask: longword): longword; overload;
mse1's avatar
mse1 committed
448
var
mse1's avatar
mse1 committed
449
 v1: longword;
mse1's avatar
mse1 committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
begin
 if new and mask = 0 then begin
  result:= new and not mask;
 end
 else begin
  v1:= (old xor new) and mask;
  if v1 = 0 then begin
   result:= new; //no change
  end
  else begin
   result:= (new and  not mask) or
            bits[lowestbit(v1 and new)];
  end;
 end;
end;

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
function setsinglebit(const new,old: byte;
                            const mask: array of byte): byte;
var
 int1: integer;
begin
 result:= new;
 for int1:= 0 to high(mask) do begin
  result:= setsinglebit(result,old,mask[int1]);
 end;
end;

function setsinglebit(const new,old: word;
                            const mask: array of word): word;
var
 int1: integer;
begin
 result:= new;
 for int1:= 0 to high(mask) do begin
  result:= setsinglebit(result,old,mask[int1]);
 end;
end;
487
{$ifndef FPC}
488 489 490 491 492 493 494 495 496 497
function setsinglebitar8(const new,old: byte;
                            const mask: array of byte): byte;
var
 int1: integer;
begin
 result:= new;
 for int1:= 0 to high(mask) do begin
  result:= setsinglebit(result,old,mask[int1]);
 end;
end;
498 499 500 501 502 503 504 505 506 507
function setsinglebitar16(const new,old: word;
                            const mask: array of word): word;
var
 int1: integer;
begin
 result:= new;
 for int1:= 0 to high(mask) do begin
  result:= setsinglebit(result,old,mask[int1]);
 end;
end;
508

509 510 511 512 513 514 515 516 517 518 519
function setsinglebitar32(const new,old: longword;
                            const mask: array of longword): longword;
var
 int1: integer;
begin
 result:= new;
 for int1:= 0 to high(mask) do begin
  result:= setsinglebit(result,old,mask[int1]);
 end;
end;
{$endif}
520 521 522 523 524 525 526 527 528 529 530
function setsinglebit(const new,old: longword;
                            const mask: array of longword): longword;
var
 int1: integer;
begin
 result:= new;
 for int1:= 0 to high(mask) do begin
  result:= setsinglebit(result,old,mask[int1]);
 end;
end;

mse1's avatar
mse1 committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
function checkbit(const value: byte; const bitnum: integer): boolean; overload;
begin
 result:= value and bits[bitnum] <> 0;
end;

function checkbit(const value: word; const bitnum: integer): boolean; overload;
begin
 result:= value and bits[bitnum] <> 0;
end;

function checkbit(const value: longword; const bitnum: integer): boolean; overload;
begin
 result:= value and bits[bitnum] <> 0;
end;



548 549 550
function roundint(const value: integer; const step: integer): integer;
var
 int1: integer;
mse1's avatar
mse1 committed
551
begin
552 553 554 555 556
 int1:= step div 2;
 if value < 0 then begin
  int1:= -int1;
 end;
 result:= ((value + int1) div step) * step;
mse1's avatar
mse1 committed
557
end;
558

mse1's avatar
mse1 committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
function scaleexp10(const value: int64; const exp: integer): int64;
begin
 if exp < 0 then begin
  result:= value div int64exp10ar[-exp];
 end
 else begin
  result:= value * int64exp10ar[exp];
 end;
end;

function scaleexp10(const value: integer; const exp: integer): integer;
begin
 if exp < 0 then begin
  result:= value div intexp10ar[-exp];
 end
 else begin
  result:= value * intexp10ar[exp];
 end;
end;

function scaleexp10(const value: currency; const exp: integer): currency;
begin
mse1's avatar
mse1 committed
581 582 583 584 585 586 587 588
//{$ifdef FPC}
// if exp < 0 then begin
//  int64(result):= int64(value) div int64exp10ar[-exp];
// end
// else begin
//  int64(result):= int64(value) * int64exp10ar[exp];
// end;
//{$else}
mse1's avatar
mse1 committed
589 590 591 592 593 594
 if exp < 0 then begin
  pint64(@result)^:= pint64(@value)^ div int64exp10ar[-exp];
 end
 else begin
  pint64(@result)^:= pint64(@value)^ * int64exp10ar[exp];
 end;
mse1's avatar
mse1 committed
595
//{$endif}
mse1's avatar
mse1 committed
596 597 598
end;

procedure scaleexp101(var value: int64; const exp: integer);
599 600 601 602 603 604 605 606 607
begin
 if exp < 0 then begin
  value:= value div int64exp10ar[-exp];
 end
 else begin
  value:= value * int64exp10ar[exp];
 end;
end;

mse1's avatar
mse1 committed
608
procedure scaleexp101(var value: integer; const exp: integer);
609 610 611 612 613 614 615 616 617
begin
 if exp < 0 then begin
  value:= value div intexp10ar[-exp];
 end
 else begin
  value:= value * intexp10ar[exp];
 end;
end;

mse1's avatar
mse1 committed
618
procedure scaleexp101(var value: currency; const exp: integer);
619
begin
mse1's avatar
mse1 committed
620 621 622 623 624 625 626 627
//{$ifdef FPC}
// if exp < 0 then begin
//  int64(value):= int64(value) div int64exp10ar[-exp];
// end
// else begin
//  int64(value):= int64(value) * int64exp10ar[exp];
// end;
//{$else}
mse1's avatar
mse1 committed
628 629 630 631 632 633
 if exp < 0 then begin
  pint64(@value)^:= pint64(@value)^ div int64exp10ar[-exp];
 end
 else begin
  pint64(@value)^:= pint64(@value)^ * int64exp10ar[exp];
 end;
mse1's avatar
mse1 committed
634
//{$endif}
635
end;
mse1's avatar
mse1 committed
636

mse1's avatar
mse1 committed
637
procedure swaprgb1(var value: longword);
mse1's avatar
mse1 committed
638 639 640 641 642 643 644 645
var
 by1: byte;
begin
 by1:= byte(value);
 pchar(@value)^:= (pchar(@value)+2)^;
 byte((pchar(@value)+2)^):= by1;
end;

mse1's avatar
mse1 committed
646
function swaprgb(const value: longword): longword;
mse1's avatar
mse1 committed
647 648 649 650 651 652
begin
 result:= value;
 pchar(@result)^:= (pchar(@value)+2)^;
 byte((pchar(@result)+2)^):= byte(value);
end;

Martin Schreiber's avatar
Martin Schreiber committed
653 654
//todo: optimize

mse1's avatar
mse1 committed
655 656 657 658 659 660 661 662 663
procedure swapbytes1(var value: word); overload;
var
 wo1: word;
begin
 wo1:= value;
 (pchar(@value))^:= (pchar(@wo1)+1)^;
 (pchar(@value)+1)^:= (pchar(@wo1))^;
end;

mse1's avatar
mse1 committed
664
procedure swapbytes1(var value: longword); overload;
mse1's avatar
mse1 committed
665
var
mse1's avatar
mse1 committed
666
 ca1: longword;
mse1's avatar
mse1 committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
 po1,po2: pchar;
begin
 ca1:= value;
 po1:= @value;
 po2:= pchar(@ca1)+3;
 po1^:= po2^; inc(po1); dec(po2);
 po1^:= po2^; inc(po1); dec(po2);
 po1^:= po2^; inc(po1); dec(po2);
 po1^:= po2^;
end;

function swapbytes(const value: word): word; overload;
begin
 (pchar(@result))^:= (pchar(@value)+1)^;
 (pchar(@result)+1)^:= (pchar(@value))^;
end;

mse1's avatar
mse1 committed
684
function swapbytes(const value: longword): longword; overload;
mse1's avatar
mse1 committed
685 686 687 688 689 690 691 692 693 694 695
var
 po1,po2: pchar;
begin
 po1:= @result;
 po2:= pchar(@value)+3;
 po1^:= po2^; inc(po1); dec(po2);
 po1^:= po2^; inc(po1); dec(po2);
 po1^:= po2^; inc(po1); dec(po2);
 po1^:= po2^;
end;

Martin Schreiber's avatar
Martin Schreiber committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
function swapbits(const value: word): word; overload;
 //value and result at different adresses!
begin
 pbyte(pchar(@result))^:= bitreverse[pbyte(pchar(@value)+1)^];
 pbyte(pchar(@result)+1)^:= bitreverse[pbyte(pchar(@value))^];
end;

function swapbits(const value: longword): longword; overload;
 //value and result at different adresses!
var
 po1,po2: pbyte;
begin
 po1:= @result;
 po2:= pbyte(pchar(@value)+3);
 po1^:= bitreverse[po2^]; inc(po1); dec(po2);
 po1^:= bitreverse[po2^]; inc(po1); dec(po2);
 po1^:= bitreverse[po2^]; inc(po1); dec(po2);
 po1^:= bitreverse[po2^];
end;

procedure swapbits1(var value: word); overload;
var
 wo1: word;
begin
 wo1:= value;
 value:= swapbits(wo1);
end;

procedure swapbits1(var value: longword); overload;
var
 lwo1: longword;
begin
 lwo1:= value;
 value:= swapbits(lwo1);
end;

mse1's avatar
mse1 committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745
function iszero(address: pointer; count: integer): boolean;
var
 int1: integer;
begin
 result:= false;
 for int1:= 0 to count - 1 do begin
  if pbyte(address)^ <> 0 then begin
   exit;
  end;
  inc(pbyte(address));
 end;
 result:= true;
end;

mse1's avatar
mse1 committed
746
function highestbit(value: longword): integer;
mse1's avatar
mse1 committed
747 748 749 750 751 752 753 754 755
//0-> first, 31-> last($80000000), -1-> none ($00000000)
begin
 result:= -1;
 while value <> 0 do begin
  inc(result);
  value:= value shr 1;
 end;
end;

mse1's avatar
mse1 committed
756
function lowestbit(value: longword): integer;
mse1's avatar
mse1 committed
757 758 759 760 761 762 763 764 765 766 767 768
//0-> first, 31-> last($80000000), -1-> none ($00000000)
begin
 result:= -1;
 if value <> 0 then begin
  result:= 32;
  while value <> 0 do begin
   dec(result);
   value:= value shl 1;
  end;
 end;
end;

mse1's avatar
mse1 committed
769 770 771 772 773 774 775 776 777 778 779 780 781
function nextpowerof2(const avalue: longword): longword;
var
 int1: integer;
begin
 int1:= highestbit(avalue);
 if avalue - bits[int1] <> 0 then begin
  result:= bits[int1+1];
 end
 else begin
  result:= avalue;
 end;
end;

mse1's avatar
mse1 committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
function highestbit64(value: qword): integer;
//0-> first, 63-> last($8000000000000000), -1-> none ($0000000000000000)
begin
 result:= -1;
 while value <> 0 do begin
  inc(result);
  value:= value shr 1;
 end;
end;

function lowestbit64(value: qword): integer;
//0-> first, 63-> last($8000000000000000), -1-> none ($0000000000000000)
begin
 result:= -1;
 if value <> 0 then begin
  result:= 64;
  while value <> 0 do begin
   dec(result);
   value:= value shl 1;
  end;
 end;
end;

mse1's avatar
mse1 committed
805
function getmask(const mask: array of bitnumty): longword;
mse1's avatar
mse1 committed
806 807 808 809 810 811 812 813 814 815
var
 int1: integer;
begin
 result:= 0;
 for int1:= 0 to high(mask) do begin
  result:= result or bits[mask[int1]];
 end;
end;

end.