int128

User contributed sources that have become inactive, deprecated, or generally unusable. But ... we don't really want to throw them away either.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: int128

Post by srvaldez »

you need to include int128.bi, I corrected the code posted above and now it should work.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: int128

Post by deltarho[1859] »

You didn't publish int128.bi. I removed the usage example from c128.bas and then saved that to int128.bi. All is well now.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: int128

Post by MrSwiss »

deltarho[1859] wrote:You didn't publish int128.bi.
Not true, srvaldez did: you just have to go back, to page 1 (to find it).
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: int128

Post by deltarho[1859] »

I stand corrected but it is not in the zip of the opening post and being pedantic I am inclined to think that it should have been.

c128.bas should have been:
#include "int128.bi" followed by the usage example.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: int128

Post by srvaldez »

you are right deltarho[1859], however it's easier to make updates to the int128.bi here instead of uploading the whole package.
but I did as you suggested.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: int128

Post by deltarho[1859] »

however it's easier to make updates to the int128.bi here instead of uploading the whole package.
Agreed.<wink>
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: int128

Post by srvaldez »

division by a constant using magic numbers https://cboard.cprogramming.com/c-progr ... mbers.html
compile with -O0 , that is zero optimization or the timing loops will be short-circuited.
there's no practical reason since it's not faster than division but it may be amusing.

Code: Select all

/'
https://cboard.cprogramming.com/c-programming/128545-division-ten-magic-numbers.html

In case anyone wants more information on how I've performed divisions using multiply and shift. The theory is that instead of doing answer = x / y you are doing answer = ((x << n) / y) >> n (the shifts logically cancel out). Rewritting the shift as a multiply by a power of two answer = (x * 2^n / y) >> n and all I'm doing is pre-calculating 2^n / y. From there it's simply a matter of picking a large enough n whilst avoiding overflow, and making sure you round up.
Take this (num2*5243)>>19 it performs a divide by 100 for any number up to 10000...

To pick those magic numbers I first picked a power of two that was large enough that when divided by the divisor it was at least half as big as the number I want to support and that when multiplied by answer, doesn't exceed 2^32
2^19 = 524288
divide by divisor -> 524288 / 100 = 5242.88 -> 5243 (rounding up) (at least half as big as 10000 - check)
multiplied by quotient -> 5243 * 10000 = 52430000 (less than 2^32 - check)

I could instead have picked 2^20 giving (num2*10486)>>20
2^20 = 1048576
divide by divisor -> 1048576/ 100 = 10485.76 -> 10486 (rounding up) (at least half as big as 10000 - check)
multiplied by quotient -> 10486 * 10000 = 104860000 (less than 2^32 - check)
'/ 

/' from the above I choose shift = (128/2)+3 = 67
   Magic number = (2^67)/10 rounded up = 14757395258967641293
   where 10 is the constant divisor.
'/

#include "int128.bi"

dim as uint128_t  m, x, z
dim as long shift=67
dim as double t, t2

m="14757395258967641293"
x="123456789"

print x;"/10 = ";(x*m) shr shift

t=timer
for x as uint128_t = 0 to 10000000
	z=(x*m) shr shift
next
t=timer-t

t2=timer
for x as uint128_t = 0 to 10000000
	z=x/10
next
t2=timer-t2

print "time using magic number = ";t
print "time using division     = ";t2
Post Reply