From: Konstantinos Margaritis Date: Sun, 25 Sep 2022 16:55:15 +0000 (+0000) Subject: clean up, convert from uint64 for python due to rounding in Python, fix copying functions X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ff1cb1d9b81b8e5a606ca93653efa75242844969;p=openpower-isa.git clean up, convert from uint64 for python due to rounding in Python, fix copying functions --- diff --git a/media/video/libvpx/variance_svp64_wrappers.c b/media/video/libvpx/variance_svp64_wrappers.c index 38828e10..cd41e2f5 100644 --- a/media/video/libvpx/variance_svp64_wrappers.c +++ b/media/video/libvpx/variance_svp64_wrappers.c @@ -15,21 +15,20 @@ uint32_t vpx_get_mb_ss_svp64(const int16_t *src_ptr) { pypowersim_state_t *state = pypowersim_prepare(); // Change the relevant elements, mandatory: body - // state->binary = PyBytes_FromStringAndSize((const char *)&vpx_get_mb_ss_svp64_real, 1000); // Set GPR #3 to the pointer - PyObject *address = PyLong_FromLongLong(src_ptr_svp64); + PyObject *address = PyLong_FromUnsignedLongLong(src_ptr_svp64); PyList_SetItem(state->initial_regs, 3, address); // Load data into buffer from real memory for (int i=0; i < 256; i += 4) { - PyObject *address = PyLong_FromLongLong(src_ptr_svp64); - uint64_t val = src_ptr[0]; - val |= (uint64_t)(src_ptr[1]) << 16; - val |= (uint64_t)(src_ptr[2]) << 32; - val |= (uint64_t)(src_ptr[3]) << 48; - //printf("src: %p -> %04x %04x %04x %04x, val: %016x -> %p\n", src_ptr, src_ptr[0], src_ptr[1], src_ptr[2], src_ptr[3], val, src_ptr_svp64); - PyObject *word = PyLong_FromLongLong(val); - PyDict_SetItem(state->initial_mem, address, word); + PyObject *svp64_address = PyLong_FromUnsignedLongLong(src_ptr_svp64); + uint64_t val = (uint64_t)(src_ptr[0]) & 0xffff; + val |= ((uint64_t)(src_ptr[1]) & 0xffff) << 16; + val |= ((uint64_t)(src_ptr[2]) & 0xffff) << 32; + val |= ((uint64_t)(src_ptr[3]) & 0xffff) << 48; + // printf("src: %p -> %04x %04x %04x %04x\t val: %016lx -> %p\n", src_ptr, (uint16_t)src_ptr[0], (uint16_t)src_ptr[1], (uint16_t)src_ptr[2], (uint16_t)src_ptr[3], val, src_ptr_svp64); + PyObject *word = PyLong_FromUnsignedLongLong(val); + PyDict_SetItem(state->initial_mem, svp64_address, word); src_ptr += 4; src_ptr_svp64 += 8; } @@ -39,26 +38,39 @@ uint32_t vpx_get_mb_ss_svp64(const int16_t *src_ptr) { // Call the function and get the resulting object state->result_obj = PyObject_CallObject(state->simulator, state->args); - Py_DECREF(state->simulator); - Py_DECREF(state->args); if (!state->result_obj) { PyErr_Print(); printf("Error invoking 'run_a_simulation'\n"); + pypowersim_finalize(state); + exit(1); } // Get the GPRs from the result_obj PyObject *final_regs = PyObject_GetAttrString(state->result_obj, "gpr"); if (!final_regs) { PyErr_Print(); - Py_DECREF(state->result_obj); printf("Error getting final GPRs\n"); + pypowersim_finalize(state); + exit(1); } // GPR #3 holds the return value as an integer PyObject *key = PyLong_FromLongLong(3); PyObject *itm = PyDict_GetItem(final_regs, key); + if (!itm) { + PyErr_Print(); + printf("Error getting GPR #3\n"); + pypowersim_finalize(state); + exit(1); + } PyObject *value = PyObject_GetAttrString(itm, "value"); - uint64_t val = PyLong_AsLongLong(value); + if (!value) { + PyErr_Print(); + printf("Error getting value of GPR #3\n"); + pypowersim_finalize(state); + exit(1); + } + uint64_t val = PyLong_AsUnsignedLongLong(value); // Return value return (uint32_t) val; @@ -78,16 +90,17 @@ uint32_t vpx_get4x4sse_cs_svp64(const uint8_t *src_ptr, int src_stride, // Change the relevant elements, mandatory: body state->binary = PyBytes_FromStringAndSize((const char *)&vpx_get4x4sse_cs_svp64_real, 1000); // Set GPR #3 to the src_ptr - PyObject *src_address = PyLong_FromLongLong(src_ptr_svp64); + PyObject *src_address = PyLong_FromUnsignedLongLong(src_ptr_svp64); PyList_SetItem(state->initial_regs, 3, src_address); // Load data into buffer from real memory for (int r=0; r < 4; r++) { - PyObject *address = PyLong_FromLongLong(src_ptr_svp64); - uint64_t val = src_ptr[0]; - val |= (uint64_t)(src_ptr[1]) << 16; - val |= (uint64_t)(src_ptr[2]) << 32; - val |= (uint64_t)(src_ptr[3]) << 48; - PyObject *word = PyLong_FromLongLong(val); + PyObject *address = PyLong_FromUnsignedLongLong(src_ptr_svp64); + uint64_t val = (uint64_t)(src_ptr[0]) & 0xffff; + val |= ((uint64_t)(src_ptr[1]) & 0xffff) << 16; + val |= ((uint64_t)(src_ptr[2]) & 0xffff) << 32; + val |= ((uint64_t)(src_ptr[3]) & 0xffff) << 48; + //printf("src: %p -> %04x %04x %04x %04x\t val: %016lx -> %p\n", src_ptr, (uint16_t)src_ptr[0], (uint16_t)src_ptr[1], (uint16_t)src_ptr[2], (uint16_t)src_ptr[3], val, src_ptr_svp64); + PyObject *word = PyLong_FromUnsignedLongLong(val); PyDict_SetItem(state->initial_mem, address, word); src_ptr += src_stride; src_ptr_svp64 += 8; @@ -97,17 +110,17 @@ uint32_t vpx_get4x4sse_cs_svp64(const uint8_t *src_ptr, int src_stride, PyList_SetItem(state->initial_regs, 4, PyLong_FromLongLong(src_stride)); // Set GPR #5 to the ref_ptr - PyObject *ref_address = PyLong_FromLongLong(ref_ptr_svp64); + PyObject *ref_address = PyLong_FromUnsignedLongLong(ref_ptr_svp64); PyList_SetItem(state->initial_regs, 5, ref_address); // Load data into buffer from real memory for (int r=0; r < 4; r++) { - PyObject *address = PyLong_FromLongLong(ref_ptr_svp64); - uint64_t val = ref_ptr[0]; - val |= (uint64_t)(ref_ptr[1]) << 16; - val |= (uint64_t)(ref_ptr[2]) << 32; - val |= (uint64_t)(ref_ptr[3]) << 48; + PyObject *address = PyLong_FromUnsignedLongLong(ref_ptr_svp64); + uint64_t val = (uint64_t)(src_ptr[0]) & 0xffff; + val |= ((uint64_t)(src_ptr[1]) & 0xffff) << 16; + val |= ((uint64_t)(src_ptr[2]) & 0xffff) << 32; + val |= ((uint64_t)(src_ptr[3]) & 0xffff) << 48; //printf("ref: %p -> %04x %04x %04x %04x, val: %016lx -> %p\n", ref_ptr, ref_ptr[0], ref_ptr[1], ref_ptr[2], ref_ptr[3], val, ref_ptr_svp64); - PyObject *word = PyLong_FromLongLong(val); + PyObject *word = PyLong_FromUnsignedLongLong(val); PyDict_SetItem(state->initial_mem, address, word); ref_ptr += ref_stride; ref_ptr_svp64 += 8; @@ -140,7 +153,7 @@ uint32_t vpx_get4x4sse_cs_svp64(const uint8_t *src_ptr, int src_stride, PyObject *key = PyLong_FromLongLong(3); PyObject *itm = PyDict_GetItem(final_regs, key); PyObject *value = PyObject_GetAttrString(itm, "value"); - uint64_t val = PyLong_AsLongLong(value); + uint64_t val = PyLong_AsUnsignedLongLong(value); // Return value return (uint32_t) val; @@ -167,39 +180,43 @@ void variance_svp64(const uint8_t *src_ptr, int src_stride, // Change the relevant elements, mandatory: body state->binary = PyBytes_FromStringAndSize((const char *)&variance_svp64_real, 1000); // Set GPR #3 to the src_ptr - PyObject *src_address = PyLong_FromLongLong(src_ptr_svp64); + PyObject *src_address = PyLong_FromUnsignedLongLong(src_ptr_svp64); PyList_SetItem(state->initial_regs, 3, src_address); // Load data into buffer from real memory - for (int r=0; r < 4; r++) { - PyObject *address = PyLong_FromLongLong(src_ptr_svp64); - uint64_t val = src_ptr[0]; - val |= (uint64_t)(src_ptr[1]) << 16; - val |= (uint64_t)(src_ptr[2]) << 32; - val |= (uint64_t)(src_ptr[3]) << 48; - PyObject *word = PyLong_FromLongLong(val); - PyDict_SetItem(state->initial_mem, address, word); + for (int r=0; r < h; r++) { + for (int c=0; c < w; c += 4) { + PyObject *address = PyLong_FromUnsignedLongLong(src_ptr_svp64 + c*2); + uint64_t val = (uint64_t)(src_ptr[c + 0]) & 0xffff; + val |= ((uint64_t)(src_ptr[c + 1]) & 0xffff) << 16; + val |= ((uint64_t)(src_ptr[c + 2]) & 0xffff) << 32; + val |= ((uint64_t)(src_ptr[c + 3]) & 0xffff) << 48; + PyObject *word = PyLong_FromUnsignedLongLong(val); + PyDict_SetItem(state->initial_mem, address, word); + } src_ptr += src_stride; - src_ptr_svp64 += 8; + src_ptr_svp64 += w*2; } // Set GPR #4 to the src_stride PyList_SetItem(state->initial_regs, 4, PyLong_FromLongLong(src_stride)); // Set GPR #5 to the ref_ptr - PyObject *ref_address = PyLong_FromLongLong(ref_ptr_svp64); + PyObject *ref_address = PyLong_FromUnsignedLongLong(ref_ptr_svp64); PyList_SetItem(state->initial_regs, 5, ref_address); // Load data into buffer from real memory - for (int r=0; r < 4; r++) { - PyObject *address = PyLong_FromLongLong(ref_ptr_svp64); - uint64_t val = ref_ptr[0]; - val |= (uint64_t)(ref_ptr[1]) << 16; - val |= (uint64_t)(ref_ptr[2]) << 32; - val |= (uint64_t)(ref_ptr[3]) << 48; - //printf("ref: %p -> %04x %04x %04x %04x, val: %016lx -> %p\n", ref_ptr, ref_ptr[0], ref_ptr[1], ref_ptr[2], ref_ptr[3], val, ref_ptr_svp64); - PyObject *word = PyLong_FromLongLong(val); - PyDict_SetItem(state->initial_mem, address, word); + for (int r=0; r < h; r++) { + for (int c=0; c < w; c += 4) { + PyObject *address = PyLong_FromUnsignedLongLong(ref_ptr_svp64 + c*2); + uint64_t val = (uint64_t)(src_ptr[c + 0]) & 0xffff; + val |= ((uint64_t)(src_ptr[c + 1]) & 0xffff) << 16; + val |= ((uint64_t)(src_ptr[c + 2]) & 0xffff) << 32; + val |= ((uint64_t)(src_ptr[c + 3]) & 0xffff) << 48; + //printf("ref: %p -> %04x %04x %04x %04x, val: %016lx -> %p\n", ref_ptr, ref_ptr[0], ref_ptr[1], ref_ptr[2], ref_ptr[3], val, ref_ptr_svp64); + PyObject *word = PyLong_FromUnsignedLongLong(val); + PyDict_SetItem(state->initial_mem, address, word); + } ref_ptr += ref_stride; - ref_ptr_svp64 += 8; + ref_ptr_svp64 += w*2; } // Set GPR #6 to the ref_stride @@ -209,12 +226,12 @@ void variance_svp64(const uint8_t *src_ptr, int src_stride, // Set GPR #8 to the height PyList_SetItem(state->initial_regs, 8, PyLong_FromLongLong(h)); // Set GPR #9 to the sse pointer - PyList_SetItem(state->initial_regs, 9, PyLong_FromLongLong(sse_ptr_svp64)); + PyList_SetItem(state->initial_regs, 9, PyLong_FromUnsignedLongLong(sse_ptr_svp64)); // Set GPR #10 to the sum pointer - PyList_SetItem(state->initial_regs, 10, PyLong_FromLongLong(sum_ptr_svp64)); + PyList_SetItem(state->initial_regs, 10, PyLong_FromUnsignedLongLong(sum_ptr_svp64)); - PyObject *sse_address = PyLong_FromLongLong(sse_ptr_svp64); - PyObject *sum_address = PyLong_FromLongLong(sum_ptr_svp64); + PyObject *sse_address = PyLong_FromUnsignedLongLong(sse_ptr_svp64); + PyObject *sum_address = PyLong_FromUnsignedLongLong(sum_ptr_svp64); PyObject *word = PyLong_FromLongLong(0); PyDict_SetItem(state->initial_mem, sse_address, word); PyDict_SetItem(state->initial_mem, sum_address, word); @@ -253,13 +270,16 @@ void variance_svp64(const uint8_t *src_ptr, int src_stride, printf("Error getting mem dict\n"); } - sse_address = PyLong_FromLongLong(sse_ptr_svp64/8); - sum_address = PyLong_FromLongLong(sum_ptr_svp64/8); + sse_address = PyLong_FromUnsignedLongLong(sse_ptr_svp64/8); + sum_address = PyLong_FromUnsignedLongLong(sum_ptr_svp64/8); PyObject *sse_val = PyDict_GetItem(mem, sse_address); - *sse = (uint32_t) PyLong_AsLongLong(sse_val); + uint64_t val = PyLong_AsUnsignedLongLong(sse_val); + *sse = (uint32_t) val; + printf("val: %016lx, sse: %d/%08x\n", val, *sse, *sse); PyObject *sum_val = PyDict_GetItem(mem, sum_address); - *sum = (int32_t)PyLong_AsLongLong(sum_val); - printf("sse: %d/%08x, sum: %d/%08x\n", *sse, *sse, *sum, *sum); + val = PyLong_AsUnsignedLongLong(sum_val); + *sum = (int32_t) val; + printf("val: %016lx, sum: %d/%08x\n", val, *sum, *sum); }