Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: nOw2 on July 21, 2013, 01:46:35 PM

Title: 64 bit integers in SAS/C
Post by: nOw2 on July 21, 2013, 01:46:35 PM
What's the best way to handle large numbers in SAS/C?

I want to calculate disk space for >4GB partitions. 64 bit would be ideal, but 48bit would work for now. Possibly also need to pass the numbers through library calls too, but that's a secondary issue.
Title: Re: 64 bit integers in SAS/C
Post by: Thomas on July 21, 2013, 02:55:20 PM
To get the size in megabytes you can divide the number of blocks on the disk by the number of blocks per megabyte. This way all numbers fit into 32 bits.
Title: Re: 64 bit integers in SAS/C
Post by: nicholas on July 21, 2013, 04:50:06 PM
I'm not 100% certain but I've got a vague recollection that the update from 6.58 to 7.01 introduced better large number handling.  I could be wrong though as it's been a long time since I used it.

http://aminet.net/package/biz/patch/expsascxx

I've just read the readme and it doesn't mention it so I must have remembered wrongly.

Can't hurt to try though. ;)
Title: Re: 64 bit integers in SAS/C
Post by: pjhutch on July 21, 2013, 06:33:11 PM
The Amiga utility.library for AmigaOS 3.9 comes with 64 bit functions:
SMult64, UMult64

I have not seen anything in the SAS/C about 64 bit functions.
Title: Re: 64 bit integers in SAS/C
Post by: Thorham on July 21, 2013, 07:05:22 PM
Why bother? Thomas' solution works fine.
Title: Re: 64 bit integers in SAS/C
Post by: psxphill on July 21, 2013, 08:11:06 PM
Quote from: nicholas;741476
I'm not 100% certain but I've got a vague recollection that the update from 6.58 to 7.01 introduced better large number handling. I could be wrong though as it's been a long time since I used it.
 
http://aminet.net/package/biz/patch/expsascxx
 
I've just read the readme and it doesn't mention it so I must have remembered wrongly.
 
Can't hurt to try though. ;)

http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/changes/zgenid-5.htm
 
long long
Title: Re: 64 bit integers in SAS/C
Post by: nOw2 on July 21, 2013, 08:53:36 PM
The value needs to be passed around in bytes (to support existing code and also importantly maintain resolution), it's not just the issue of reading the size from disk. Though that does the question of doing that - I'd want to maintain accuracy so would need the true size of disks/files rather than the more granular block sizes.

I'm not confident of patching the compiler with experimental code. SAS/C 6.5x is a restriction at the moment.

I'm thinking of using two longs to hold the data, even if that makes calculations interesting. UtilityBase functions may be suitable for that, otherwise I'll roll my own. This way existing code will continue to work as it does today (little-endian style) and new code can be adapted to see the higher bytes.

Then I need to work out how to write that as human readable - converting to display in MB/GB/TB I think should be okay as by that point the calculation will leave a single long suitable for sprintf, but starting to worry myself about how to get the large >2^32 byte values out in ASCII.
Title: Re: 64 bit integers in SAS/C
Post by: itix on July 21, 2013, 09:11:49 PM
Quote from: nOw2;741497

I'm thinking of using two longs to hold the data, even if that makes calculations interesting. UtilityBase functions may be suitable for that, otherwise I'll roll my own. This way existing code will continue to work as it does today (little-endian style) and new code can be adapted to see the higher bytes.


Create small structure with two longs and develop small framework around it to do 64-bit math and ToString() function to convert numbers to ASCII.
Title: Re: 64 bit integers in SAS/C
Post by: nicholas on July 21, 2013, 09:30:44 PM
Quote from: psxphill;741490
http://support.sas.com/documentation/onlinedoc/sasc/doc750/html/changes/zgenid-5.htm
 
long long


My memory isn't failing me as much as I thought then. :)
Title: Re: 64 bit integers in SAS/C
Post by: matthey on July 21, 2013, 10:06:55 PM
Quote from: nOw2;741497

I'm not confident of patching the compiler with experimental code. SAS/C 6.5x is a restriction at the moment.


If you roll your own 64-bit int math functionality then it will also be experimental. If you don't want experimental then consider another compiler. I've found bugs in the 64-bit integer math of some versions of GCC 3.x. The latest version of vbcc has been accurate and is reasonably fast from what I've seen with 64-bit int. The next version out soon will have much faster 64x64=64 for the 68060 ;).

What does SAS/C support that you need?

Quote from: nOw2;741497

I'm thinking of using two longs to hold the data, even if that makes calculations interesting. UtilityBase functions may be suitable for that, otherwise I'll roll my own. This way existing code will continue to work as it does today (little-endian style) and new code can be adapted to see the higher bytes.


The utility.library only has partial 64-bit integer functions. A true 64x64=64 bit multiplication becomes easy with 32x32=64 help but there is not much help for 64/64=64 which is dead slow as there is no hardware help on any 68k processor. Addition and subtraction of full 64-bit numbers are easy enough but faster with hardware help that C does not provide. Logic operations are no problem and fast even with C.

Itix's suggestion can be done but you will be reinventing the wheel for a stoneage compiler. New compilers already have well oiled and nicely rounded 64-bit wheels.
Title: Re: 64 bit integers in SAS/C
Post by: ChaosLord on July 22, 2013, 01:32:27 AM
Quote from: nOw2;741497

I'm not confident of patching the compiler with experimental code. SAS/C 6.5x is a restriction at the moment.

The patch was created by the ppl who wrote SASC.  It isn't like some random haxx0r made it. :)

Also, once you realize that all C compilers are experimental and have weird rare bugs then you realize that everything is ok.

There are at least 2 bugs in SASC 6.58.  Perhaps these have been fixed in v7.x?  Who knows?


Quote

I'm thinking of using two longs to hold the data, even if that makes calculations interesting. UtilityBase functions may be suitable for that, otherwise I'll roll my own.



Yep you are always allowed to create your own new datatypes.  You can code them in C or asm since SASC interfaces to asm routines easily.
Title: Re: 64 bit integers in SAS/C
Post by: nicholas on July 22, 2013, 01:50:01 AM
Quote from: nOw2;741497
I'm not confident of patching the compiler with experimental code. SAS/C 6.5x is a restriction at the moment.


Well if it makes you feel more comfortable, it's more stable than 6.58 was as I remember it. :)
Title: Re: 64 bit integers in SAS/C
Post by: olsen on July 22, 2013, 08:07:25 AM
Quote from: nOw2;741465
What's the best way to handle large numbers in SAS/C?

I want to calculate disk space for >4GB partitions. 64 bit would be ideal, but 48bit would work for now. Possibly also need to pass the numbers through library calls too, but that's a secondary issue.


Have a look at the http://aminet.net/comm/tcp/SendRawDisk.lha utility which I wrote a while ago (full source code included). It includes both 64 bit arithmetic code written in portable 'C', as well a complete set of functions for accessing "large disks" (covering both NSD/TD64, with auto-detection of some sort which handles both sets transparently).

The purpose of this utility was to make it easier to transfer disk images from my Amiga to UAE. But if you're after the arithmetic functions, please by my guest :)

Multiplication and addition are rather straightforward. The division algorithm is a tricky one, though. Would you believe that there are several different division algorithms, some more readable than others?

If you're in dire need of arithmetic algorithms, I would hesitate to recommend Knuth's "The Art of Computer Programming: Seminumerical algorithms" (chapter 4). You have to master both the language in which the implementations are written, and make something useful out of the copious notes which accompany them. Many programmers work extra hard on solving problems on their own, rather than looking them up in the Knuth ;)
Title: Re: 64 bit integers in SAS/C
Post by: ChaosLord on July 22, 2013, 08:33:13 AM
Quote from: olsen;741551
Have a look at the http://aminet.net/comm/tcp/SendRawDisk.lha utility which I wrote a while ago (full source code included). It includes both 64 bit arithmetic code written in portable 'C', as well a complete set of functions for accessing "large disks" (covering both NSD/TD64, with auto-detection of some sort which handles both sets transparently).

Yeah!!!!!!!!!!!!!!!!!!!!!!  Olsen saves the day!!!!!!  :banana:


Quote

The purpose of this utility was to make it easier to transfer disk images from my Amiga to UAE. But if you're after the arithmetic functions, please by my guest :)

Multiplication and addition are rather straightforward. The division algorithm is a tricky one, though. Would you believe that there are several different division algorithms, some more readable than others?

If you're in dire need of arithmetic algorithms, I would hesitate to recommend Knuth's "The Art of Computer Programming: Seminumerical algorithms" (chapter 4). You have to master both the language in which the implementations are written, and make something useful out of the copious notes which accompany them. Many programmers work extra hard on solving problems on their own, rather than looking them up in the Knuth ;)


I have the 2nd edition of that book from 1981.  Is that good enough?
Have any new algorithms been invented since 1981?




@nOw2

I have not tested SASC v7.x yet.  I plan to test it sometime in a couple of weeks and I will compile giant piles of source codes thru it to see if it works or not.

I predict that 64-bit integers (long long) will work perfectly in SASC v7.x

