integer-division

Removing slow int64 division from fixed point atan2() approximation

大兔子大兔子 提交于 2020-01-24 10:10:48
问题 I made a function to compute a fixed-point approximation of atan2(y, x). The problem is that of the ~83 cycles it takes to run the whole function, 70 cycles (compiling with gcc 4.9.1 mingw-w64 -O3 on an AMD FX-6100) are taken entirely by a simple 64-bit integer division! And sadly none of the terms of that division are constant. Can I speed up the division itself? Is there any way I can remove it? I think I need this division because since I approximate atan2(y, x) with a 1D lookup table I

how to calculate (a times b) divided by c only using 32-bit integer types even if a times b would not fit such a type

别说谁变了你拦得住时间么 提交于 2020-01-11 03:15:08
问题 Consider the following as a reference implementation: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint64_t x = a; x = x * b; x = x / c; return x; } I am interested in an implementation (in C or pseudocode) that does not require a 64-bit integer type. I started sketching an implementation that outlines like this: /* calculates (a * b) / c */ uint32_t muldiv(uint32_t a, uint32_t b, uint32_t c) { uint32_t d1, d2, d1d2; d1 = (1 << 10); d2 = (1 << 10); d1d2 =

Is integer division always equal to the floor of regular division?

南笙酒味 提交于 2020-01-10 20:25:10
问题 For large quotients, integer division ( // ) doesn't seem to be necessarily equal to the floor of regular division ( math.floor(a/b) ). According to Python docs (https://docs.python.org/3/reference/expressions.html - 6.7), floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result. However, math.floor(648705536316023400 / 7) = 92672219473717632 648705536316023400 // 7 = 92672219473717628 '{0:.10f}'.format

Floating Point Exception (Core Dumped) while doing division in assembly

女生的网名这么多〃 提交于 2020-01-03 06:34:44
问题 I'm trying to add 2 two-digit numbers which are bound to yield a two-digit or three-digit number. Here's what I have so far, and when I try to print the carry, it says Floating Point Exception (Core Dumped) section .data msg db "Enter 2 numbers: " msgLen equ $-msg section .bss numa1 resb 1 numa2 resb 1 numb1 resb 1 numb2 resb 1 carry resb 1 section .text global _start _start: ;print message mov eax, 4 mov ebx, 1 mov ecx, msg mov edx, msgLen int 80h ;accept first number (1st digit) mov eax, 3

Efficiently dividing unsigned value by a power of two, rounding up - in CUDA

情到浓时终转凉″ 提交于 2019-12-31 01:46:11
问题 I was just reading: Efficiently dividing unsigned value by a power of two, rounding up and I was wondering what was the fastest way to do this in CUDA. Of course by "fast" I mean in terms of throughput (that question also addressed the case of subsequent calls depending on each other). For the lg() function mentioned in that question (base-2 logarithm of divisor), suppose we have: template <typename T> __device__ int find_first_set(T x); template <> __device__ int find_first_set<uint32_t>

Is div function useful (stdlib.h)? [duplicate]

蹲街弑〆低调 提交于 2019-12-30 07:56:19
问题 This question already has answers here : What is the purpose of the div() library function? (5 answers) Closed 4 months ago . There is a function called div in C,C++ (stdlib.h) div_t div(int numer, int denom); typedef struct _div_t { int quot; int rem; } div_t; But C,C++ have / and % operators. My question is: " When there are / and % operators, Is div function useful ?" 回答1: The div() function returns a structure which contains the quotient and remainder of the division of the first

Integer division compared to floored quotient: why this surprising result?

前提是你 提交于 2019-12-30 06:14:14
问题 The // "integer division" operator of Python surprised me, today: >>> math.floor(11/1.1) 10.0 >>> 11//1.1 9.0 The documentation reads "(floored) quotient of x and y". So, why is math.floor(11/1.1) equal to 10, but 11//1.1 equal to 9? 回答1: Because 1.1 can't be represented in binary form exactly; the approximation is a littler higher than 1.1 - therefore the division result is a bit too small. Try the following: Under Python 2, type at the console: >>> 1.1 1.1000000000000001 In Python 3.1, the

Integer division by 7

ε祈祈猫儿з 提交于 2019-12-30 02:00:07
问题 Source my answer in: Is this expression correct in C preprocessor I'm a little bit out of my forte here, and I'm trying to understand how this particular optimization works. As mentioned in the answer, gcc will optimize integer division by 7 to: mov edx, -1840700269 mov eax, edi imul edx lea eax, [rdx+rdi] sar eax, 2 sar edi, 31 sub eax, edi Which translates back into C as: int32_t divideBySeven(int32_t num) { int32_t temp = ((int64_t)num * -015555555555) >> 32; temp = (temp + num) >> 2;

Why does this division result in zero?

浪尽此生 提交于 2019-12-28 07:06:12
问题 I was writing this code in C when I encountered the following problem. #include <stdio.h> int main() { int i=2; int j=3; int k,l; float a,b; k=i/j*j; l=j/i*i; a=i/j*j; b=j/i*i; printf("%d %d %f %f\n",k,l,a,b); return 0; } Can anyone tell me why the code is returning zero for the first and third variables ( k and a )? 回答1: What I think you are experiencing is integer arithmetic . You correctly suppose l and b to be 2, but incorrectly assume that k and a will be 3 because it's the same

C: converting Farenheit to Celsius

核能气质少年 提交于 2019-12-28 03:15:12
问题 int main (void) { int fahrenheit; // fahrenheit stands for fahrenheit double c; // c stands for celsius printf("Enter your fahrenheit, we'll covnvert it into celsius! "); scanf("%f", &fahrenheit); c = 5/9 * (fahrenheit - 32); printf("Here is your %f in celsius!.\n"); return (0); } I've followed the code through break points and when it takes in my input the calculations are off, but the formula is correct. Some sort of logic error I can't put my finger on. Please help! 回答1: The scanf call