start working on bit shifting
This commit is contained in:
		
							
								
								
									
										10
									
								
								config.nims
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								config.nims
									
									
									
									
									
								
							| @@ -1,5 +1,5 @@ | |||||||
| --threads: on | # --threads: on | ||||||
| --d: release | # --d: release | ||||||
| --opt: speed | # --opt: speed | ||||||
| --passC: -flto | # --passC: -flto | ||||||
| --passL: -flto | # --passL: -flto | ||||||
							
								
								
									
										82
									
								
								faststack.nim
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								faststack.nim
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,82 @@ | |||||||
|  | import bitops, strutils | ||||||
|  |  | ||||||
|  |  | ||||||
|  | type | ||||||
|  |   Color = enum | ||||||
|  |     cRed, cGreen, cBlue, cYellow, cPurple | ||||||
|  |  | ||||||
|  |   ColorStack = object | ||||||
|  |     data: uint16 | ||||||
|  |     len: uint8 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | const | ||||||
|  |   masks = [ | ||||||
|  |     0'u16, | ||||||
|  |     7, | ||||||
|  |     63, | ||||||
|  |     511, | ||||||
|  |     4095, | ||||||
|  |   ] | ||||||
|  |  | ||||||
|  |  | ||||||
|  | proc add(s: var ColorStack, c: Color) = | ||||||
|  |   # e.g. if stack is 0b0000000000000100: | ||||||
|  |   # and color is 0b00000011 | ||||||
|  |   # shift: 0b0000000000100000 | ||||||
|  |   # bitor: 0b0000000000100000 and 0b00000011 | ||||||
|  |   # results in 0b0000000000100011 | ||||||
|  |   s.data = (s.data shl 3).bitor(cast[uint8](c)) | ||||||
|  |   echo BiggestInt(s.data).toBin(16) | ||||||
|  |   inc s.len | ||||||
|  |  | ||||||
|  |  | ||||||
|  | proc moveSubstack(src, dst: var ColorStack, nToMove: uint8) = | ||||||
|  |   # shift the dst stack by the length of the substack to make room | ||||||
|  |   let shift = nToMove * 3 | ||||||
|  |   dst.data = dst.data shl shift | ||||||
|  |   # then we mask the source data to present only the items  | ||||||
|  |   # being moved, and AND that with the shifted dst data | ||||||
|  |   dst.data = bitor( | ||||||
|  |     bitand(src.data, masks[src.len]), | ||||||
|  |     dst.data | ||||||
|  |   ) | ||||||
|  |   dst.len += nToMove | ||||||
|  |   # then we shift the source to get rid of the moved items | ||||||
|  |   src.data = src.data shr shift | ||||||
|  |   src.len -= nToMove | ||||||
|  |  | ||||||
|  |  | ||||||
|  | iterator items(s: ColorStack): Color = | ||||||
|  |   for offset in countdown(s.len - 1, 0'u8): | ||||||
|  |     let shifted = s.data shr offset | ||||||
|  |     echo shifted | ||||||
|  |     echo bitand(shifted, masks[0]) | ||||||
|  |     yield Color(bitand(shifted, masks[0])) | ||||||
|  |     # yield s.data shr i * 3 |> bitand(masks[0]) |> Color | ||||||
|  |     # :( | ||||||
|  |  | ||||||
|  |  | ||||||
|  | proc `$`(s: ColorStack): string = | ||||||
|  |   result = "St@[" | ||||||
|  |   for c in s: | ||||||
|  |     if result[^1] != '[': | ||||||
|  |       result.add(", ") | ||||||
|  |     result.add($c) | ||||||
|  |   result.add("]") | ||||||
|  |  | ||||||
|  |  | ||||||
|  | var one: ColorStack | ||||||
|  | one.add(cGreen) | ||||||
|  | one.add(cBlue) | ||||||
|  |  | ||||||
|  | var two: ColorStack | ||||||
|  | two.add(cPurple) | ||||||
|  | two.add(cRed) | ||||||
|  | two.add(cYellow) | ||||||
|  |  | ||||||
|  | echo "one: ", one | ||||||
|  | # echo "two: ", two | ||||||
|  |  | ||||||
|  | # moveSubstack(one, two, 2) | ||||||
|  | # echo two | ||||||
		Reference in New Issue
	
	Block a user