decoration decoration
Stories

GROKLAW
When you want to know more...
decoration
For layout only
Home
Archives
Site Map
Search
About Groklaw
Awards
Legal Research
Timelines
ApplevSamsung
ApplevSamsung p.2
ArchiveExplorer
Autozone
Bilski
Cases
Cast: Lawyers
Comes v. MS
Contracts/Documents
Courts
DRM
Gordon v MS
GPL
Grokdoc
HTML How To
IPI v RH
IV v. Google
Legal Docs
Lodsys
MS Litigations
MSvB&N
News Picks
Novell v. MS
Novell-MS Deal
ODF/OOXML
OOXML Appeals
OraclevGoogle
Patents
ProjectMonterey
Psystar
Quote Database
Red Hat v SCO
Salus Book
SCEA v Hotz
SCO Appeals
SCO Bankruptcy
SCO Financials
SCO Overview
SCO v IBM
SCO v Novell
SCO:Soup2Nuts
SCOsource
Sean Daly
Software Patents
Switch to Linux
Transcripts
Unix Books

Gear

Groklaw Gear

Click here to send an email to the editor of this weblog.


You won't find me on Facebook


Donate

Donate Paypal


No Legal Advice

The information on Groklaw is not intended to constitute legal advice. While Mark is a lawyer and he has asked other lawyers and law students to contribute articles, all of these articles are offered to help educate, not to provide specific legal advice. They are not your lawyers.

Here's Groklaw's comments policy.


What's New

STORIES
No new stories

COMMENTS last 48 hrs
No new comments


Sponsors

Hosting:
hosted by ibiblio

On servers donated to ibiblio by AMD.

Webmaster
only one way? | 221 comments | Create New Account
Comments belong to whoever posts them. Please notify us of inappropriate comments.
only one way?
Authored by: ais523 on Monday, October 22 2012 @ 02:34 AM EDT

(PJ/Mark/some moderator: the parent comment is breaking the layout of the page somehow, could you edit it to fix that? It's probably an unbalanced tag.)

I had a different reaction to the question. If I'm writing an "add" function in my compiler, it's probably because I need to implement addition in terms of simpler operations because it doesn't exist on the platform I'm aiming for. The only reasonable interpretation of that would be if I'm trying to add numbers too wide for the compiler to handle natively, so I'd do something like this (note: this code is machine-specific, to a little-endian system which has 32-bit ints and two's complement signed overflow but cannot natively add more than 16 bits, again as you'd expect for a task like this):

int add(int i, int j)
{
  unsigned short i_low = ((short*)&i)[0];
  short i_high = ((short*)&i)[1];
  unsigned short j_low = ((short*)&j)[0];
  short j_high = ((short*)&j)[1];
  unsigned short r_low = i_low + j_low;
  short r_high = i_high + j_high;
  int r;
  /* handle carry: if i_low + j_low overflows, the result
     will be less than some input, otherwise it will be
     greater than or equal to both inputs */
  if (i_low + j_low < i_low ||
      i_low + j_low < j_low) r_high++;
  ((short*)&r)[0] = r_low;
  ((short*)&r)[1] = r_high;
  return r;
}

This was written from memory (and may wel contain mistakes), but I believe there's some pretty similar code in gcc somewhere, again trying to implement the addition operator as a function.

In general, writing a function to do addition is a little pointless unless the implementation is a complicated one like this; you'd just use the operator instead. Also, even if you did need to write something like this out, it'd be better to get at the carry flag in some system-specific way rather than doing extra arithmetic.

[ Reply to This | Parent | # ]

Groklaw © Copyright 2003-2013 Pamela Jones.
All trademarks and copyrights on this page are owned by their respective owners.
Comments are owned by the individual posters.

PJ's articles are licensed under a Creative Commons License. ( Details )