1) File ultranaiveprimes.c
- Code: Tout sélectionner
// ultranaiveprimes.c: silly algorithm for finding primes
#define MIN_AMS 101
#define USE_TI89
#define USE_TI92P
#define USE_V200
#define USE_TI89T
#define NO_CALC_DETECT
#define OPTIMIZE_ROM_CALLS
#define RETURN_VALUE
#include <stdint.h>
#include <system.h>
#include <args.h>
#include <estack.h>
#include <intr.h>
#include <timath.h>
#define TIMER_START_VAL (100000UL)
#define N (10000)
void _main(void) {
uint16_t j, k;
short orig_rate = PRG_getRate();
unsigned short orig_start = PRG_getStart();
unsigned long val = 0;
// Make the system timer an order of magnitude more precise;
// NOTE: this code assumes a HW2+ TI-68k, i.e. anything since 1999.
PRG_setRate(1); // Increment counter at a rate of 2^19/2^9 Hz
PRG_setStart(0xCE); // Trigger the interrupt every 257 - 0xCE = 51 increments ~ 20.07 Hz.
// The PRG_getStart() above effectively waited for the interrupt to trigger, so we don't need another wait.
/*OSRegisterTimer(USER_TIMER, 1);
while (!OSTimerExpired(USER_TIMER));
OSFreeTimer(USER_TIMER);*/
OSRegisterTimer(USER_TIMER, TIMER_START_VAL);
// Main loop :)
for (k = 3; k < N; k++) {
for (j = 2; j < k - 1; j++) {
if (k % j == 0) {
j = k - 1; // so we exit from the inner for
}
}
}
// Retrieve timer value.
val = TIMER_START_VAL - OSTimerCurVal(USER_TIMER);
OSFreeTimer(USER_TIMER);
// Push arguments onto the RPN stack: clean arguments up, then create a list.
while (GetArgType (top_estack) != END_TAG) {
top_estack = next_expression_index (top_estack);
}
top_estack--;
push_longint(val);
// Restore old system state.
PRG_setRate(orig_rate);
PRG_setStart(orig_start);
}
2) Compiler invocation
- Code: Tout sélectionner
tigcc -v -O3 -Wall -W -mpcrel --optimize-code --cut-ranges --reorder-sections --remove-unused --merge-constants -fmerge-all-constants -Wa,--all-relocs -Wa,-l -fverbose-asm -save-temps -o unprimes ultranaiveprimes.c
3) Results on 89T HW4 AMS 3.10 patched with tiosmod+amspatch:
* N=1000: 25, 25, i.e. about 1"25.
* N=10000: 1918, 1919, i.e. ~1'35"6.