Nibble reverse (HP-48,49,50g) - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Nibble reverse (HP-48,49,50g) (/thread-247032.html) |
Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-25-2013 FWIW,
NR:
This reverses the bits of a nibble. # 1101b NR --> # 1011b Gerson.
Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-25-2013 \<<
Size ChkSum
This is a direct translation of: def reverse(x): Which prints:
0: 0
Kind regards Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-25-2013 Just noticed that I don't need to mask (x >> 2) with 0x3. Thus the function reverse can be simplified to: def reverse(x):
This makes the program a little shorter: \<<
Size ChkSum
Cheers Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-25-2013 Same size on the 50g (76 bytes), and about five times faster than mine since it uses only binary operations. The difference should be greater on the 48GX. Cheers,
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Paul Dale - 07-25-2013 Somewhat shorter on the 34S:
WSIZE 04 I should have had the bit mirror command take an argument rather than relying on word size :-(
Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-25-2013 Quote:
That's when I became first aware of this command of the WP-34S.
Kind regard Re: Nibble reverse (HP-48,49,50g) - Paul Dale - 07-25-2013 Coding theory uses this operation at times. I believe FFTs can make use of it too.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-25-2013 Unfortunately I need this on the 50g, so I'll use Thomas Klemm's fast code. I intend to write a few binary numbers as a graphic object using the GROB ommand. For instance, 35027 and 65535. The output would be * * ** * ** (1000100011010011b or 88D3h) However GROB 16 2 88D3 returns the nibbles mirrored: Graphic 16 x 2So, the nibbles have to be reversed first before using the GROB command (GROB 16 2 11BC). Another problem is the hex sequence apparently can't be programmed (my program generates the string of hex digits, but I don't now how to make GROB use it -- I copy and paste it to the command line which is rather cumbersome). Gerson. Edited to fix a typo
Edited: 26 July 2013, 11:07 p.m. after one or more responses were posted
Re: Nibble reverse (HP-48,49,50g) - Kiyoshi Akima - 07-26-2013 If you're willing to accept a larger program in return for more speed, you can try something like this:
<< Or smaller (64 bytes) and faster:
<< Re: Nibble reverse (HP-48,49,50g) - cyrille de Brébisson - 07-26-2013 Hello I have not written any SAturn ASM for a while, and I have not tested the code, so it might not be 100% accurate... but this should reverse your nibble relatively fast :-)
CODE
P=C.0 LC 84c2a6e195d3b7f0 P=0 % load appropriate nibble in C.0
Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-26-2013 I was trying to get rid of "65. -" in your 2nd example. But it's not possible to enter CHR 0 directly: Error:What's the reason for this? As far as I understand strings aren't \0 terminated in RPL. So I transformed the program to a string using \->STR, duplicated it and cut each part off where I intended to insert the 0 character. Then I created a string using CHR 0 and added all three in the correct order. With this string I was able to compile the desired program with OBJ\->. Is there a simpler way to do that on the HP-48GX?
Cheers Re: Nibble reverse (HP-48,49,50g) - Software49g - 07-26-2013 > and I have not tested the code
CODE Edited: 26 July 2013, 8:36 a.m.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-26-2013 That's what I was trying to get:
These lines are the first few tens of primes, in binary format. GROB 16 128This will return the graphic object for the first 128 primes. Re: Nibble reverse (HP-48,49,50g) - Kiyoshi Akima - 07-26-2013 I think it's just because someone decided that 0 doesn't belong in UserRPL strings. It's somewhat easier in SysRPL.
It's the little things like this that keep the 50g from being a perfect calculator. Have you ever tried Sigma-LIST on a one-element list?
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-26-2013 I am totally illiterate in Saturn Assembly Language, but it looks like Cyrille's aproach, like Kioshi Akima's, is a look-up table. That's what I had first thought of, but decided to find a conversion formula instead. It turns out that the table approach is much better in this case. Best regards,
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-26-2013 Andreas gave this answer bellow:
Quote: This makes sense as I was able to create the program with a string that contains the NULL character but now I'm not able anymore to edit the program.
Kind regards Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-26-2013 Thanks for the explanation.
Cheers Re: Nibble reverse (HP-48,49,50g) - Software49g - 07-26-2013 Hi Gerson, Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-26-2013 %%HP: T(3)A(R)F(,); 59.5 bytes on both the HP-48GX and 50g. Hex mode assumed, of course. Cheers,
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-26-2013 Thanks, Andreas!
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Edwin - 07-26-2013 it works in any base: %%HP: T(3)A(D)F(,);-Edwin-
Edited: 26 July 2013, 3:48 p.m.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-26-2013 Thanks for your version. As they say, there's more than a way to skin a cat :-) Here are the timings and sizes for what we've got now:
Cyrille's "relatively fast" ASM code is hors-concours :-) (I'm still gettin "asm Error: Invalid File").
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Kiyoshi Akima - 07-26-2013 The SysRPL translation of my second program (without the 65 -) comes out at 38.5 bytes and quite a bit faster than the UserRPL version:
!RPL !NO CODE Re: Nibble reverse (HP-48,49,50g) - Thomas Ritschel - 07-27-2013 Here is another solution adapted from this algorithm:
\<< Tested on a 48SX, but should work on the newer ones too.
Edit: Here is a short explanation of the algorithm:
abcd Edited: 27 July 2013, 12:48 p.m. after one or more responses were posted
Re: Nibble reverse (HP-48,49,50g) - Gilles Carpentier - 07-27-2013 49-50 only :
\<< BIN \->STR TAIL SREV TAIL "#" SWAP + STR\-> \>> Probably not the fastest but 45 bytes only
Development library must me attach (256 ATTACH) Edited: 27 July 2013, 10:28 a.m.
Re: Nibble reverse (HP-48,49,50g) - Gilles Carpentier - 07-27-2013 Quote: Whith your hexa string on the stack try : \<< "GROB w h " SWAP + OBJ\-> \>> replacing w and h by the value of width and high of the picture ex :
\<<
Edited: 27 July 2013, 11:44 a.m.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-27-2013 This is the fastest User RPL program so far. Thanks for your contribution! Your B\->R instruction has been omitted in the byte count in the table.
-----------------------------------+---------------------------------- These are the routines for test and timing:
HP 50g: Corresponding running times for SysRPL and ASM versions, if available, are welcome. Gerson.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-27-2013 Ah, I knew there had to be a way! Thank you very much!
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-27-2013 I would place BIN in the main program and add OBJ\-> in the end. That's still 45 bytes. This does reverse the bits of a binary number, but not the nibble. For instance: # 1b --> # 1 bThis might be useful for some applications, however. Cheers,
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-27-2013 That's a nice solution. Thanks for the helpful explanation of the algorithm!
Cheers Re: Nibble reverse (HP-48,49,50g) - DavidM - 07-27-2013 Using Cyrille's/Andreas' ASM code, I created a code object on the 50g and called it from the small app you identified in your previous post. I wrapped all of that in between two TICKS calls to obtain the timings below for 10 iterations (all values are in seconds):
0.0890 The code object "NR" is 32 bytes in size (CRC #339Ah).
So as long as you don't care about error-checking, this would appear to be a good approach. :-)
Re: Nibble reverse (HP-48,49,50g) - Thomas Klemm - 07-27-2013 The result is a surprise to me as I was assuming that using a lookup-table is faster than translatimg it using elementary operations. This is another proof that you always have to measure.
Cheers Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-27-2013 Thank you!
Quote: Perhaps some three or four-second saving in the running time of the TSTBPRM program below. It takes about 45 seconds using Thomas Klemm's NR (not 15 as stated elsewhere in this thread) and 42 seconds using Thomas Ritschel's. Gerson.
--------------------------------- %%HP: T(3)A(R)F(,);
Edited: 27 July 2013, 7:55 p.m.
Re: Nibble reverse (HP-48,49,50g) - Paul Dale - 07-27-2013 This algorithm would be very suitable for implementation in sysRPL using 20 bit system integers. Quite possibly another speed gain to be had but it still wouldn't compete with assembler.
Re: Nibble reverse (HP-48,49,50g) - DavidM - 07-28-2013 This application was a good mental exercise for me to attempt a SysRPL/ASM version to see how it would turn out (though it felt a bit like cheating at times :-) ). I used the code you posted above as a model and created new versions of BPRM and TSTBPRM. Re: Nibble reverse (HP-48,49,50g) - Robert Prosperi - 07-28-2013 Please do post the SysRPL code David, it would be educational. Following threads like this is very useful for me. Thanks to all that posted dif ideas, and to Gerson for the original post and compiling and posting the stats.
--bob prosperi Edited: 28 July 2013, 4:59 p.m.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-28-2013 Quote: This appears to be more memory-effective. I'll try to use your method next time (in User RPL).
Quote: Perhaps if you had started from scratch, you'd have come out with an even faster version. That was the first time I ever used GROB in a program, so I might have not used the best approach for the task.
Quote: That would be nice. Even though I know neither SyrRPL nor ASM, I could compile it and see how it fares on the 49G and 50g. Best regards,
Gerson.
Re: Nibble reverse (HP-48,49,50g) - DavidM - 07-28-2013 Quote: If I were doing this from scratch, I'd probably pre-allocate the space for the final grob first, then move the pixel data to the appropriate place in the final file while looping. It's a little messier to do that, but I believe there would be a noticeable savings in execution time (especially for larger grobs). Doing it that way would mean there's much less movement of data each time a row is added. As it stands now, there's at least the potential for a new copy of the main grob to be made with each loop iteration. So as it stands now, each row addition slows down as the grob gets larger. I realize now that I made another change, which is to output a 20-bit row instead of 16. It's a minor change, but didn't want that to confuse anyone. The source code follows, exactly as it was compiled by Debug4x. If you want to try this with the built-in assembler (ala menu 256), you'll have to pull it apart into separate pieces. I've attempted to use enough comments to make it readable even if you're not familiar with SysRPL/ASM:
RPL Re: Nibble reverse (HP-48,49,50g) - Han - 07-28-2013 Quote: For a single nibble, why not just do:
\<< # 1111b XOR />>>
For longer words, just use 2^n-1 to get an appropriately large integer to convert into binary form, and again use XOR
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-28-2013 Quote: Han,
that inverts the nibble, not reverse it. For example: # 1101b # 1111b XOR --> # 10b, not # 1011b as we want. Regards,
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-28-2013 Really impressive, about ten times as fast! 4.07006835938As a comparison, the UserRPL version returns 42.118775 Quote:There was a discussion here about the primality (or non-primality) of 1 about three years ago:
Quote: Indeed easier using Debug4. Thanks for the tip!
Gerson.
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-28-2013 Quote: I second that! That's what I like in this forum: For every occasional small contribution, I've always been given many more in return, and much more valuable ones! Thank you for your interest!
Gerson.
Re: Nibble reverse (HP-48,49,50g) - DavidM - 07-28-2013 Yikes! Hope I'm not forced to give up my museum account for that inferred (or is it "implied"?) mathematical infraction. :-)
I've always been in awe of folks who can actually write assembly code on the 49/50 without the use of PC-based display/keyboard/debugging/etc. Their brains are clearly wired differently than mine, hence my need for something like Debug4x. Glad you were able to make use of it!
Re: Nibble reverse (HP-48,49,50g) - Gerson W. Barbosa - 07-28-2013 Thanks to all of you who've commented, contributed algorithms and implementations or just followed this thread! Here is a very concise explanation of my method, just for the record:
Re: Nibble reverse (HP-48,49,50g) - Han - 07-29-2013 I clearly did not read carefully, and jumped the gun in presuming reversing the bits to mean "flip" 0 to 1 and vice versa. Thank you for setting me straight =D
|