MCPcopy Create free account
hub / github.com/Meituan-Dianping/SQLAdvisor / do_sub

Function do_sub

strings/decimal.c:1892–2025  ·  view source on GitHub ↗

to=from1-from2. if to==0, return -1/0/+1 - the result of the comparison */

Source from the content-addressed store, hash-verified

1890/* to=from1-from2.
1891 if to==0, return -1/0/+1 - the result of the comparison */
1892static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1893{
1894 int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
1895 frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac);
1896 int frac0= MY_MAX(frac1, frac2), error;
1897 dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0;
1898
1899 /* let carry:=1 if from2 > from1 */
1900 start1=buf1=from1->buf; stop1=buf1+intg1;
1901 start2=buf2=from2->buf; stop2=buf2+intg2;
1902 if (unlikely(*buf1 == 0))
1903 {
1904 while (buf1 < stop1 && *buf1 == 0)
1905 buf1++;
1906 start1=buf1;
1907 intg1= (int) (stop1-buf1);
1908 }
1909 if (unlikely(*buf2 == 0))
1910 {
1911 while (buf2 < stop2 && *buf2 == 0)
1912 buf2++;
1913 start2=buf2;
1914 intg2= (int) (stop2-buf2);
1915 }
1916 if (intg2 > intg1)
1917 carry=1;
1918 else if (intg2 == intg1)
1919 {
1920 dec1 *end1= stop1 + (frac1 - 1);
1921 dec1 *end2= stop2 + (frac2 - 1);
1922 while (unlikely((buf1 <= end1) && (*end1 == 0)))
1923 end1--;
1924 while (unlikely((buf2 <= end2) && (*end2 == 0)))
1925 end2--;
1926 frac1= (int) (end1 - stop1) + 1;
1927 frac2= (int) (end2 - stop2) + 1;
1928 while (buf1 <=end1 && buf2 <= end2 && *buf1 == *buf2)
1929 buf1++, buf2++;
1930 if (buf1 <= end1)
1931 {
1932 if (buf2 <= end2)
1933 carry= *buf2 > *buf1;
1934 else
1935 carry= 0;
1936 }
1937 else
1938 {
1939 if (buf2 <= end2)
1940 carry=1;
1941 else /* short-circuit everything: from1 == from2 */
1942 {
1943 if (to == 0) /* decimal_cmp() */
1944 return 0;
1945 decimal_make_zero(to);
1946 return E_DEC_OK;
1947 }
1948 }
1949 }

Callers 3

decimal_addFunction · 0.85
decimal_subFunction · 0.85
decimal_cmpFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected