From: "Lawrence Rust" Newsgroups: comp.os.msdos.djgpp References: <9krgph$496$04$1 AT news DOT t-online DOT com> Subject: Re: 128 bit integer Lines: 43 Organization: Software Systems X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-Mimeole: Produced By Microsoft MimeOLE V5.50.4522.1200 Message-ID: Date: Wed, 8 Aug 2001 17:07:39 +0100 NNTP-Posting-Host: 213.107.106.28 X-Complaints-To: abuse AT ntlworld DOT com X-Trace: news11-gui.server.ntli.net 997286840 213.107.106.28 (Wed, 08 Aug 2001 17:07:20 BST) NNTP-Posting-Date: Wed, 08 Aug 2001 17:07:20 BST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com "Kai Dietrich" wrote in message news:9krgph$496$04$1 AT news DOT t-online DOT com... Message by Eli Zaretskii< eliz AT is DOT elta DOT co DOT il> on: 08.08.01 15:51:59 > >>I want to do simple 128 bit integer math (+ and -). Do I have to combine >>two long long int by myself or is there something like a library or a >>little bit free source code which does that job? > [snip] > I want the solution to behave like long long but with 128 bits > (256 or more would be OK, too) instead of 64. I know that it is > possible, it is the same solution like getting long long working > on a 32bit machine. I would be able to combined two long long but > I can only write + not - :-(. I need a little bit help writing > > _int128 add(_int128 a, _int128 b); > > _int128 sub(_int128 a, _int128 b); Extended precision addition can be achieved in C as follows: unsigned char a[2], b[2], sum[2], carry; sum[0] = a[0] + b[0]; carry = sum[0] < a[0] ? 1 : 0; sum[1] = a[1] + b[1] + carry; Subtraction follows similarly. This approach can be extended to unsigned integers of any size. For best efficiency replace char with the longest unsigned integer supported by the implementation i.e. unsigned long long for DJGPP Signed integers can be represented by the 2's complement of the unsigned value. NB it's not good etiquette to post in HTML to usenet groups. -- Lawrence Rust Software Systems www.softsystem.co.uk