From c92e4992ad619d1e60ff942988f748c9e90b4ce0 Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Wed, 5 May 2021 21:07:01 +0900 Subject: [PATCH 1/5] Add not-supported exception --- bootstrap.fs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bootstrap.fs b/bootstrap.fs index e13d5d7..a68faa3 100644 --- a/bootstrap.fs +++ b/bootstrap.fs @@ -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 ; From b67d488a586859d55fb203a58f1b1da391a88976 Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Wed, 5 May 2021 21:07:13 +0900 Subject: [PATCH 2/5] Add lib/bitscan.fs --- lib/bitscan.fs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ runtests.fs | 1 + 2 files changed, 57 insertions(+) create mode 100644 lib/bitscan.fs diff --git a/lib/bitscan.fs b/lib/bitscan.fs new file mode 100644 index 0000000..5261d5f --- /dev/null +++ b/lib/bitscan.fs @@ -0,0 +1,56 @@ +\ 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-forward ( u -- u ) + dup negate and 131 mod cells bsf-modulo-131-table + @ +; export + +create msb-index-table + + +\ Find the index of the most significant 1 bit +\ If u == 0, returns -1 +: bitscan-reverse ( u -- u ) +; 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 diff --git a/runtests.fs b/runtests.fs index 932dc87..4f78a84 100644 --- a/runtests.fs +++ b/runtests.fs @@ -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 From d07e23eb006dbbb83f9c33942a313b7bad3a90f7 Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Wed, 5 May 2021 23:09:26 +0900 Subject: [PATCH 3/5] Add comments to bitscan-forward --- lib/bitscan.fs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/bitscan.fs b/lib/bitscan.fs index 5261d5f..5c2f6bc 100644 --- a/lib/bitscan.fs +++ b/lib/bitscan.fs @@ -32,7 +32,9 @@ create bsf-modulo-131-table \ Find the index of the least significant 1 bit \ If u == 0, returns -1 : bitscan-forward ( u -- u ) - dup negate and 131 mod cells bsf-modulo-131-table + @ + dup negate and \ LS1B isolation + 131 mod \ perfect hashing + cells bsf-modulo-131-table + @ ; export create msb-index-table From 4f0380dafa4748ad12ebf39b5dc132eae6823c60 Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Wed, 5 May 2021 23:09:48 +0900 Subject: [PATCH 4/5] WIP: bitscan reverse --- lib/bitscan.fs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/bitscan.fs b/lib/bitscan.fs index 5c2f6bc..47d13e0 100644 --- a/lib/bitscan.fs +++ b/lib/bitscan.fs @@ -37,12 +37,29 @@ create bsf-modulo-131-table cells bsf-modulo-131-table + @ ; export -create msb-index-table +create msb-table + -1 , 0 , 1 , 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , + 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , + 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , + 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , + 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , + 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , + 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , + 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , + 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , + 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , + 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , + 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , + 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , + 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , + 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 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 ; export }private @@ -56,3 +73,12 @@ 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 From 1473176c3ee73e6c91a54f0587cc06efa4c69cd6 Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Wed, 5 May 2021 23:19:02 +0900 Subject: [PATCH 5/5] Passed tests of bitscan-reverse --- lib/bitscan.fs | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/bitscan.fs b/lib/bitscan.fs index 47d13e0..e51ba64 100644 --- a/lib/bitscan.fs +++ b/lib/bitscan.fs @@ -60,6 +60,7 @@ create msb-table \ If u == 0, returns -1 : bitscan-reverse ( u -- u ) dup 0xff u< if cells msb-table + @ exit then + 8 rshift recurse 8 + ; export }private