Skip to content

Instantly share code, notes, and snippets.

@plaes
Created October 2, 2019 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plaes/dec4b76ffc4b720726f21031d8d399ac to your computer and use it in GitHub Desktop.
Save plaes/dec4b76ffc4b720726f21031d8d399ac to your computer and use it in GitHub Desktop.
/* compile with gcc -lm -march=native test_tms34020_setcdp.c */
#include <stdint.h>
#include <stdio.h>
#include <math.h>
uint32_t setcdp(uint32_t dptch) {
uint32_t m_convdp;
// Check whether we're dealing with an even number
if ((dptch & 1) == 0) {
switch(__builtin_popcount(dptch))
{
// .. only single bit set, pitch is power of two!
case 1:
{
uint32_t val = log2(dptch);
m_convdp = ~val & 0x1f;
// printf("case: power of two: input: %x, ouput: %x\t", dptch, m_convdp);
return m_convdp;
}
// .. two bits, we can decompose it to sum of two power of two numbers
case 2:
{
uint8_t v1 = __builtin_ffs(dptch) - 1;
uint8_t v2 = __builtin_ffs(dptch & ~(1 << v1)) - 1;
v1 = ~v1 & 0x1f;
v2 = ~v2 & 0x1f;
m_convdp = v2 + (v1 << 8);
// printf("case: two powers of two: input: %x, ouput: %x\t", dptch, m_convdp);
return m_convdp;
}
}
}
// Default to arbitrary number, setting pitch to 0
m_convdp = 0;
// printf("case: arbitrary: input: %x, ouput: %x\t", dptch, m_convdp);
return m_convdp;
}
int main() {
uint32_t test_vectors[4][2] = {
{0x1000, 0x13},
{0x400, 0x15},
{0x1400, 0x1513},
{0x19, 0x0},
};
for (int i = 0; i < sizeof(test_vectors) / ( 2 * sizeof(uint32_t)); i++) {
uint32_t result = setcdp(test_vectors[i][0]);
printf("IN: %x: OUT: %x\t-\t", test_vectors[i][0], result);
if (result != test_vectors[i][1]) {
printf("FAIL!\n");
} else {
printf("OK!\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment