Arm/normalize

From ASMBits

arm/bcd2Previous

Given an array of unsigned 32-bit words (e.g., audio samples), shift all of the samples left or right so that the largest sample will fit into an unsigned 16-bit number. Then write out an array of unsigned 16-bit half-words with the scaled samples.

If the largest (highest amplitude) sample in the input array requires more than 16 bits to store, shift right all of the samples until it fits. If the largest sample requires less than 16 bits to store, left shift all of the samples so that bit 15 of the largest sample is 1. (i.e., increase the amplitude by a power of two as much as possible, while still fitting into 16 bits).

In the example below, the largest sample is 0x80000, which requires 20 bits to represent. Thus, all of the samples are right-shifted by 4 bits. The largest sample then becomes 0x8000, which requires 16 bits.

The function has three parameters:

  • r0: The number of elements in the input (and output) array
  • r1: A pointer to the input array of unsigned 32-bit words. Do not modify this array.
  • r2: A pointer to the output array of unsigned 16-bit half-words. Write your output here.

There is no return value.

void normalize(unsigned int length, const unsigned int *input, unsigned short *output);

You may wish to reuse your solutions to arm/maxu and arm/bits1.


Sample Input

[0x12345, 0x20000, 0x80000, 0x4000]

Sample Output

[0x1234, 0x2000, 0x8000, 0x400]

Write your solution here

Upload a source file...