I predict that any bugs will be in a few random routines in the C library where they forgot to add 64-bit support or somehow messed it up.  These library functions will be functions I have never used and never will use or I use them but will never use them with 64-bit integers.  For what you are trying to accomplish I think you will be safe and it would take you years to find a bug.

It took me something like 6 years to find a bug in SASC 6.58.  And I was using the compiler every single day and night and compiling giant programs with it.   Its not like I was just compiling a tiny 2000 line program on the weekends or something.  I was really banging SASC hard.  The bug I found can only be triggered by linking asm code (which gets inlined) that takes floating point parameters.  Although I have been doing some hardcore studying of Advanced C books lately and I have found that C wants to always promote all types to either into or double when calling functions.  So maybe I could have worked around the problem by using Doubles instead of Floats?  (The forced promotion is not supposed to happen when you use a concrete datatype.)


p.s. Feel free to post any discoveries or problems that you find both here and at www.AmigaCoding.de (http://www.AmigaCoding.de) which is an entire giant forum specifically for Programming Amigas and is kinda like the reincarnation of UtilityBase (R.I.P.)
Title: Re: 64 bit integers in SAS/C
Post by: olsen on July 22, 2013, 09:48:16 AM
Quote from: ChaosLord;741553
Yeah!!!!!!!!!!!!!!!!!!!!!!  Olsen saves the day!!!!!!  :banana:


Many of the problems which are still relevant were solved (or at least chipped away at) some 5-10 years ago. It's just getting harder to find the solutions. My own contribution is (correctly) filed away in the comm/tcp section of Aminet, but who'd look there for it?

It's challenging to build a good knowledge base for Amiga programming :(

Quote

I have the 2nd edition of that book from 1981.  Is that good enough?


I'm not the one to judge, but it appears that some algorithms covered in great depth and detail by Knuth have aged better than others. Also, Knuth selected the algorithms which he covered both for their importance and didactic reasons, which means that the books are not necessarily a "directory" of algorithms and the problems which they solve. For that kind of book, I'd recommend "The algorithm design manual" by Steven Skiena.

Quote

Have any new algorithms been invented since 1981?


Of course :)  For example, probabilistic algorithms (e.g. skip lists) were not invented until the late 1980'ies.
Title: Re: 64 bit integers in SAS/C
Post by: ChaosLord on July 22, 2013, 10:45:28 AM
Quote from: olsen;741562

I'm not the one to judge, but it appears that some algorithms covered in great depth and detail by Knuth have aged better than others. Also, Knuth selected the algorithms which he covered both for their importance and didactic reasons, which means that the books are not necessarily a "directory" of algorithms and the problems which they solve. For that kind of book, I'd recommend "The algorithm design manual" by Steven Skiena.


I have the 2nd edition of that back from 2008.

Any other books I should arm myself with?

Would you happen to know of any books that you recommend that explain every single feature that was added to C99 with examples?  Or a good article on the internet?
Title: Re: 64 bit integers in SAS/C
Post by: olsen on July 22, 2013, 11:18:13 AM
Quote from: ChaosLord;741565
I have the 2nd edition of that back from 2008.

Any other books I should arm myself with?


Arm against what, exactly? ;)

Now that we have the Internet (things were so much rougher in the early 2000's and before), the importance of having the right selection of books in your personal library has declined somewhat. If you follow the trail of breadcrumbs, you'll invariably find a good, comprehensive collection of algorithm documentation and implementation. Just start on the page for "Quicksort" in Wikipedia and look through the footnotes and links at the bottom of the article's page.

The one exception concerning books I would make is Steven Skiena's book ("The algorithm design manual") which I already mentioned. The first half of the book covers algorithms in general (what are these things, how do they work, how do you build them, how can you tell when there's no scalable/optimal solution for a problem), and while this part is already great, the second half is really crackers.

The second half (modestly labeled "Resources") is a catalog of algorithms, categorized by problem domain. This doesn't sound much, but each section contains a general description of what problems fall into each class and can be solved algorithmically, and then gives a list of hints that allow you to pick the best likely solution for each problem.

This helps to overcome the big obstacle you'll always have when you're looking for a good or better approach to solving a problem with an algorithm: finding one which does the job, and making an informed decision when choosing between alternatives. For example, when you have to sort small numbers of integers, what kind of algorithm will you pick? Will you use heaps, for example, or trust in a recursive algorithm? Which circumstances surrounding your need for a matching algorithm would favour heapsort over shellsort, or even merge sort?

Quote

Would you happen to know of any books that you recommend that explain every single feature that was added to C99 with examples?  Or a good article on the internet?


