Merge pull request #11 from nineties/add_bitscan

Add bitscan
This commit is contained in:
Koichi NAKAMURA 2021-05-05 23:38:43 +09:00 committed by GitHub
commit dad711718a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 0 deletions

View file

@ -1465,6 +1465,9 @@ decimal
s" Not implemented" exception constant NOT-IMPLEMENTED
: not-implemented NOT-IMPLEMENTED throw ;
s" Not supported" exception constant NOT-SUPPORTED
: not-supported NOT-SUPPORTED throw ;
( 31 bytes )
s" Not reachable here. may be a bug" exception constant NOT-REACHABLE
: not-reachable NOT-REACHABLE throw ;

85
lib/bitscan.fs Normal file
View file

@ -0,0 +1,85 @@
\ planckforth -
\ Copyright (C) 2021 nineties
( === Bit-Scan operations === )
cell 16 > [if]
." Bit-Scan for integers longer than 128bits is not supported"
not-supported
[then]
private{
create bsf-modulo-131-table
-1 , 0 , 1 , 72 , 2 , 46 , 73 , 96 ,
3 , 14 , 47 , 56 , 74 , 18 , 97 , 118 ,
4 , 43 , 15 , 35 , 48 , 38 , 57 , 23 ,
75 , 92 , 19 , 86 , 98 , 51 , 119 , 29 ,
5 , -1 , 44 , 12 , 16 , 41 , 36 , 90 ,
49 , 126 , 39 , 124 , 58 , 60 , 24 , 105 ,
76 , 62 , 93 , 115 , 20 , 26 , 87 , 102 ,
99 , 107 , 52 , 82 , 120 , 78 , 30 , 110 ,
6 , 64 , -1 , 71 , 45 , 95 , 13 , 55 ,
17 , 117 , 42 , 34 , 37 , 22 , 91 , 85 ,
50 , 28 , 127 , 11 , 40 , 89 , 125 , 123 ,
59 , 104 , 61 , 114 , 25 , 101 , 106 , 81 ,
77 , 109 , 63 , 70 , 94 , 54 , 116 , 33 ,
21 , 84 , 27 , 10 , 88 , 122 , 103 , 113 ,
100 , 80 , 108 , 69 , 53 , 32 , 83 , 9 ,
121 , 112 , 79 , 68 , 31 , 8 , 111 , 67 ,
7 , 66 , 65 ,
\ Find the index of the least significant 1 bit
\ If u == 0, returns -1
: bitscan-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, 7 , 7 , 7 ,
7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 ,
\ Find the index of the most significant 1 bit
\ If u == 0, returns -1
: bitscan-reverse ( u -- u )
dup 0xff u< if cells msb-table + @ exit then
8 rshift recurse 8 +
; export
}private
T{ 0 bitscan-forward -> -1 }T
T{ 1 bitscan-forward -> 0 }T
T{ 2 bitscan-forward -> 1 }T
T{ 3 bitscan-forward -> 0 }T
T{ 4 bitscan-forward -> 2 }T
T{ 5 bitscan-forward -> 0 }T
T{ -1 bitscan-forward -> 0 }T
T{ max-int bitscan-forward -> 0 }T
T{ min-int bitscan-forward -> cell 8 * 1- }T
T{ 0 bitscan-reverse -> -1 }T
T{ 1 bitscan-reverse -> 0 }T
T{ 2 bitscan-reverse -> 1 }T
T{ 3 bitscan-reverse -> 1 }T
T{ 4 bitscan-reverse -> 2 }T
T{ -1 bitscan-reverse -> cell 8 * 1- }T
T{ max-int bitscan-reverse -> cell 8 * 2 - }T
T{ min-int bitscan-reverse -> cell 8 * 1- }T

View file

@ -15,6 +15,7 @@ include test/coreexttest.fs
include test/export.fs
include lib/array.fs
include lib/bitscan.fs
report-errors
report-and-exit