digitalmars.D.learn - signed nibble
- Michelle Long (3/3) Jan 07 2019 Is there any direct way to convert a signed nibble in to a signed
- Patrick Schluter (2/6) Jan 07 2019 byte b = nibble | ((nibble & 0x40)?0xF0:0);
- Adam D. Ruppe (2/3) Jan 07 2019 don't you mean & 0x80 ?
- Patrick Schluter (4/8) Jan 07 2019 He asked for signed nybble. So mine is wrong and yours also :-)
- H. S. Teoh (9/15) Jan 07 2019 This is equivalent to doing a bit comparison (implied by the ?
- Patrick Schluter (3/17) Jan 07 2019 Yeah, my bit-fiddle-fu goes back to pre-barrel-shifter days. Up
- H. S. Teoh (7/23) Jan 07 2019 Really? Haha, never knew that, even though I date all the way back to
- Patrick Schluter (5/27) Jan 07 2019 Most of my career was programming for 80186. Shifting by one was
- H. S. Teoh (9/22) Jan 07 2019 I used to hack 6502 assembly code. During the PC revolution I wrote an
- Patrick Schluter (8/28) Jan 08 2019 Yeah, that's also what I started with, on the Apple II in the
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (13/18) Jan 08 2019 Heh, I remember they had a friday-night trivia contest at the
- Patrick Schluter (3/17) Jan 08 2019 2-7 cycles ;-)
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (6/11) Jan 08 2019 There you go, the last one in that series I touched was 6800 in
- H. S. Teoh (14/17) Jan 07 2019 Assuming you have the nibble stored in the lower bits of a ubyte:
Is there any direct way to convert a signed nibble in to a signed byte with the same absolute value? Obviously I can do some bit comparisons but just curious if there is a very quick way.
Jan 07 2019
On Monday, 7 January 2019 at 17:23:19 UTC, Michelle Long wrote:Is there any direct way to convert a signed nibble in to a signed byte with the same absolute value? Obviously I can do some bit comparisons but just curious if there is a very quick way.byte b = nibble | ((nibble & 0x40)?0xF0:0);
Jan 07 2019
On Monday, 7 January 2019 at 18:42:13 UTC, Patrick Schluter wrote:byte b = nibble | ((nibble & 0x40)?0xF0:0);don't you mean & 0x80 ?
Jan 07 2019
On Monday, 7 January 2019 at 18:47:04 UTC, Adam D. Ruppe wrote:On Monday, 7 January 2019 at 18:42:13 UTC, Patrick Schluter wrote:He asked for signed nybble. So mine is wrong and yours also :-) It's obviously 0x08 for the highest bit of the low nybble. byte b = nibble | ((nibble & 0x08)?0xF0:0);byte b = nibble | ((nibble & 0x40)?0xF0:0);don't you mean & 0x80 ?
Jan 07 2019
On Mon, Jan 07, 2019 at 06:42:13PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:On Monday, 7 January 2019 at 17:23:19 UTC, Michelle Long wrote:This is equivalent to doing a bit comparison (implied by the ? operator). You can do it without a branch: cast(byte)(nibble << 4) >> 4 will use the natural sign extension of a (signed) byte to "stretch" the upper bit. It just takes 2-3 CPU instructions. T -- Written on the window of a clothing store: No shirt, no shoes, no service.Is there any direct way to convert a signed nibble in to a signed byte with the same absolute value? Obviously I can do some bit comparisons but just curious if there is a very quick way.byte b = nibble | ((nibble & 0x40)?0xF0:0);
Jan 07 2019
On Monday, 7 January 2019 at 18:56:17 UTC, H. S. Teoh wrote:On Mon, Jan 07, 2019 at 06:42:13PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:Yeah, my bit-fiddle-fu goes back to pre-barrel-shifter days. Up to 32 bit processors, shifting was more expensive than branching.On Monday, 7 January 2019 at 17:23:19 UTC, Michelle Long wrote:This is equivalent to doing a bit comparison (implied by the ? operator). You can do it without a branch: cast(byte)(nibble << 4) >> 4 will use the natural sign extension of a (signed) byte to "stretch" the upper bit. It just takes 2-3 CPU instructions.Is there any direct way to convert a signed nibble in to a signed byte with the same absolute value? Obviously I can do some bit comparisons but just curious if there is a very quick way.byte b = nibble | ((nibble & 0x40)?0xF0:0);
Jan 07 2019
On Mon, Jan 07, 2019 at 08:06:17PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:On Monday, 7 January 2019 at 18:56:17 UTC, H. S. Teoh wrote:[...]On Mon, Jan 07, 2019 at 06:42:13PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:Really? Haha, never knew that, even though I date all the way back to writing assembly on 8-bit processors. :-D T -- One reason that few people are aware there are programs running the internet is that they never crash in any significant way: the free software underlying the internet is reliable to the point of invisibility. -- Glyn Moody, from the article "Giving it all away"Yeah, my bit-fiddle-fu goes back to pre-barrel-shifter days. Up to 32 bit processors, shifting was more expensive than branching.byte b = nibble | ((nibble & 0x40)?0xF0:0);This is equivalent to doing a bit comparison (implied by the ? operator). You can do it without a branch: cast(byte)(nibble << 4) >> 4 will use the natural sign extension of a (signed) byte to "stretch" the upper bit. It just takes 2-3 CPU instructions.
Jan 07 2019
On Monday, 7 January 2019 at 20:28:21 UTC, H. S. Teoh wrote:On Mon, Jan 07, 2019 at 08:06:17PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:Most of my career was programming for 80186. Shifting by one was 2 cycles in register and 15 in memory. Shifting by 4, 9 cycles for regs/21 for mem. And 80186 was a fast shifter compared to 8088/86 or 68000 (8+2n cycles).On Monday, 7 January 2019 at 18:56:17 UTC, H. S. Teoh wrote:[...]On Mon, Jan 07, 2019 at 06:42:13PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:Really? Haha, never knew that, even though I date all the way back to writing assembly on 8-bit processors. :-DYeah, my bit-fiddle-fu goes back to pre-barrel-shifter days. Up to 32 bit processors, shifting was more expensive than branching.byte b = nibble | ((nibble & 0x40)?0xF0:0);This is equivalent to doing a bit comparison (implied by the ? operator). You can do it without a branch: cast(byte)(nibble << 4) >> 4 will use the natural sign extension of a (signed) byte to "stretch" the upper bit. It just takes 2-3 CPU instructions.
Jan 07 2019
On Mon, Jan 07, 2019 at 08:41:32PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:On Monday, 7 January 2019 at 20:28:21 UTC, H. S. Teoh wrote:[...]On Mon, Jan 07, 2019 at 08:06:17PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:I used to hack 6502 assembly code. During the PC revolution I wrote an entire application in 8088 assembly. Used to know many of the opcodes and cycle counts by heart like you do, but it's all but a faint memory now. T -- Right now I'm having amnesia and deja vu at the same time. I think I've forgotten this before.Most of my career was programming for 80186. Shifting by one was 2 cycles in register and 15 in memory. Shifting by 4, 9 cycles for regs/21 for mem. And 80186 was a fast shifter compared to 8088/86 or 68000 (8+2n cycles).Up to 32 bit processors, shifting was more expensive than branching.Really? Haha, never knew that, even though I date all the way back to writing assembly on 8-bit processors. :-D
Jan 07 2019
On Monday, 7 January 2019 at 21:46:21 UTC, H. S. Teoh wrote:On Mon, Jan 07, 2019 at 08:41:32PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:Yeah, that's also what I started with, on the Apple II in the early 80s. I was quite surprized that my 6502 knowledge came in very handy when we worked on dial-in modems in the late 90s as the Rockwell modems all used 6502 derived micro-controllers for them.On Monday, 7 January 2019 at 20:28:21 UTC, H. S. Teoh wrote:[...]On Mon, Jan 07, 2019 at 08:06:17PM +0000, Patrick Schluter via Digitalmars-d-learn wrote:I used to hack 6502 assembly code.Most of my career was programming for 80186. Shifting by one was 2 cycles in register and 15 in memory. Shifting by 4, 9 cycles for regs/21 for mem. And 80186 was a fast shifter compared to 8088/86 or 68000 (8+2n cycles).Up to 32 bit processors, shifting was more expensive than branching.Really? Haha, never knew that, even though I date all the way back to writing assembly on 8-bit processors. :-DDuring the PC revolution I wrote an entire application in 8088 assembly. Used to know many of the opcodes and cycle counts by heart like you do, but it's all but a faint memory now.I had to lookup the exact cycle counts ;-) . I remember the relative costs, more or less, but not the details anymore.
Jan 08 2019
On Tuesday, 8 January 2019 at 09:30:14 UTC, Patrick Schluter wrote:Heh, I remember they had a friday-night trivia contest at the mid-90s students pub (for natural sciences) where one of the questions was the opcode for 6502 LDA (or was it NOP?), and I believe I got it right. The opcode for NOP is burned into my memory as $EA was used for erasing code during debugging in a monitor. And it was also the letters for the big game company Electronic Arts... The cycle counts for 6502 are pretty easy though as they tend to be related to the addressing mode and most of them are in the range 1-5... No instruction for multiplication or division... Oh the fun...During the PC revolution I wrote an entire application in 8088 assembly. Used to know many of the opcodes and cycle counts by heart like you do, but it's all but a faint memory now.I had to lookup the exact cycle counts ;-) . I remember the relative costs, more or less, but not the details anymore.
Jan 08 2019
On Tuesday, 8 January 2019 at 10:32:25 UTC, Ola Fosheim Grøstad wrote:On Tuesday, 8 January 2019 at 09:30:14 UTC, Patrick Schluter wrote:2-7 cycles ;-)[...]Heh, I remember they had a friday-night trivia contest at the mid-90s students pub (for natural sciences) where one of the questions was the opcode for 6502 LDA (or was it NOP?), and I believe I got it right. The opcode for NOP is burned into my memory as $EA was used for erasing code during debugging in a monitor. And it was also the letters for the big game company Electronic Arts... The cycle counts for 6502 are pretty easy though as they tend to be related to the addressing mode and most of them are in the range 1-5... No instruction for multiplication or division... Oh the fun...
Jan 08 2019
On Tuesday, 8 January 2019 at 10:55:59 UTC, Patrick Schluter wrote:There you go, the last one in that series I touched was 6800 in 1990. But it kinda make sense, the instructions were not pipelined, so even NOP takes 2 cycles... Another world really.The cycle counts for 6502 are pretty easy though as they tend to be related to the addressing mode and most of them are in the range 1-5... No instruction for multiplication or division... Oh the fun...2-7 cycles ;-)
Jan 08 2019
On Mon, Jan 07, 2019 at 05:23:19PM +0000, Michelle Long via Digitalmars-d-learn wrote:Is there any direct way to convert a signed nibble in to a signed byte with the same absolute value? Obviously I can do some bit comparisons but just curious if there is a very quick way.Assuming you have the nibble stored in the lower bits of a ubyte: import std.stdio; byte nibSgnExt(ubyte nib) { return cast(byte)(nib << 4) >> 4; } void main() { writefln("%02X", nibSgnExt(0x0F)); writefln("%02X", nibSgnExt(0x07)); } T -- If it breaks, you get to keep both pieces. -- Software disclaimer notice
Jan 07 2019