I can recommend "C - A reference manual" (5th edition) by Guy Steele & Sam Harbison (well, it's mainly by Guy Steele). It makes for a great reference manual, too, so the title is actually spot on and not just for show ;)
Title: Re: 64 bit integers in SAS/C
Post by: nOw2 on July 24, 2013, 12:37:38 PM
Quote from: olsen;741551
Have a look at the http://aminet.net/comm/tcp/SendRawDisk.lha utility which I wrote a while ago (full source code included). It includes both 64 bit arithmetic code written in portable 'C', as well a complete set of functions for accessing "large disks" (covering both NSD/TD64, with auto-detection of some sort which handles both sets transparently).


Just a quick reply to say thanks as that is exactly what I was looking for. I didn't know where to start but that gets me going in the right direction.

I now have Directory Opus 5 reporting disk sizes correctly. Next job, calculations for files and directories.
Title: Re: 64 bit integers in SAS/C
Post by: ChaosLord on July 24, 2013, 01:11:06 PM
@Now2

Is the Directory Opus 5 you are working on the same thing as:
Opus Magellan II (v5.8): 1998-11-01 ?

For some reason I had it in my head that v5.8 already had 64-bit math in it...
Title: Re: 64 bit integers in SAS/C
Post by: nOw2 on July 24, 2013, 06:25:47 PM
Yes, or 5.82 Magellan 2 at least. It was open sourced through a bounty. And no, it doesn't support 64bit maths on sizes :)
Title: Re: 64 bit integers in SAS/C
Post by: olsen on July 25, 2013, 03:27:35 PM
Quote from: nOw2;741977
Just a quick reply to say thanks as that is exactly what I was looking for. I didn't know where to start but that gets me going in the right direction.

I now have Directory Opus 5 reporting disk sizes correctly. Next job, calculations for files and directories.


Cool, so writing the stuff in the first place and putting it on Aminet for others to find it useful did turn out to be a good idea. Glad to have been of some help :)
Title: Re: 64 bit integers in SAS/C
Post by: Thorham on May 06, 2017, 10:57:27 PM
VERY late, but here's disk size in bytes for greater than 32 bits using only 32 bit integers:

Code: [Select]
   unsigned int low;
    unsigned int high;

    low = info->id_NumBlocks % 100000;
    high = info->id_NumBlocks / 100000;

    low *= info->id_BytesPerBlock;
    high *= info->id_BytesPerBlock;

    high += low / 100000;
    low = low % 100000;

    printf("Size: %d%d bytes.\n", high, low);
Title: Re: 64 bit integers in SAS/C
Post by: guest11527 on May 06, 2017, 11:11:04 PM
Reviving this old thread....  Back then, I had (and still have) a somewhat icky approach at 64 bit math with SAS/C which required a bit wrestling with the compiler. The trick is to compile with math=ieee, then use a typedef  typedef double long_long;  and instead of the usual arithmetic operators, you called (in my case assembler, but could also be C) stups "addll(op1,op2)" and so on of the signature  long_long addll(op1,op2);  The trick is here that SAS/C packs the "seemingly floating point numbers" into register pairs (as by math=ieee) and you could store and move them around as you like as SAS/C does itself not care what the registers represent. The only troublesome part was the arithmetic which had to be spelled out by individual function calls instead of the usual operators (which would have called mathieeedoubbas, which you did not want).  Now, if you used the SAS/C C++ compiler, you could get away even with that and write it in the usual arithmetics with operator overloading...  It's a neat trick, though you still did not have a "printf()" for a long long. Sorry, long_long.
Title: Re: 64 bit integers in SAS/C
Post by: billt on May 07, 2017, 03:15:33 PM
How dows one get sasc 7.01? I only remember 6.5something as the max...
Title: Re: 64 bit integers in SAS/C
Post by: psxphill on May 07, 2017, 05:07:52 PM
Quote from: billt;825465
How dows one get sasc 7.01? I only remember 6.5something as the max...


These two are on aminet, no idea what the difference is between them

http://aminet.net/package/dev/c/expsascxx.lha
http://aminet.net/package/dev/c/expsascxx_old.lha
Title: Re: 64 bit integers in SAS/C
Post by: psxphill on May 07, 2017, 05:12:37 PM
Quote from: Thorham;825449


Code: [Select]

    printf("Size: %d%d bytes.\n", high, low);


The printf is wrong, because low isn't zero filled.

you'd need something like:
Code: [Select]

    if (high)
        printf("Size: %d%05d bytes.\n", high, low);
    else
        printf("Size: %d bytes.\n", low);
Title: Re: 64 bit integers in SAS/C
Post by: Thorham on May 07, 2017, 10:39:45 PM
Quote from: psxphill;825467
The printf is wrong, because low isn't zero filled.
You're right, thanks. Don't know how I missed that.