Changeset 6acd020
- Timestamp:
- Jul 20, 2021, 9:06:24 PM (4 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- d2260ad
- Parents:
- 3d7d407 (diff), 86fc350 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Files:
-
- 1 added
- 24 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/code/cond-catch.cfa
r3d7d407 r6acd020 19 19 throw_exception(); 20 20 } catch (empty_exception * exc ; should_catch) { 21 // ...21 asm volatile ("# catch block (conditional)"); 22 22 } 23 23 } … … 37 37 cond_catch(); 38 38 } catch (empty_exception * exc) { 39 // ...39 asm volatile ("# catch block (unconditional)"); 40 40 } 41 41 } -
doc/theses/andrew_beach_MMath/code/cond-catch.cpp
r3d7d407 r6acd020 22 22 throw; 23 23 } 24 asm volatile ("# catch block (conditional)"); 24 25 } 25 26 } … … 39 40 cond_catch(); 40 41 } catch (EmptyException &) { 41 // ...42 asm volatile ("# catch block (unconditional)"); 42 43 } 43 44 } -
doc/theses/andrew_beach_MMath/code/cond-fixup.cfa
r3d7d407 r6acd020 19 19 throw_exception(); 20 20 } catchResume (empty_exception * exc ; should_catch) { 21 // ...21 asm volatile ("# fixup block (conditional)"); 22 22 } 23 23 } … … 37 37 cond_catch(); 38 38 } catchResume (empty_exception * exc) { 39 // ...39 asm volatile ("# fixup block (unconditional)"); 40 40 } 41 41 } -
doc/theses/andrew_beach_MMath/code/cross-catch.cfa
r3d7d407 r6acd020 7 7 EHM_EXCEPTION(not_raised_exception)(); 8 8 9 EHM_VIRTUAL_TABLE(not_raised_exception, not_vt); 10 9 11 int main(int argc, char * argv[]) { 10 12 unsigned int times = 1; 11 unsigned int total_frames = 1;13 bool should_throw = false; 12 14 if (1 < argc) { 13 15 times = strtol(argv[1], 0p, 10); 14 }15 if (2 < argc) {16 total_frames = strtol(argv[2], 0p, 10);17 16 } 18 17 … … 20 19 for (unsigned int count = 0 ; count < times ; ++count) { 21 20 try { 22 // ... 21 asm volatile ("# try block" : "=rm" (should_throw)); 22 if (should_throw) { 23 throw (not_raised_exception){¬_vt}; 24 } 23 25 } catch (not_raised_exception *) { 24 // ...26 asm volatile ("# catch block"); 25 27 } 26 28 } -
doc/theses/andrew_beach_MMath/code/cross-catch.cpp
r3d7d407 r6acd020 11 11 int main(int argc, char * argv[]) { 12 12 unsigned int times = 1; 13 bool should_throw = false; 13 14 if (1 < argc) { 14 15 times = strtol(argv[1], nullptr, 10); … … 18 19 for (unsigned int count = 0 ; count < times ; ++count) { 19 20 try { 20 // ... 21 asm volatile ("# try block" : "=rm" (should_throw)); 22 if (should_throw) { 23 throw NotRaisedException(); 24 } 21 25 } catch (NotRaisedException &) { 22 // ...26 asm volatile ("# catch block"); 23 27 } 24 28 } -
doc/theses/andrew_beach_MMath/code/cross-finally.cfa
r3d7d407 r6acd020 5 5 #include <stdlib.hfa> 6 6 7 EHM_EXCEPTION(not_raised_exception)(); 8 9 EHM_VIRTUAL_TABLE(not_raised_exception, not_vt); 10 7 11 int main(int argc, char * argv[]) { 8 12 unsigned int times = 1; 9 unsigned int total_frames = 1;13 bool should_throw = false; 10 14 if (1 < argc) { 11 15 times = strtol(argv[1], 0p, 10); 12 }13 if (2 < argc) {14 total_frames = strtol(argv[2], 0p, 10);15 16 } 16 17 17 18 Time start_time = timeHiRes(); 18 19 for (unsigned int count = 0 ; count < times ; ++count) { 19 try { 20 // ... 20 try { 21 asm volatile ("# try block" : "=rm" (should_throw)); 22 if (should_throw) { 23 throw (not_raised_exception){¬_vt}; 24 } 21 25 } finally { 22 // ...26 asm volatile ("# finally block"); 23 27 } 24 28 } -
doc/theses/andrew_beach_MMath/code/cross-resume.cfa
r3d7d407 r6acd020 20 20 for (unsigned int count = 0 ; count < times ; ++count) { 21 21 try { 22 // ...22 asm volatile (""); 23 23 } catchResume (not_raised_exception *) { 24 // ...24 asm volatile (""); 25 25 } 26 26 } -
doc/theses/andrew_beach_MMath/code/resume-detor.cfa
r3d7d407 r6acd020 12 12 13 13 void ^?{}(WithDestructor & this) { 14 // ... 14 asm volatile ("# destructor body"); 15 15 } 16 16 17 17 void unwind_destructor(unsigned int frames) { 18 18 if (frames) { 19 19 20 21 22 23 24 20 WithDestructor object; 21 unwind_destructor(frames - 1); 22 } else { 23 throwResume (empty_exception){&empty_vt}; 24 } 25 25 } 26 26 … … 36 36 37 37 Time start_time = timeHiRes(); 38 39 40 41 42 // ... 43 44 38 for (int count = 0 ; count < times ; ++count) { 39 try { 40 unwind_destructor(total_frames); 41 } catchResume (empty_exception *) { 42 asm volatile ("# fixup block"); 43 } 44 } 45 45 Time end_time = timeHiRes(); 46 46 sout | "Run-Time (ns): " | (end_time - start_time)`ns; -
doc/theses/andrew_beach_MMath/code/resume-empty.cfa
r3d7d407 r6acd020 32 32 unwind_empty(total_frames); 33 33 } catchResume (empty_exception *) { 34 // ...34 asm volatile ("# fixup block"); 35 35 } 36 36 } -
doc/theses/andrew_beach_MMath/code/resume-finally.cfa
r3d7d407 r6acd020 14 14 unwind_finally(frames - 1); 15 15 } finally { 16 // ...16 asm volatile ("# finally block"); 17 17 } 18 18 } else { … … 36 36 unwind_finally(total_frames); 37 37 } catchResume (empty_exception *) { 38 // ...38 asm volatile ("# fixup block"); 39 39 } 40 40 } -
doc/theses/andrew_beach_MMath/code/resume-other.cfa
r3d7d407 r6acd020 16 16 unwind_other(frames - 1); 17 17 } catchResume (not_raised_exception *) { 18 // ...18 asm volatile ("# fixup block (stack)"); 19 19 } 20 20 } else { … … 38 38 unwind_other(total_frames); 39 39 } catchResume (empty_exception *) { 40 // ...40 asm volatile ("# fixup block (base)"); 41 41 } 42 42 } -
doc/theses/andrew_beach_MMath/code/throw-detor.cfa
r3d7d407 r6acd020 12 12 13 13 void ^?{}(WithDestructor & this) { 14 // ...14 asm volatile ("# destructor body"); 15 15 } 16 16 … … 39 39 unwind_destructor(total_frames); 40 40 } catch (empty_exception *) { 41 // ...41 asm volatile ("# catch block"); 42 42 } 43 43 } -
doc/theses/andrew_beach_MMath/code/throw-detor.cpp
r3d7d407 r6acd020 10 10 11 11 struct WithDestructor { 12 ~WithDestructor() {} 12 ~WithDestructor() { 13 asm volatile ("# destructor body"); 14 } 13 15 }; 14 16 … … 37 39 unwind_destructor(total_frames); 38 40 } catch (EmptyException &) { 39 // ...41 asm volatile ("# catch block"); 40 42 } 41 43 } -
doc/theses/andrew_beach_MMath/code/throw-empty.cfa
r3d7d407 r6acd020 32 32 unwind_empty(total_frames); 33 33 } catch (empty_exception *) { 34 // ...34 asm volatile ("# catch block"); 35 35 } 36 36 } -
doc/theses/andrew_beach_MMath/code/throw-empty.cpp
r3d7d407 r6acd020 32 32 unwind_empty(total_frames); 33 33 } catch (EmptyException &) { 34 // ...34 asm volatile ("# catch block"); 35 35 } 36 36 } -
doc/theses/andrew_beach_MMath/code/throw-finally.cfa
r3d7d407 r6acd020 14 14 unwind_finally(frames - 1); 15 15 } finally { 16 // ...16 asm volatile ("# finally block"); 17 17 } 18 18 } else { … … 36 36 unwind_finally(total_frames); 37 37 } catch (empty_exception *) { 38 // ...38 asm volatile ("# catch block"); 39 39 } 40 40 } -
doc/theses/andrew_beach_MMath/code/throw-other.cfa
r3d7d407 r6acd020 16 16 unwind_other(frames - 1); 17 17 } catch (not_raised_exception *) { 18 // ...18 asm volatile ("# catch block (stack)"); 19 19 } 20 20 } else { … … 38 38 unwind_other(total_frames); 39 39 } catch (empty_exception *) { 40 // ...40 asm volatile ("# catch block (base)"); 41 41 } 42 42 } -
doc/theses/andrew_beach_MMath/code/throw-other.cpp
r3d7d407 r6acd020 16 16 unwind_other(frames - 1); 17 17 } catch (NotRaisedException &) { 18 // ...18 asm volatile ("# catch block (stack)"); 19 19 } 20 20 } else { … … 38 38 unwind_other(total_frames); 39 39 } catch (EmptyException &) { 40 // ...40 asm volatile ("# catch block (base)"); 41 41 } 42 42 } -
libcfa/prelude/builtins.c
r3d7d407 r6acd020 10 10 // Created On : Fri Jul 21 16:21:03 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Apr 13 17:26:32202113 // Update Count : 1 1712 // Last Modified On : Tue Jul 20 17:31:40 2021 13 // Update Count : 128 14 14 // 15 15 … … 78 78 79 79 static inline { 80 forall( DT & | { DT & ?+=?( DT &, one_t ); } )81 DT & ++?( DT & x ) { return x += 1; }80 forall( T | { T ?+=?( T &, one_t ); } ) 81 T ++?( T & x ) { return x += 1; } 82 82 83 forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )84 DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; }83 forall( T | { T ?+=?( T &, one_t ); } ) 84 T ?++( T & x ) { T tmp = x; x += 1; return tmp; } 85 85 86 forall( DT & | { DT & ?-=?( DT &, one_t ); } )87 DT & --?( DT & x ) { return x -= 1; }86 forall( T | { T ?-=?( T &, one_t ); } ) 87 T --?( T & x ) { return x -= 1; } 88 88 89 forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )90 DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; }89 forall( T | { T ?-=?( T &, one_t ); } ) 90 T ?--( T & x ) { T tmp = x; x -= 1; return tmp; } 91 91 92 forall( DT & | { int ?!=?( const DT &, zero_t ); } )93 int !?( const DT & x ) { return !( x != 0 ); }92 forall( T | { int ?!=?( T, zero_t ); } ) 93 int !?( T & x ) { return !( x != 0 ); } 94 94 } // distribution 95 95 -
libcfa/src/Makefile.am
r3d7d407 r6acd020 11 11 ## Created On : Sun May 31 08:54:01 2015 12 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Sat Apr 24 09:09:56202114 ## Update Count : 25 413 ## Last Modified On : Fri Jul 16 16:00:40 2021 14 ## Update Count : 255 15 15 ############################################################################### 16 16 … … 45 45 exception.h \ 46 46 gmp.hfa \ 47 math.trait.hfa \ 47 48 math.hfa \ 48 49 time_t.hfa \ -
libcfa/src/rational.cfa
r3d7d407 r6acd020 10 10 // Created On : Wed Apr 6 17:54:28 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Feb 8 17:56:36 202013 // Update Count : 1 8712 // Last Modified On : Tue Jul 20 16:30:06 2021 13 // Update Count : 193 14 14 // 15 15 … … 18 18 #include "stdlib.hfa" 19 19 20 forall( RationalImpl | arithmetic( RationalImpl) ) {20 forall( T | Arithmetic( T ) ) { 21 21 // helper routines 22 22 23 23 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce 24 24 // rationals. alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm 25 static RationalImpl gcd( RationalImpl a, RationalImplb ) {25 static T gcd( T a, T b ) { 26 26 for ( ;; ) { // Euclid's algorithm 27 RationalImplr = a % b;28 if ( r == ( RationalImpl){0} ) break;27 T r = a % b; 28 if ( r == (T){0} ) break; 29 29 a = b; 30 30 b = r; … … 33 33 } // gcd 34 34 35 static RationalImpl simplify( RationalImpl & n, RationalImpl& d ) {36 if ( d == ( RationalImpl){0} ) {35 static T simplify( T & n, T & d ) { 36 if ( d == (T){0} ) { 37 37 abort | "Invalid rational number construction: denominator cannot be equal to 0."; 38 38 } // exit 39 if ( d < ( RationalImpl){0} ) { d = -d; n = -n; } // move sign to numerator39 if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator 40 40 return gcd( abs( n ), d ); // simplify 41 41 } // Rationalnumber::simplify … … 43 43 // constructors 44 44 45 void ?{}( Rational(RationalImpl) & r ) { 46 r{ (RationalImpl){0}, (RationalImpl){1} }; 47 } // rational 48 49 void ?{}( Rational(RationalImpl) & r, RationalImpl n ) { 50 r{ n, (RationalImpl){1} }; 51 } // rational 52 53 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) { 54 RationalImpl t = simplify( n, d ); // simplify 45 void ?{}( Rational(T) & r, zero_t ) { 46 r{ (T){0}, (T){1} }; 47 } // rational 48 49 void ?{}( Rational(T) & r, one_t ) { 50 r{ (T){1}, (T){1} }; 51 } // rational 52 53 void ?{}( Rational(T) & r ) { 54 r{ (T){0}, (T){1} }; 55 } // rational 56 57 void ?{}( Rational(T) & r, T n ) { 58 r{ n, (T){1} }; 59 } // rational 60 61 void ?{}( Rational(T) & r, T n, T d ) { 62 T t = simplify( n, d ); // simplify 55 63 r.[numerator, denominator] = [n / t, d / t]; 56 64 } // rational 57 65 58 void ?{}( Rational(RationalImpl) & r, zero_t ) {59 r{ (RationalImpl){0}, (RationalImpl){1} };60 } // rational61 62 void ?{}( Rational(RationalImpl) & r, one_t ) {63 r{ (RationalImpl){1}, (RationalImpl){1} };64 } // rational65 66 66 // getter for numerator/denominator 67 67 68 RationalImpl numerator( Rational(RationalImpl) r ) {68 T numerator( Rational(T) r ) { 69 69 return r.numerator; 70 70 } // numerator 71 71 72 RationalImpl denominator( Rational(RationalImpl) r ) {72 T denominator( Rational(T) r ) { 73 73 return r.denominator; 74 74 } // denominator 75 75 76 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {76 [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ) { 77 77 return dest = src.[ numerator, denominator ]; 78 78 } // ?=? … … 80 80 // setter for numerator/denominator 81 81 82 RationalImpl numerator( Rational(RationalImpl) r, RationalImpln ) {83 RationalImplprev = r.numerator;84 RationalImplt = gcd( abs( n ), r.denominator ); // simplify82 T numerator( Rational(T) r, T n ) { 83 T prev = r.numerator; 84 T t = gcd( abs( n ), r.denominator ); // simplify 85 85 r.[numerator, denominator] = [n / t, r.denominator / t]; 86 86 return prev; 87 87 } // numerator 88 88 89 RationalImpl denominator( Rational(RationalImpl) r, RationalImpld ) {90 RationalImplprev = r.denominator;91 RationalImplt = simplify( r.numerator, d ); // simplify89 T denominator( Rational(T) r, T d ) { 90 T prev = r.denominator; 91 T t = simplify( r.numerator, d ); // simplify 92 92 r.[numerator, denominator] = [r.numerator / t, d / t]; 93 93 return prev; … … 96 96 // comparison 97 97 98 int ?==?( Rational( RationalImpl) l, Rational(RationalImpl) r ) {98 int ?==?( Rational(T) l, Rational(T) r ) { 99 99 return l.numerator * r.denominator == l.denominator * r.numerator; 100 100 } // ?==? 101 101 102 int ?!=?( Rational( RationalImpl) l, Rational(RationalImpl) r ) {102 int ?!=?( Rational(T) l, Rational(T) r ) { 103 103 return ! ( l == r ); 104 104 } // ?!=? 105 105 106 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 106 int ?!=?( Rational(T) l, zero_t ) { 107 return ! ( l == (Rational(T)){ 0 } ); 108 } // ?!=? 109 110 int ?<?( Rational(T) l, Rational(T) r ) { 107 111 return l.numerator * r.denominator < l.denominator * r.numerator; 108 112 } // ?<? 109 113 110 int ?<=?( Rational( RationalImpl) l, Rational(RationalImpl) r ) {114 int ?<=?( Rational(T) l, Rational(T) r ) { 111 115 return l.numerator * r.denominator <= l.denominator * r.numerator; 112 116 } // ?<=? 113 117 114 int ?>?( Rational( RationalImpl) l, Rational(RationalImpl) r ) {118 int ?>?( Rational(T) l, Rational(T) r ) { 115 119 return ! ( l <= r ); 116 120 } // ?>? 117 121 118 int ?>=?( Rational( RationalImpl) l, Rational(RationalImpl) r ) {122 int ?>=?( Rational(T) l, Rational(T) r ) { 119 123 return ! ( l < r ); 120 124 } // ?>=? … … 122 126 // arithmetic 123 127 124 Rational( RationalImpl) +?( Rational(RationalImpl) r ) {125 return (Rational( RationalImpl)){ r.numerator, r.denominator };128 Rational(T) +?( Rational(T) r ) { 129 return (Rational(T)){ r.numerator, r.denominator }; 126 130 } // +? 127 131 128 Rational( RationalImpl) -?( Rational(RationalImpl) r ) {129 return (Rational( RationalImpl)){ -r.numerator, r.denominator };132 Rational(T) -?( Rational(T) r ) { 133 return (Rational(T)){ -r.numerator, r.denominator }; 130 134 } // -? 131 135 132 Rational( RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {136 Rational(T) ?+?( Rational(T) l, Rational(T) r ) { 133 137 if ( l.denominator == r.denominator ) { // special case 134 return (Rational( RationalImpl)){ l.numerator + r.numerator, l.denominator };138 return (Rational(T)){ l.numerator + r.numerator, l.denominator }; 135 139 } else { 136 return (Rational( RationalImpl)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };140 return (Rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; 137 141 } // if 138 142 } // ?+? 139 143 140 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 144 Rational(T) ?+=?( Rational(T) & l, Rational(T) r ) { 145 l = l + r; 146 return l; 147 } // ?+? 148 149 Rational(T) ?+=?( Rational(T) & l, one_t ) { 150 l = l + (Rational(T)){ 1 }; 151 return l; 152 } // ?+? 153 154 Rational(T) ?-?( Rational(T) l, Rational(T) r ) { 141 155 if ( l.denominator == r.denominator ) { // special case 142 return (Rational( RationalImpl)){ l.numerator - r.numerator, l.denominator };156 return (Rational(T)){ l.numerator - r.numerator, l.denominator }; 143 157 } else { 144 return (Rational( RationalImpl)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };158 return (Rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; 145 159 } // if 146 160 } // ?-? 147 161 148 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 149 return (Rational(RationalImpl)){ l.numerator * r.numerator, l.denominator * r.denominator }; 162 Rational(T) ?-=?( Rational(T) & l, Rational(T) r ) { 163 l = l - r; 164 return l; 165 } // ?-? 166 167 Rational(T) ?-=?( Rational(T) & l, one_t ) { 168 l = l - (Rational(T)){ 1 }; 169 return l; 170 } // ?-? 171 172 Rational(T) ?*?( Rational(T) l, Rational(T) r ) { 173 return (Rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; 150 174 } // ?*? 151 175 152 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 153 if ( r.numerator < (RationalImpl){0} ) { 176 Rational(T) ?*=?( Rational(T) & l, Rational(T) r ) { 177 return l = l * r; 178 } // ?*? 179 180 Rational(T) ?/?( Rational(T) l, Rational(T) r ) { 181 if ( r.numerator < (T){0} ) { 154 182 r.[numerator, denominator] = [-r.numerator, -r.denominator]; 155 183 } // if 156 return (Rational( RationalImpl)){ l.numerator * r.denominator, l.denominator * r.numerator };184 return (Rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator }; 157 185 } // ?/? 158 186 187 Rational(T) ?/=?( Rational(T) & l, Rational(T) r ) { 188 return l = l / r; 189 } // ?/? 190 159 191 // I/O 160 192 161 forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl& ); } )162 istype & ?|?( istype & is, Rational( RationalImpl) & r ) {193 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) 194 istype & ?|?( istype & is, Rational(T) & r ) { 163 195 is | r.numerator | r.denominator; 164 RationalImplt = simplify( r.numerator, r.denominator );196 T t = simplify( r.numerator, r.denominator ); 165 197 r.numerator /= t; 166 198 r.denominator /= t; … … 168 200 } // ?|? 169 201 170 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl); } ) {171 ostype & ?|?( ostype & os, Rational( RationalImpl) r ) {202 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { 203 ostype & ?|?( ostype & os, Rational(T) r ) { 172 204 return os | r.numerator | '/' | r.denominator; 173 205 } // ?|? 174 206 175 void ?|?( ostype & os, Rational( RationalImpl) r ) {207 void ?|?( ostype & os, Rational(T) r ) { 176 208 (ostype &)(os | r); ends( os ); 177 209 } // ?|? … … 179 211 } // distribution 180 212 181 forall( RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } ) 182 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) { 183 if ( y < 0 ) { 184 return (Rational(RationalImpl)){ x.denominator \ -y, x.numerator \ -y }; 185 } else { 186 return (Rational(RationalImpl)){ x.numerator \ y, x.denominator \ y }; 187 } // if 188 } 213 forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { 214 Rational(T) ?\?( Rational(T) x, long int y ) { 215 if ( y < 0 ) { 216 return (Rational(T)){ x.denominator \ -y, x.numerator \ -y }; 217 } else { 218 return (Rational(T)){ x.numerator \ y, x.denominator \ y }; 219 } // if 220 } // ?\? 221 222 Rational(T) ?\=?( Rational(T) & x, long int y ) { 223 return x = x \ y; 224 } // ?\? 225 } // distribution 189 226 190 227 // conversion 191 228 192 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl); } )193 double widen( Rational( RationalImpl) r ) {229 forall( T | Arithmetic( T ) | { double convert( T ); } ) 230 double widen( Rational(T) r ) { 194 231 return convert( r.numerator ) / convert( r.denominator ); 195 232 } // widen 196 233 197 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImplconvert( double ); } )198 Rational( RationalImpl) narrow( double f, RationalImplmd ) {234 forall( T | Arithmetic( T ) | { double convert( T ); T convert( double ); } ) 235 Rational(T) narrow( double f, T md ) { 199 236 // http://www.ics.uci.edu/~eppstein/numth/frap.c 200 if ( md <= ( RationalImpl){1} ) { // maximum fractional digits too small?201 return (Rational( RationalImpl)){ convert( f ), (RationalImpl){1}}; // truncate fraction237 if ( md <= (T){1} ) { // maximum fractional digits too small? 238 return (Rational(T)){ convert( f ), (T){1}}; // truncate fraction 202 239 } // if 203 240 204 241 // continued fraction coefficients 205 RationalImplm00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 };206 RationalImplai, t;242 T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; 243 T ai, t; 207 244 208 245 // find terms until denom gets too big … … 221 258 if ( f > (double)0x7FFFFFFF ) break; // representation failure 222 259 } // for 223 return (Rational( RationalImpl)){ m00, m10 };260 return (Rational(T)){ m00, m10 }; 224 261 } // narrow 225 262 -
libcfa/src/rational.hfa
r3d7d407 r6acd020 12 12 // Created On : Wed Apr 6 17:56:25 2016 13 13 // Last Modified By : Peter A. Buhr 14 // Last Modified On : Tue Mar 26 23:16:10 201915 // Update Count : 1 0914 // Last Modified On : Tue Jul 20 17:45:29 2021 15 // Update Count : 118 16 16 // 17 17 … … 19 19 20 20 #include "iostream.hfa" 21 22 trait scalar( T ) { 23 }; 24 25 trait arithmetic( T | scalar( T ) ) { 26 int !?( T ); 27 int ?==?( T, T ); 28 int ?!=?( T, T ); 29 int ?<?( T, T ); 30 int ?<=?( T, T ); 31 int ?>?( T, T ); 32 int ?>=?( T, T ); 33 void ?{}( T &, zero_t ); 34 void ?{}( T &, one_t ); 35 T +?( T ); 36 T -?( T ); 37 T ?+?( T, T ); 38 T ?-?( T, T ); 39 T ?*?( T, T ); 40 T ?/?( T, T ); 41 T ?%?( T, T ); 42 T ?/=?( T &, T ); 43 T abs( T ); 44 }; 21 #include "math.trait.hfa" // Arithmetic 45 22 46 23 // implementation 47 24 48 forall( RationalImpl | arithmetic( RationalImpl) ) {25 forall( T | Arithmetic( T ) ) { 49 26 struct Rational { 50 RationalImpl numerator, denominator;// invariant: denominator > 027 T numerator, denominator; // invariant: denominator > 0 51 28 }; // Rational 52 29 53 30 // constructors 54 31 55 void ?{}( Rational( RationalImpl) & r );56 void ?{}( Rational( RationalImpl) & r, RationalImpl n);57 void ?{}( Rational( RationalImpl) & r, RationalImpl n, RationalImpl d);58 void ?{}( Rational( RationalImpl) & r, zero_t);59 void ?{}( Rational( RationalImpl) & r, one_t);32 void ?{}( Rational(T) & r ); 33 void ?{}( Rational(T) & r, zero_t ); 34 void ?{}( Rational(T) & r, one_t ); 35 void ?{}( Rational(T) & r, T n ); 36 void ?{}( Rational(T) & r, T n, T d ); 60 37 61 38 // numerator/denominator getter 62 39 63 RationalImpl numerator( Rational(RationalImpl) r );64 RationalImpl denominator( Rational(RationalImpl) r );65 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );40 T numerator( Rational(T) r ); 41 T denominator( Rational(T) r ); 42 [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ); 66 43 67 44 // numerator/denominator setter 68 45 69 RationalImpl numerator( Rational(RationalImpl) r, RationalImpln );70 RationalImpl denominator( Rational(RationalImpl) r, RationalImpld );46 T numerator( Rational(T) r, T n ); 47 T denominator( Rational(T) r, T d ); 71 48 72 49 // comparison 73 50 74 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 75 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 76 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 77 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 78 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 79 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 51 int ?==?( Rational(T) l, Rational(T) r ); 52 int ?!=?( Rational(T) l, Rational(T) r ); 53 int ?!=?( Rational(T) l, zero_t ); // => ! 54 int ?<?( Rational(T) l, Rational(T) r ); 55 int ?<=?( Rational(T) l, Rational(T) r ); 56 int ?>?( Rational(T) l, Rational(T) r ); 57 int ?>=?( Rational(T) l, Rational(T) r ); 80 58 81 59 // arithmetic 82 60 83 Rational(RationalImpl) +?( Rational(RationalImpl) r ); 84 Rational(RationalImpl) -?( Rational(RationalImpl) r ); 85 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 86 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 87 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 88 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 61 Rational(T) +?( Rational(T) r ); 62 Rational(T) -?( Rational(T) r ); 63 Rational(T) ?+?( Rational(T) l, Rational(T) r ); 64 Rational(T) ?+=?( Rational(T) & l, Rational(T) r ); 65 Rational(T) ?+=?( Rational(T) & l, one_t ); // => ++?, ?++ 66 Rational(T) ?-?( Rational(T) l, Rational(T) r ); 67 Rational(T) ?-=?( Rational(T) & l, Rational(T) r ); 68 Rational(T) ?-=?( Rational(T) & l, one_t ); // => --?, ?-- 69 Rational(T) ?*?( Rational(T) l, Rational(T) r ); 70 Rational(T) ?*=?( Rational(T) & l, Rational(T) r ); 71 Rational(T) ?/?( Rational(T) l, Rational(T) r ); 72 Rational(T) ?/=?( Rational(T) & l, Rational(T) r ); 89 73 90 74 // I/O 91 forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl& ); } )92 istype & ?|?( istype &, Rational( RationalImpl) & );75 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) 76 istype & ?|?( istype &, Rational(T) & ); 93 77 94 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl); } ) {95 ostype & ?|?( ostype &, Rational( RationalImpl) );96 void ?|?( ostype &, Rational( RationalImpl) );78 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { 79 ostype & ?|?( ostype &, Rational(T) ); 80 void ?|?( ostype &, Rational(T) ); 97 81 } // distribution 98 82 } // distribution 99 83 100 forall( RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} ) 101 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ); 84 forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { 85 Rational(T) ?\?( Rational(T) x, long int y ); 86 Rational(T) ?\=?( Rational(T) & x, long int y ); 87 } // distribution 102 88 103 89 // conversion 104 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl); } )105 double widen( Rational( RationalImpl) r );106 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImplconvert( double );} )107 Rational( RationalImpl) narrow( double f, RationalImplmd );90 forall( T | Arithmetic( T ) | { double convert( T ); } ) 91 double widen( Rational(T) r ); 92 forall( T | Arithmetic( T ) | { double convert( T ); T convert( double );} ) 93 Rational(T) narrow( double f, T md ); 108 94 109 95 // Local Variables: // -
tests/.expect/rational.txt
r3d7d407 r6acd020 1 1 constructor 2 3/1 4/1 0/1 0/1 1/1 3 1/2 5/7 4 2/3 -3/2 5 -2/3 3/2 6 logical 7 -2/1 -3/2 8 1 9 1 10 1 11 0 12 0 2 a : 3/1 b : 4/1 c : 0/1 d : 0/1 e : 1/1 3 a : 1/2 b : 5/7 4 a : 2/3 b : -3/2 5 a : -2/3 b : 3/2 6 7 comparison 8 a : -2/1 b : -3/2 9 a == 0 : 0 10 a == 1 : 0 11 a != 0 : 1 12 ! a : 0 13 a != b : 1 14 a < b : 1 15 a <= b : 1 16 a > b : 0 17 a >= b : 0 18 13 19 arithmetic 14 -2/1 -3/2 15 -7/2 16 -1/2 17 3/1 18 4/3 20 a : -2/1 b : -3/2 21 a + b : -7/2 22 a += b : -7/2 23 ++a : -5/2 24 a++ : -5/2 25 a : -3/2 26 a - b : 0/1 27 a -= b : 0/1 28 --a : -1/1 29 a-- : -1/1 30 a : -2/1 31 a * b : 3/1 32 a / b : 4/3 33 a \ 2 : 4/1 b \ 2 : 9/4 34 a \ -2 : 1/4 b \ -2 : 4/9 35 19 36 conversion 20 37 0.75 … … 24 41 1/7 25 42 355/113 26 decompose 43 27 44 more tests 28 45 -3/2 -
tests/rational.cfa
r3d7d407 r6acd020 10 10 // Created On : Mon Mar 28 08:43:12 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Feb 8 18:46:23 202013 // Update Count : 8612 // Last Modified On : Tue Jul 20 18:13:40 2021 13 // Update Count : 107 14 14 // 15 15 … … 26 26 sout | "constructor"; 27 27 RatInt a = { 3 }, b = { 4 }, c, d = 0, e = 1; 28 sout | a | b | c | d| e;28 sout | "a : " | a | "b : " | b | "c : " | c | "d : " | d | "e : " | e; 29 29 30 30 a = (RatInt){ 4, 8 }; 31 31 b = (RatInt){ 5, 7 }; 32 sout | a| b;32 sout | "a : " | a | "b : " | b; 33 33 a = (RatInt){ -2, -3 }; 34 34 b = (RatInt){ 3, -2 }; 35 sout | a| b;35 sout | "a : " | a | "b : " | b; 36 36 a = (RatInt){ -2, 3 }; 37 37 b = (RatInt){ 3, 2 }; 38 sout | a | b; 38 sout | "a : " | a | "b : " | b; 39 sout | nl; 39 40 40 sout | " logical";41 sout | "comparison"; 41 42 a = (RatInt){ -2 }; 42 43 b = (RatInt){ -3, 2 }; 43 sout | a | b; 44 // sout | a == 1; // FIX ME 45 sout | a != b; 46 sout | a < b; 47 sout | a <= b; 48 sout | a > b; 49 sout | a >= b; 44 sout | "a : " | a | "b : " | b; 45 sout | "a == 0 : " | a == (Rational(int)){0}; // FIX ME 46 sout | "a == 1 : " | a == (Rational(int)){1}; // FIX ME 47 sout | "a != 0 : " | a != 0; 48 sout | "! a : " | ! a; 49 sout | "a != b : " | a != b; 50 sout | "a < b : " | a < b; 51 sout | "a <= b : " | a <= b; 52 sout | "a > b : " | a > b; 53 sout | "a >= b : " | a >= b; 54 sout | nl; 50 55 51 56 sout | "arithmetic"; 52 sout | a | b; 53 sout | a + b; 54 sout | a - b; 55 sout | a * b; 56 sout | a / b; 57 // sout | a \ 2 | b \ 2; // FIX ME 58 // sout | a \ -2 | b \ -2; 57 sout | "a : " | a | "b : " | b; 58 sout | "a + b : " | a + b; 59 sout | "a += b : " | (a += b); 60 sout | "++a : " | ++a; 61 sout | "a++ : " | a++; 62 sout | "a : " | a; 63 sout | "a - b : " | a - b; 64 sout | "a -= b : " | (a -= b); 65 sout | "--a : " | --a; 66 sout | "a-- : " | a--; 67 sout | "a : " | a; 68 sout | "a * b : " | a * b; 69 sout | "a / b : " | a / b; 70 sout | "a \\ 2 : " | a \ 2u | "b \\ 2 : " | b \ 2u; 71 sout | "a \\ -2 : " | a \ -2 | "b \\ -2 : " | b \ -2; 72 sout | nl; 59 73 60 74 sout | "conversion"; … … 68 82 sout | narrow( 0.14285714285714, 16 ); 69 83 sout | narrow( 3.14159265358979, 256 ); 84 sout | nl; 70 85 71 sout | "decompose";72 int n, d;73 //[n, d] = a;74 //sout | a | n | d;86 // sout | "decompose"; 87 // int n, d; 88 // [n, d] = a; 89 // sout | a | n | d; 75 90 76 91 sout | "more tests";
Note: See TracChangeset
for help on using the changeset viewer.