URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
Subversion Repositories forwardcom
[/] [forwardcom/] [examples/] [sumarray.as] - Rev 158
Go to most recent revision | Compare with Previous | Blame | View Log
/**************************** sumarray.as ************************************ Author: Agner Fog* date created: 2018-02-24* last modified: 2021-08-08* Version: 1.11* Project: ForwardCom example, assembly code* Description: Calculates the sum of the numbers from 1 to 100** This code will fill an array with the numbers from 1 to 100 and then* calculate the sum. The purpose is to show how the variable-length vector* instructions work.* The expected result is the mean times the number: (1+100)*100/2 = 5050.** Copyright 2018-2021 GNU General Public License http://www.gnu.org/licenses*****************************************************************************/%num = 100 // number of array elementsconst section read ip // read-only data sectionconclude: int8 "\nThe sum of numbers from 1 to %i is %i\n",0 // format string for printfconst endbss section datap uninitialized // uninitialized read/write data sectionint32 myarray[num] // array of 100 integersint64 parlist[4] // parameter list for printfbss endcode section execute align = 4 // code sectionextern _printf: function // library function: formatted output to stdout_main function public // program begins here// Step 1: Fill myarray with numbers 1 .. 100int64 r0 = num // = 100int32 v1 = make_sequence(r0, 1) // will be (1, 2, 3, ...) up to as much as the maximum vector length allows, or 100int32 r1 = get_num(v1) // number of elements in vectorint64 r2 = r1 - 1 // index to last elementint32 v2 = extract(v1, r2) // get last element and broadcast it// A vector loop needs a pointer to the end of the arrayint64 r2 = address [myarray+num*4] // address of the end of myarrayint64 r1 = num * 4 // total array size in bytes// This loop will count down r1 with the maximum length until the array is filled// The last iteration will automatically get fewer elements if the array size is not divisible by the maximum lengthfor (int32 v1 in [r2-r1]) {int32 [r2-r1, length = r1] = v1 // put as many elements into the array as the maximum length permitsint32 v1 += v2 // add up the vector of sequential numbers}// Step 2: Calculate the sum of all elements in the arrayint32 v0 = replace(v1, 0) // make a vector of all zeroes and same lengthint64 r1 = num * 4 // total array size in bytes// vector loop, counting down r1 with the maximum length until the array is filledfor (int32 v0 in [r2-r1]) {int32 v0 += [r2-r1, length = r1] // add elements to vector}// Step 3: Calculate the horizontal sum of the elements in v0int32 r1 = get_len(v0) // length of vector in bytes// Round up the vector length to the nearest power of 2.// The maximum vector length is known to be a power of 2,// but the length may be 'num' elements, which is not a power of 2int32 r1 = roundp2(r1, 1) // r1 is now a power of 2, not bigger than the maximum vector lengthint32 v0 = set_len(v0, r1) // adjust vector length to nearest higher power of 2. Added elements will be zerowhile (uint32+ r1 > 4) { // loop to calculate horizontal sumuint32+ r1 >>= 1 // the vector length is halvedint32 v1 = shift_reduce (v0, r1) // get upper half of vector// Add upper half and lower half// The result vector has the length of the first operand, which will be halved each iterationint32 v0 = v1 + v0}// The sum is now a scalar in v0// Step 4: Write the resultint64 r0 = address([conclude]) // format string for printfint64 r1 = address([parlist]) // parameter listint32 [r1] = num // put number into parameter listint32 [r1+8, scalar] = v0 // put result into parameter listcall _printf // printf("\nThe sum of numbers from 1 to %i is %i", num, v0)// Return from mainint64 r0 = 0 // program return valuereturn // return from main_main endcode end
Go to most recent revision | Compare with Previous | Blame | View Log
