mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-22 00:06:59 +02:00
Improve auto-generation of format conversion routines.
This commit is contained in:
@@ -10273,9 +10273,6 @@ void mal_blend_f32(float* pOut, float* pInA, float* pInB, float factor, mal_uint
|
|||||||
//
|
//
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
#if 0
|
|
||||||
#include "tools/malgen/bin/malgen_test0.c"
|
|
||||||
#else
|
|
||||||
void mal_pcm_u8_to_s16(short* pOut, const unsigned char* pIn, unsigned int count)
|
void mal_pcm_u8_to_s16(short* pOut, const unsigned char* pIn, unsigned int count)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
@@ -10442,19 +10439,10 @@ void mal_pcm_s32_to_f32(float* pOut, const int* pIn, unsigned int count)
|
|||||||
float r;
|
float r;
|
||||||
for (unsigned int i = 0; i < count; ++i) {
|
for (unsigned int i = 0; i < count; ++i) {
|
||||||
int x = pIn[i];
|
int x = pIn[i];
|
||||||
|
|
||||||
#if 1
|
|
||||||
double t;
|
double t;
|
||||||
t = (double)(x + 2147483648);
|
t = (double)(x + 2147483648);
|
||||||
t = t * 0.0000000004656612873077392578125;
|
t = t * 0.0000000004656612873077392578125;
|
||||||
r = (float)(t - 1);
|
r = (float)(t - 1);
|
||||||
#else
|
|
||||||
int s;
|
|
||||||
s = ((*((int*)&x)) & 0x80000000) >> 31;
|
|
||||||
s = s + 2147483647;
|
|
||||||
r = x / (float)(unsigned int)s;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
pOut[i] = (float)r;
|
pOut[i] = (float)r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10466,18 +10454,8 @@ void mal_pcm_f32_to_u8(unsigned char* pOut, const float* pIn, unsigned int count
|
|||||||
float x = pIn[i];
|
float x = pIn[i];
|
||||||
float c;
|
float c;
|
||||||
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
||||||
|
|
||||||
#if 1
|
|
||||||
c = c + 1;
|
c = c + 1;
|
||||||
r = (int)(c * 127.5f);
|
r = (int)(c * 127.5f);
|
||||||
#else
|
|
||||||
int s;
|
|
||||||
s = ((*((int*)&x)) & 0x80000000) >> 31;
|
|
||||||
s = s + 127;
|
|
||||||
r = (int)(c * s);
|
|
||||||
r = r + 128;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
pOut[i] = (unsigned char)r;
|
pOut[i] = (unsigned char)r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10489,18 +10467,9 @@ void mal_pcm_f32_to_s16(short* pOut, const float* pIn, unsigned int count)
|
|||||||
float x = pIn[i];
|
float x = pIn[i];
|
||||||
float c;
|
float c;
|
||||||
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
||||||
|
|
||||||
#if 1
|
|
||||||
c = c + 1;
|
c = c + 1;
|
||||||
r = (int)(c * 32767.5f);
|
r = (int)(c * 32767.5f);
|
||||||
r = r - 32768;
|
r = r - 32768;
|
||||||
#else
|
|
||||||
int s;
|
|
||||||
s = ((*((int*)&x)) & 0x80000000) >> 31;
|
|
||||||
s = s + 32767;
|
|
||||||
r = (int)(c * s);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
pOut[i] = (short)r;
|
pOut[i] = (short)r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10512,18 +10481,9 @@ void mal_pcm_f32_to_s24(void* pOut, const float* pIn, unsigned int count)
|
|||||||
float x = pIn[i];
|
float x = pIn[i];
|
||||||
float c;
|
float c;
|
||||||
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
||||||
|
|
||||||
#if 1
|
|
||||||
c = c + 1;
|
c = c + 1;
|
||||||
r = (int)(c * 8388607.5f);
|
r = (int)(c * 8388607.5f);
|
||||||
r = r - 8388608;
|
r = r - 8388608;
|
||||||
#else
|
|
||||||
int s;
|
|
||||||
s = ((*((int*)&x)) & 0x80000000) >> 31;
|
|
||||||
s = s + 8388607;
|
|
||||||
r = (int)(c * s);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
((unsigned char*)pOut)[(i*3)+0] = (unsigned char)(r & 0xFF); ((unsigned char*)pOut)[(i*3)+1] = (unsigned char)((r & 0xFF00) >> 8); ((unsigned char*)pOut)[(i*3)+2] = (unsigned char)((r & 0xFF0000) >> 16);
|
((unsigned char*)pOut)[(i*3)+0] = (unsigned char)(r & 0xFF); ((unsigned char*)pOut)[(i*3)+1] = (unsigned char)((r & 0xFF00) >> 8); ((unsigned char*)pOut)[(i*3)+2] = (unsigned char)((r & 0xFF0000) >> 16);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10534,24 +10494,14 @@ void mal_pcm_f32_to_s32(int* pOut, const float* pIn, unsigned int count)
|
|||||||
for (unsigned int i = 0; i < count; ++i) {
|
for (unsigned int i = 0; i < count; ++i) {
|
||||||
float x = pIn[i];
|
float x = pIn[i];
|
||||||
float c;
|
float c;
|
||||||
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
|
||||||
|
|
||||||
#if 1
|
|
||||||
mal_int64 t;
|
mal_int64 t;
|
||||||
|
c = ((x < -1) ? -1 : ((x > 1) ? 1 : x));
|
||||||
c = c + 1;
|
c = c + 1;
|
||||||
t = (mal_int64)(c * 2147483647.5);
|
t = (mal_int64)(c * 2147483647.5);
|
||||||
r = (int)(t - 2147483648);
|
r = (int)(t - 2147483648);
|
||||||
#else
|
|
||||||
mal_int64 s;
|
|
||||||
s = ((*((int*)&x)) & 0x80000000) >> 31;
|
|
||||||
s = s + 2147483647;
|
|
||||||
r = (int)(c * s);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
pOut[i] = (int)r;
|
pOut[i] = (int)r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -116,53 +116,48 @@ s32->s24 {
|
|||||||
shr r x 8;
|
shr r x 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
# r = x / (2147483647 + sign(x))
|
# r = ((x + 2147483648) * 0.0000000004656612873077392578125) - 1
|
||||||
s32->f32 {
|
s32->f32 {
|
||||||
int s;
|
dbl t;
|
||||||
sig s x;
|
add (dbl)t x 2147483648;
|
||||||
add s s 2147483647;
|
mul t t 0.0000000004656612873077392578125;
|
||||||
div r x (flt)(uint)s;
|
sub (flt)r t 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# r = (clip(x) * (0x7F + sign(x))) + 128
|
# r = (clip(x) + 1) * 127.5
|
||||||
f32->u8 {
|
f32->u8 {
|
||||||
flt c;
|
flt c;
|
||||||
int s;
|
|
||||||
clip c x;
|
clip c x;
|
||||||
sig s x;
|
add c c 1;
|
||||||
add s s 127;
|
mul (int)r c 127.5f;
|
||||||
mul (int)r c s;
|
|
||||||
add r r 128;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# r = clip(x) * (0x7FFF + sign(x))
|
# r = (clip(x) + 1) * 32767.5 - 32768
|
||||||
f32->s16 {
|
f32->s16 {
|
||||||
flt c;
|
flt c;
|
||||||
int s;
|
|
||||||
clip c x;
|
clip c x;
|
||||||
sig s x;
|
add c c 1;
|
||||||
add s s 32767;
|
mul (int)r c 32767.5f;
|
||||||
mul (int)r c s;
|
sub r r 32768;
|
||||||
}
|
}
|
||||||
|
|
||||||
# r = clip(x) * (0x7FFFFF + sign(x))
|
# r = (clip(x) + 1) * 8388607.5 - 8388608
|
||||||
f32->s24 {
|
f32->s24 {
|
||||||
flt c;
|
flt c;
|
||||||
int s;
|
|
||||||
clip c x;
|
clip c x;
|
||||||
sig s x;
|
add c c 1;
|
||||||
add s s 8388607;
|
mul (int)r c 8388607.5f;
|
||||||
mul (int)r c s;
|
sub r r 8388608;
|
||||||
}
|
}
|
||||||
|
|
||||||
# r = clip(x) * (0x7FFFFFFF + sign(x))
|
# r = (clip(x) + 1) * 2147483647.5 - 2147483648
|
||||||
f32->s32 {
|
f32->s32 {
|
||||||
flt c;
|
flt c;
|
||||||
lng s;
|
lng t;
|
||||||
clip c x;
|
clip c x;
|
||||||
sig s x;
|
add c c 1;
|
||||||
add s s 2147483647;
|
mul (lng)t c 2147483647.5;
|
||||||
mul (int)r c s;
|
sub (int)r t 2147483648;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -413,6 +413,15 @@ std::string malgen_format_op_param(const char* param)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// (lng) -> (mal_int64)
|
||||||
|
{
|
||||||
|
const char* src = "(lng)"; const char* dst = "(mal_int64)";
|
||||||
|
size_t loc = s.find(src);
|
||||||
|
if (loc != std::string::npos) {
|
||||||
|
s = s.replace(loc, strlen(src), dst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,11 +465,14 @@ std::string malgen_generate_code__conversion_func_inst(malgen_context* pContext,
|
|||||||
code += "int "; code += pInst->params[0];
|
code += "int "; code += pInst->params[0];
|
||||||
}
|
}
|
||||||
if (strcmp(pInst->name, "lng") == 0) {
|
if (strcmp(pInst->name, "lng") == 0) {
|
||||||
code += "long long "; code += pInst->params[0];
|
code += "mal_int64 "; code += pInst->params[0];
|
||||||
}
|
}
|
||||||
if (strcmp(pInst->name, "flt") == 0) {
|
if (strcmp(pInst->name, "flt") == 0) {
|
||||||
code += "float "; code += pInst->params[0];
|
code += "float "; code += pInst->params[0];
|
||||||
}
|
}
|
||||||
|
if (strcmp(pInst->name, "dbl") == 0) {
|
||||||
|
code += "double "; code += pInst->params[0];
|
||||||
|
}
|
||||||
|
|
||||||
if (strcmp(pInst->name, "add") == 0) {
|
if (strcmp(pInst->name, "add") == 0) {
|
||||||
code += malgen_generate_code__conversion_func_inst_binary_op(pInst->params[0], pInst->params[1], pInst->params[2], "+");
|
code += malgen_generate_code__conversion_func_inst_binary_op(pInst->params[0], pInst->params[1], pInst->params[2], "+");
|
||||||
@@ -487,7 +499,7 @@ std::string malgen_generate_code__conversion_func_inst(malgen_context* pContext,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(pInst->name, "sig") == 0) { // <-- This gets the sign of the first input parameter and moves it to the result.
|
if (strcmp(pInst->name, "sig") == 0) { // <-- This gets the sign of the first input parameter and moves it to the result.
|
||||||
code += pInst->params[0]; code += " = "; code += "((*((int*)&"; code += pInst->params[1]; code += ")) & 0x80000000) >> 31";
|
code += pInst->params[0]; code += " = "; code += "(("; code += pInst->params[1]; code += " < 0) ? 1 : 0)"; // ((a < 0) ? 1 : 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(pInst->name, "clip") == 0) { // clamp(a, -1, 1) -> r = ((a < -1) ? -1 : ((a > 1) ? 1 : a))
|
if (strcmp(pInst->name, "clip") == 0) { // clamp(a, -1, 1) -> r = ((a < -1) ? -1 : ((a > 1) ? 1 : a))
|
||||||
|
|||||||
Reference in New Issue
Block a user