This is an archive of the discontinued LLVM Phabricator instance.

[Fixed Point Arithmetic] Remaining Binary Operations on Primary Fixed Point Types
Needs ReviewPublic

Authored by leonardchan on May 15 2018, 7:50 PM.

Details

Summary

This patch implements the remaining arithmetic and logical operations on the primary fixed point types.

The operations are +, -, *, /, <<, and >>.

// Addition
s_accum = 3.0hk;
short _Accum s_accum_sum = s_accum + s_accum2;
assert(s_accum_sum == 5);
assert(s_fract + s_fract2 == 0);

// Subtraction
short _Accum s_accum_diff = s_accum - s_accum2;
assert(s_accum_diff == 1);
assert(s_accum2 - s_accum == -1);

// Multiplication
short _Accum s_accum_mul = s_accum * s_accum2;
assert(s_accum_mul == 6);
assert(2.0hk * 3.0hk == 6);
assert(2.0hk * 3 == 6);
assert(2.5hk * 3 == 7.5k);
assert(-2.5hk * 3 == -7.5lk);
assert(3 * -2.5hk == -7.5hk);
assert(-2.5hk * 0 == 0);

// Division
const short _Accum s_accum3 = 2.5hk;
short _Accum s_accum_div = s_accum3 / s_accum2;
assert(s_accum_div == 1.25hk);
assert(5.0hk / s_accum3 == 2);
assert(-5.0hk / s_accum3 == -2);
assert(9.9k / 3.3k == 3);
assert(9.9hk / 3.3k != 3);  // We lose precision when converting between types of different
                            // fractional width.
assert(6.75hk / 2.25k == 3);  // Unless the fractional part can be evenly represented with
                              // sums of powers of 2.
assert(0 / 2.0hk == 0);

% is not a valid operation on fixed point types.

Diff Detail

Repository
rC Clang

Event Timeline

leonardchan created this revision.May 15 2018, 7:50 PM
leonardchan edited the summary of this revision. (Show Details)
leonardchan retitled this revision from Remaining Binary Operations on Primary Fixed Point Types to [Fixed Point Arithmetic] Remaining Binary Operations on Primary Fixed Point Types.May 16 2018, 9:42 AM
Ka-Ka added a subscriber: Ka-Ka.May 23 2018, 1:09 AM