32 bit integer logic in Viper? #11259
Replies: 4 comments 9 replies
-
@micropython.viper
def inv32_a(dest:ptr32, source:ptr32, length:int):
n: int = 0
z: int = -1
for n in range(length):
dest[n] = source[n] ^ z
@micropython.viper
def inv32_b(dest:ptr32, source:ptr32, length:int):
n: int = length-1
z: int = -1
while n >= 0:
dest[n] = source[n] ^ z
n -= 1
N = 2000
src = bytearray(0xe7 for _ in range(N))
for b in src[0:8]:
print(f'{b:08b}', end=' ')
print()
dst = bytearray(0 for _ in range(N))
t0 = time.ticks_us()
inv32_a(dst, src, N>>2)
t1 = time.ticks_us()
for b in dst[0:8]:
print(f'{b:08b}', end=' ')
print(f'\n{time.ticks_diff(t1,t0)}')
dst = bytearray(0 for _ in range(N))
t0 = time.ticks_us()
inv32_b(dst, src, N>>2)
t1 = time.ticks_us()
for b in dst[0:8]:
print(f'{b:08b}', end=' ')
print(f'\n{time.ticks_diff(t1,t0)}') Give those a try… |
Beta Was this translation helpful? Give feedback.
-
Thank you. I can't use the PIO as the code is meant to be cross-platform. But I would like to know why my solution fails: @micropython.viper
def foo(dest:ptr32, source:ptr32, length:int):
d :uint32 = 0xFFFFFFFF
for n in range(length):
c: uint32 = source[n]
dest[n] = c ^ d # ViperTypeError: can't do binary op between 'int' and 'object' Applying elements of your working code one by one, the line that fixes it is here: @micropython.viper
def foo(dest:ptr32, source:ptr32, length:int):
d : int = -1 # This fixes it. Also works if I declare uint32 but setting a uint negative is ugly
for n in range(length):
c: uint32 = source[n]
dest[n] = c ^ d This fact that failure occurs when I use d :uint32 = 0xFFFFFFFF creates a long int in the teeth of the declaration. Any thoughts? Bug? |
Beta Was this translation helpful? Give feedback.
-
@GitHubsSilverBullet @peterhinch @GuillaumeLeclerc
I gratefully used and modified @GitHubsSilverBullet s code.
The assembler is 4 times faster than the fastest viper function. |
Beta Was this translation helpful? Give feedback.
-
Nice! I didn't know there was an invert modifier that's good to know!
…On Sat, Apr 15, 2023, 1:48 PM rkompass ***@***.***> wrote:
By mov(x, invert(x)) or mov(osr, invert(isr)).
The docs are here: https://docs.micropython.org/en/latest/library/rp2.html,
which were created by Peter iirc.
—
Reply to this email directly, view it on GitHub
<#11259 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAPMOGZE7744ATTYWMMBFLDXBLNNXANCNFSM6AAAAAAW6LSZNY>
.
You are receiving this because you commented.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is of academic interest as I've achieved adequate speed, but I keep wondering if improvement is possible.
The requirement is to move data from one bytearray to another, performing inversion (1's complement) in the process. The first go was
followed by:
Extension to 32 bits founders because unsigned integers >0x3FFFFFFF become long ints. Any clever fixes (aside from C/asm)?
Beta Was this translation helpful? Give feedback.
All reactions